Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Simple question on using "Job" inside Eclipse
Simple question on using "Job" inside Eclipse [message #779613] Mon, 16 January 2012 07:00 Go to next message
Sai Missing name is currently offline Sai Missing nameFriend
Messages: 25
Registered: March 2011
Junior Member
Hi all-

I got a simple question on the "Job" concept in the eclipse plugin platform:
how could I know is a Job is executed in a new thread or in
the current thread?

For example:

new ReportingJob().schedule(); //I simply call schedule();

private class ReportingJob extends Job {
protected IStatus run(IProgressMonitor monitor) {
// ... omitted
return Status.OK_STATUS;
}
}



So, is the above ReportingJob() instance running in a new thread or not?

I found the Worker/Job scheduling mechanism inside eclipse is quite
complex, sometimes, a Job is executed in a new thread, while sometimes not.
it would be great if someone can kindly point me to some reference or tutorials.

thanks very much. Any comments are welcome

-Sai
Re: Simple question on using "Job" inside Eclipse [message #780135 is a reply to message #779613] Tue, 17 January 2012 08:52 Go to previous messageGo to next message
Deepak Azad is currently offline Deepak AzadFriend
Messages: 543
Registered: July 2009
Senior Member
On 1/16/2012 12:30 PM, Sai Mising name wrote:
> So, is the above ReportingJob() instance running in a new thread or not?
Same thread.

From javadoc of org.eclipse.core.runtime.jobs.Job.run(IProgressMonitor)
"Jobs can optionally finish their execution asynchronously (in another
thread) by returning a result status of ASYNC_FINISH. Jobs that finish
asynchronously must specify the execution thread by calling setThread,
and must indicate when they are finished by calling the method done."

> I found the Worker/Job scheduling mechanism inside eclipse is quite
> complex, sometimes, a Job is executed in a new thread, while sometimes not.
> it would be great if someone can kindly point me to some reference or
> tutorials.

You can go through this Eclipse Corner Article:
http://www.eclipse.org/articles/Article-Concurrency/jobs-api.html

--
Deepak Azad
http://wiki.eclipse.org/JDT/FAQ
Re: Simple question on using "Job" inside Eclipse [message #780429 is a reply to message #780135] Tue, 17 January 2012 19:53 Go to previous messageGo to next message
Sai Missing name is currently offline Sai Missing nameFriend
Messages: 25
Registered: March 2011
Junior Member
Hi Deepak:

Thanks very much for your reply.

The information you provided is *very* useful.

As far as I understand, the purpose of "Job" is to make the UI responsive and active. Thus, all Jobs should be run in a separate and background thread instead of the current thread. Is that right? (probably the only exception is the UIJob, which explicitly desigates some operations to be run on the event dispatch thread).

So, for a non UI-Job like:

new ReportingJob().schedule(); //it will be executed in a separate thread? right?

private class ReportingJob extends Job {
protected IStatus run(IProgressMonitor monitor) {
//if I want to access UI object here,
//I must wrap it using asyncExec / syncExec to avoid invalid thread access
//right?

return Status.OK_STATUS;
}
}

If I understood something wrong, please point me out! Thank you!

-Sai
Re: Simple question on using "Job" inside Eclipse [message #780456 is a reply to message #780429] Wed, 18 January 2012 04:12 Go to previous messageGo to next message
Deepak Azad is currently offline Deepak AzadFriend
Messages: 543
Registered: July 2009
Senior Member
> As far as I understand, the purpose of "Job" is to make the UI
> responsive and active. Thus, all Jobs should be run in a separate and
> background thread instead of the current thread. Is that right?
What was I thinking? :-)
Of course a Job will run in a separate thread. It can be instructive to
have a look at org.eclipse.core.runtime.jobs.IJobManager and its
implementation org.eclipse.core.internal.jobs.JobManager.

> //if I want to access UI object here,
> //I must wrap it using asyncExec / syncExec to avoid invalid thread access
> //right?
Yes, or you can simply use UIJob if you want to do this.

--
Deepak Azad
http://wiki.eclipse.org/JDT/FAQ
Re: Simple question on using "Job" inside Eclipse [message #780684 is a reply to message #780456] Thu, 19 January 2012 03:24 Go to previous message
Sai Missing name is currently offline Sai Missing nameFriend
Messages: 25
Registered: March 2011
Junior Member
Hi Deepak-

Thanks for your answer! Really appreciate your help.

A closely-related question, I also see JDT (and other eclipse components) uses
the class "Action" extensively. Usually, programmers override the

"public void run() "

method to do something. However, I found I still got invalid thread access errors
sometimes when accessing GUI object inside "run()", while sometimes not.

So, I am really wondering are all Action#run must be executed in UI-thread or non-UI thread? or it really depends? If so, could anyone kindly points me to some resources about code in Action is executed?

Thanks a lot

-Sai


Deepak Azad wrote on Tue, 17 January 2012 23:12
> As far as I understand, the purpose of "Job" is to make the UI
> responsive and active. Thus, all Jobs should be run in a separate and
> background thread instead of the current thread. Is that right?
Of course a Job will run in a separate thread. It can be instructive to
have a look at org.eclipse.core.runtime.jobs.IJobManager and its
implementation org.eclipse.core.internal.jobs.JobManager.

> //if I want to access UI object here,
> //I must wrap it using asyncExec / syncExec to avoid invalid thread access
> //right?
Yes, or you can simply use UIJob if you want to do this.

--
Deepak Azad
http://wiki.eclipse.org/JDT/FAQ

Previous Topic:Confused on the subclasses of EventListener in eclipse
Next Topic:Broken Eclipse M2 Plugin
Goto Forum:
  


Current Time: Tue Apr 16 06:02:56 GMT 2024

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

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

Back to the top