Difference Between Job and Thread? [message #552128] |
Wed, 11 August 2010 00:46 |
|
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 #553257 is a reply to message #552128] |
Mon, 16 August 2010 20:43 |
|
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 #554257 is a reply to message #553621] |
Fri, 20 August 2010 19:45 |
|
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 |
|
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!!!
|
|
|
|
|
|
Re: Difference Between Job and Thread? [message #559540 is a reply to message #559491] |
Fri, 17 September 2010 05:46 |
Daniel Krügler 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
|
|
|
Powered by
FUDForum. Page generated in 0.03748 seconds