Eclipse Community Forums - RDF feed
https://www.eclipse.org/forums/
Eclipse Community ForumsDifference Between Job and Thread?
https://www.eclipse.org/forums/index.php/mv/msg/173498/552128/#msg_552128
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]]>Bahar Sateli2010-08-11T00:46:16-00:00Re: Difference Between Job and Thread?
https://www.eclipse.org/forums/index.php/mv/msg/173498/552141/#msg_552141
> 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?
www.eclipse-tips.com]]>Prakash G.R.2010-08-11T04:30:21-00:00Re: Difference Between Job and Thread?
https://www.eclipse.org/forums/index.php/mv/msg/173498/553162/#msg_553162
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 :-)
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]]>Bahar Sateli2010-08-16T20:43:09-00:00Re: Difference Between Job and Thread?
https://www.eclipse.org/forums/index.php/mv/msg/173498/553289/#msg_553289
> 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.]]>David Wegener2010-08-17T01:15:31-00:00Re: Difference Between Job and Thread?
https://www.eclipse.org/forums/index.php/mv/msg/173498/553621/#msg_553621
> 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]]>Prakash G.R.2010-08-18T08:26:17-00:00Re: Difference Between Job and Thread?
https://www.eclipse.org/forums/index.php/mv/msg/173498/554257/#msg_554257
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]]>Bahar Sateli2010-08-20T19:45:10-00:00Re: Difference Between Job and Thread?
https://www.eclipse.org/forums/index.php/mv/msg/173498/554274/#msg_554274
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();
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!!! ]]>Bahar Sateli2010-08-20T21:12:42-00:00Re: Difference Between Job and Thread?
https://www.eclipse.org/forums/index.php/mv/msg/173498/554299/#msg_554299
> 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!!! :? ]]>David Wegener2010-08-21T02:44:09-00:00Re: Difference Between Job and Thread?
https://www.eclipse.org/forums/index.php/mv/msg/173498/557798/#msg_557798
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 !!! I'm all mixed up!
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?]]>Bahar Sateli2010-09-09T00:09:10-00:00Re: Difference Between Job and Thread?
https://www.eclipse.org/forums/index.php/mv/msg/173498/559491/#msg_559491
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! ]]>Bahar Sateli2010-09-16T19:42:44-00:00Re: Difference Between Job and Thread?
https://www.eclipse.org/forums/index.php/mv/msg/173498/559540/#msg_559540
> 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]]>Daniel Krügler2010-09-17T05:46:02-00:00