Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Difference Between Job and Thread?
Difference Between Job and Thread? [message #552128] Wed, 11 August 2010 00:46 Go to next message
Bahar Sateli is currently offline Bahar SateliFriend
Messages: 19
Registered: May 2010
Location: Canada
Junior Member
Hey all,

I'm developing a plug-in for Eclipse. I'm executing a long-running service invocation job over the network and for that, I'm using a thread that is supposed to work concurrently with the UI thread. Using IWorkbenchWindow.run(...) method I'm starting the thread and informing the user about the progress at the same time.

The problem is IWorkbenchWindow.run method is still using the UI thread and user can not interact with the GUI unless the thread is done.

So I googled and found out that since Eclipse 3.0 a Job API has been introduced. Can somebody please tell me the exact difference between a Job and a Thread?

Thanks a lot,
Bahar
Re: Difference Between Job and Thread? [message #552141 is a reply to message #552128] Wed, 11 August 2010 04:30 Go to previous messageGo to next message
Prakash G.R. is currently offline Prakash G.R.Friend
Messages: 621
Registered: July 2009
Senior Member
On 11/08/10 6:16 AM, Bahar Sateli wrote:
> So I googled and found out that since Eclipse 3.0 a Job API has been
> introduced. Can somebody please tell me the exact difference between a
> Job and a Thread?

Jobs are preferred way of scheduling a background task in Eclipse, not
threads. See more at:
http://www.eclipse.org/articles/Article-Concurrency/jobs-api .html


--
- Prakash
Platform UI Team, IBM

www.eclipse-tips.com
Re: Difference Between Job and Thread? [message #553162 is a reply to message #552128] Mon, 16 August 2010 15:12 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

As Prakash mentions, read the Jobs API article.

But a thread and a job can fulfill the same purpose. The important
thing to remember is that you do the long running operations in your Job
(thread) and only occasionally post an update back to the UI thread.

If you join on a job or thread from the UI thread you will freeze your
UI, and you may have well done the operations in line (please don't do
this :-)

PW

--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Re: Difference Between Job and Thread? [message #553257 is a reply to message #552128] Mon, 16 August 2010 20:43 Go to previous messageGo to next message
Bahar Sateli is currently offline Bahar SateliFriend
Messages: 19
Registered: May 2010
Location: Canada
Junior Member
Thank you Prakash and Paul.

Well...I really couldn't read the Eclipse Job API from their website http:// help.eclipse.org/help33/topic/org.eclipse.platform.doc.isv/r eference/api/org/eclipse/core/runtime/jobs/Job.html, I don't know why I always get a Bad gateway error! But I read some stuff from Google's cached page and also from the Prakash's link.

Now there's only one problem left:

Paul, as you said it is not a good idea to call the join() method on a job. So here's the tricky part:

In my plug-in I have to wait for the job to finish, then open a view to show the results. If the sequence is not correct then I will have a view with an empty result table. I read that a job has different states (i.e RUNNING, NONE etc.) but there isn't a FINISH state! So there was no way for me to figure if the job has finished or not, and I used the join() method and now it's blocking the UI thread and ruining the whole philosophy of using the job at the first place.

Is there any other way to find about the FINISHED state of a job?

Thank you.
Bahar
Re: Difference Between Job and Thread? [message #553289 is a reply to message #553257] Tue, 17 August 2010 01:15 Go to previous messageGo to next message
David Wegener is currently offline David WegenerFriend
Messages: 1445
Registered: July 2009
Senior Member
On Mon, 2010-08-16 at 16:43 -0400, Bahar Sateli wrote:
> Thank you Prakash and Paul.
>
> Well...I really couldn't read the Eclipse Job API from their website http://help.eclipse.org/help33/topic/org.eclipse.platform.do c.isv/reference/api/org/eclipse/core/runtime/jobs/Job.html, I don't know why I always get a Bad gateway error! But I read some stuff from Google's cached page and also from the Prakash's link.
>
> Now there's only one problem left:
>
> Paul, as you said it is not a good idea to call the join() method on a job. So here's the tricky part:
>
> In my plug-in I have to wait for the job to finish, then open a view to show the results. If the sequence is not correct then I will have a view with an empty result table. I read that a job has different states (i.e RUNNING, NONE etc.) but there isn't a FINISH state! So there was no way for me to figure if the job has finished or not, and I used the join() method and now it's blocking the UI thread and ruining the whole philosophy of using the job at the first place.
>
> Is there any other way to find about the FINISHED state of a job?
>
> Thank you.
> Bahar

Pass your job a callback object that has a finish method (or something
that makes sense for you purposes). Call the finish method when your
job completes. In the finish method call Display.asyncExec() to display
your view.
Re: Difference Between Job and Thread? [message #553621 is a reply to message #553257] Wed, 18 August 2010 08:26 Go to previous messageGo to next message
Prakash G.R. is currently offline Prakash G.R.Friend
Messages: 621
Registered: July 2009
Senior Member
On 17/08/10 2:13 AM, Bahar Sateli wrote:
> Is there any other way to find about the FINISHED state of a job?

check IJobChangeListener.done()

--
- Prakash
Platform UI Team, IBM

www.eclipse-tips.com
Re: Difference Between Job and Thread? [message #554257 is a reply to message #553621] Fri, 20 August 2010 19:45 Go to previous messageGo to next message
Bahar Sateli is currently offline Bahar SateliFriend
Messages: 19
Registered: May 2010
Location: Canada
Junior Member
Hello. I finally figured out how to get the finished state of my job using IJobChangeListener. So here is the code:

invocationJob.addJobChangeListener(new JobChangeAdapter() {
public void done(IJobChangeEvent event) {
if (event.getResult().isOK()){
System.out.println("DONE");
FileSelectionHandler.flag = false;
}else{
System.out.println("NOT DONE");
}
}
});

invocationJob.schedule();

and then I used a flag to trigger the other class.

Thank you
Bahar
Re: Difference Between Job and Thread? [message #554274 is a reply to message #554257] Fri, 20 August 2010 21:12 Go to previous messageGo to next message
Bahar Sateli is currently offline Bahar SateliFriend
Messages: 19
Registered: May 2010
Location: Canada
Junior Member
But still my first problem exists. The job is blocking the UI thread.

Here's what is happening:

1. A dialog opens and allows the user to select a service and press 'run'.
2. The dialog handler class creates a new 'session' instance and calls its 'invoke' method.
3. Inside the 'invoke' method, the followings happen:
3.1. invocationJob = new ServiceInvocationJob(serviceName);
3.2. invocationJob.setUser(true);
3.3. invocationJob obtains more data about the job
3.4. a IJobChangeListener is added to the job
3.5. invocationJob.schedule();

4. Here is what happens in job run method

@Override
protected IStatus run(IProgressMonitor monitor) {
monitor.beginTask("Retrieving results...", 1000000000);
try
{

literalInvocation(); // This is the long-running process

}
catch( javax.xml.ws.WebServiceException e )
{
System.out.println( "\nConnection error" );
}finally{
this.done(ASYNC_FINISH);
}
monitor.done();
return Status.OK_STATUS;
}

5. The handler class waits to see if the job is done (Session.invocationJob.getResult().isOK())

6 The handler class opens the related views.

Am I missing something in here? I've spent days on this!!! Confused
Re: Difference Between Job and Thread? [message #554299 is a reply to message #554274] Sat, 21 August 2010 02:44 Go to previous messageGo to next message
David Wegener is currently offline David WegenerFriend
Messages: 1445
Registered: July 2009
Senior Member
On Fri, 2010-08-20 at 17:12 -0400, Bahar Sateli wrote:
> But still my first problem exists. The job is blocking the UI thread.
>
> Here's what is happening:
>
> 1. A dialog opens and allows the user to select a service and press 'run'..
> 2. The dialog handler class creates a new 'session' instance and calls its 'invoke' method.
> 3. Inside the 'invoke' method, the followings happen:
> 3.1. invocationJob = new ServiceInvocationJob(serviceName);
> 3.2. invocationJob.setUser(true);
> 3.3. invocationJob obtains more data about the job
> 3.4. a IJobChangeListener is added to the job
> 3.5. invocationJob.schedule();
>
> 4. Here is what happens in job run method
>
> @Override
> protected IStatus run(IProgressMonitor monitor) {
> monitor.beginTask("Retrieving results...", 1000000000);
> try
> {
>
> literalInvocation(); // This is the long-running process
>
> }
> catch( javax.xml.ws.WebServiceException e )
> {
> System.out.println( "\nConnection error" );
> }finally{
> this.done(ASYNC_FINISH);
> }
> monitor.done();
> return Status.OK_STATUS;
> }
>
> 5. The handler class waits to see if the job is done (Session.invocationJob.getResult().isOK())

Is step 5 being performed on the same thread that invoked the dialog
handler? If so, then that is what is locking up your UI. You have to
return from the call to your dialog handler in order for the UI thread
to process other events.

Your JobChangeAdapter.done method needs to invoke the code to opent the
related views.

>
> 6 The handler class opens the related views.
>
> Am I missing something in here? I've spent days on this!!! :?
Re: Difference Between Job and Thread? [message #557798 is a reply to message #554299] Thu, 09 September 2010 00:09 Go to previous messageGo to next message
Bahar Sateli is currently offline Bahar SateliFriend
Messages: 19
Registered: May 2010
Location: Canada
Junior Member
Thanks David, Yes the problem was on step 5. I was constantly checking the value of a flag to start opening the views. So I guess it was blocking the UI.
Instead I put all the codes needed to open the views into a separate static method and called if from the Job class once it's finished his job.

However, when I'm back inside the view opener method and I want to open a view, I get an Invalid Thread Access exception.

Here is the faulty code line:

public static IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event); // ExecutionEvent event
window.getActivePage().showView("myView"); // FAULTY LINE

Here is the console log:

org.eclipse.swt.SWTException: Invalid thread access
at org.eclipse.swt.SWT.error(SWT.java:4083)
at org.eclipse.swt.SWT.error(SWT.java:3998)
at org.eclipse.swt.SWT.error(SWT.java:3969)
at org.eclipse.swt.widgets.Widget.error(Widget.java:466)
at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:404)
at org.eclipse.swt.widgets.Widget.checkParent(Widget.java:342)
at org.eclipse.swt.widgets.Widget.<init>(Widget.java:220)
at org.eclipse.swt.widgets.Control.<init>(Control.java:95)
at org.eclipse.swt.widgets.Scrollable.<init>(Scrollable.java:74)
at org.eclipse.swt.widgets.Composite.<init>(Composite.java:95)
at org.eclipse.ui.internal.PartPane.createControl(PartPane.java :148)
at org.eclipse.ui.internal.ViewPane.createControl(ViewPane.java :127)
at org.eclipse.ui.internal.ViewReference.createPartHelper(ViewR eference.java:310)
at org.eclipse.ui.internal.ViewReference.createPart(ViewReferen ce.java:229)
at org.eclipse.ui.internal.WorkbenchPartReference.getPart(Workb enchPartReference.java:595)
at org.eclipse.ui.internal.Perspective.showView(Perspective.jav a:2245)
at org.eclipse.ui.internal.WorkbenchPage.busyShowView(Workbench Page.java:1071)
at org.eclipse.ui.internal.WorkbenchPage$20.run(WorkbenchPage.j ava:3822)
at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator .java:52)
at org.eclipse.ui.internal.WorkbenchPage.showView(WorkbenchPage .java:3819)
at org.eclipse.ui.internal.WorkbenchPage.showView(WorkbenchPage .java:3795)
at info.semanticsoftware.semassist.client.eclipse.handlers.File SelectionHandler.openViews(FileSelectionHandler.java:125)
at info.semanticsoftware.semassist.client.eclipse.handlers.Serv iceInvocationJob.run(ServiceInvocationJob.java:47)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)

So I debugged and figured out that Inside the Worker class run method I get exceptions on this line:

try {

result = currentJob.run(currentJob.getProgressMonitor());

} catch (Exception e) {

result = handleException(currentJob, e);

}

It's so confusing that I'm running a job but it's calling a Worker class, which itself is extending Thread !!! Shocked I'm all mixed up! Rolling Eyes

UPDATE: The showView("myView") is the cause of the problem. It apparently tries to access the Display thread to show the view and because it's a non_UI thread, it causes an Invalid Thread Access exception. Any ideas to fix it?

[Updated on: Mon, 13 September 2010 23:01]

Report message to a moderator

Re: Difference Between Job and Thread? [message #559491 is a reply to message #557798] Thu, 16 September 2010 19:42 Go to previous messageGo to next message
Bahar Sateli is currently offline Bahar SateliFriend
Messages: 19
Registered: May 2010
Location: Canada
Junior Member
Okay... again I'm answering my own question! Cool

A little flash back:
I had a job doing some service invocation over the net and bringing back some results that had to be shown in an eclipse view part instance.
The invocation was right, the results were ready, but when I wanted to open the view... BAM! Invalid Thread Access exception was thrown.

What was the problem? Simply because my job (as it's been defined in Eclipse Job API) was a Non-UI thread by nature and it didn't have access to the UI thread in order to use, for example, the Display.

The solution was to make a hook to the UI thread. How? Like this:

new Thread(new Runnable() {

@Override
public void run() {

Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() { // Do the UI stuff here. i.e. opening a view }
});

}

}).start();

I put this at the end of my job's run method. So when finished with the invocation, it can open the view after. Problem... solved! Smile

[Updated on: Thu, 16 September 2010 19:44]

Report message to a moderator

Re: Difference Between Job and Thread? [message #559540 is a reply to message #559491] Fri, 17 September 2010 05:46 Go to previous message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 16.09.2010 21:42, Bahar Sateli wrote:
> Okay... again I'm answering my own question! 8)
>
> A little flash back:
> I had a job doing some service invocation over the net and bringing back
> some results that had to be shown in an eclipse view part instance.
> The invocation was right, the results were ready, but when I wanted to
> open the view... BAM! Invalid Thread Access thread was thrown.
>
> What was the problem? Simply because my job as it's been defined in
> Eclipse Job API is a Non-UI thread by nature, it didn't have access to
> the UI thread in order to use, for example, the Display.
>
> The solution was to make a hook to the UI thread. How? Like this:
>
> new Thread(new Runnable() {
>
> @Override
> public void run() {
>
> Display.getDefault().asyncExec(new Runnable() {
> @Override
> public void run() { // Do the UI stuff here. i.e. opening a view }
> });
>
> }
>
> }).start();
>
> I put this at the end of my job's run method. So when finished with the
> invocation, it can open the view after. Problem... solved! :)

Note that this "solution" is working by chance and is *not* the right
way to do that. Also there is an unnecessary overhead because of
creating a new thread that is absolutely not need. You just can call an
Display.asyncExec at any stage of your non-UI job where you think it's
OK to refresh the UI and if you want to ensure that your UI is
refreshed at the very end of your job, you just add a
IJobChangeListener listener for your job (your job can register for
that in it's own constructor, if you want) and during the
IJobChangeListener.done event you do the final Display.asyncExec.

HTH & Greetings from Bremen,

Daniel Krügler
Previous Topic:How to add my own annotation column to the left ruler of editor
Next Topic:Plugin/Tool for quick Eclipse RCP mockups?
Goto Forum:
  


Current Time: Thu Apr 18 23:58:58 GMT 2024

Powered by FUDForum. Page generated in 0.03408 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top