English 中文(简体)
Eclipse RCP: Only one Job runs at a time?
原标题:

The Jobs API in Eclipse RCP apparently works much differently than I expected. I thought that creating and scheduling multiple Jobs would actually cause multiple worker threads to be created, executing the Jobs in parallel unless there was an ISchedulingRule conflict.

I went back and read the documentation more closely, and also discovered this comment in the JobManager class:

/**
 * Returns a running or blocked job whose scheduling rule conflicts with the 
 * scheduling rule of the given waiting job.  Returns null if there are no 
 * conflicting jobs.  A job can only run if there are no running jobs and no blocked
 * jobs whose scheduling rule conflicts with its rule.
 */

Now it looks to me like the Job manager will only ever attempt to use one background worker thread. Am I completely wrong about this? If I m right,

  • what is the point of scheduling rules and locks? If there is only one worker thread, Jobs can never preemt each other. Wouldn t these only ever be used in case a Job s sleep() method is called (e.g. sleeping while holding a Lock)?
  • does any part of the platform allow two Jobs to actually run concurrently, on multiple worker threads, thus making the above features useful somehow?

What am I missing here?

最佳回答

Take a look at the run method in the documentation, specifically this part:

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.

ASYNC_FINISH there looks interresting.

问题回答

AFAIK creating and scheduling multiple Jobs DO actually cause multiple worker threads to be created and to be executed in parallel. However if you specify an optional scheduling rule to your job (using the setRule() method) and if that rule conflicts with another job s scheduling rule then those two jobs can t run simultaneously.

This Eclipse Corner article provides good description as well as few code samples for Eclipse Job API. The IJobManager API is only needed for advanced job manipulation, e.g. when you need to use locks, synchronize between several jobs, terminate jobs, etc.

Note: Eclipse 4.5M4 will include now (Q4 2014) a way to Support for Job Groups with throttling

See bug 432049:

Eclipse provides a simple Jobs API to perform different tasks in parallel and in asynchronous fashion. One limitation of the Eclipse Jobs is that there is no easy way to limit the number of worker threads being used to execute jobs.
This may lead to a thread pool explosion when many jobs are scheduled in quick succession. Due to that it’s easy to use Jobs to perform different unrelated tasks in parallel, but hard to implement thousands of Jobs co-operating to complete a single large task.

Eclipse currently supports the concept of Job Families, which provides one way of grouping with support for join, cancel, sleep, and wakeup operations on the whole family.

To address all these issue we would like to propose a simple way to group a set of Eclipse Jobs that are responsible for pieces of the same large task.
The API would support throttling, join, cancel, combined progress and error reporting for all of the jobs in the group and the job grouping functionality can be used to rewrite performance critical algorithms to use parallel execution of cooperating jobs.

You can see the implementation in this commit 26471fa





相关问题
Silverlight, Updating the UI during processing

I have a simple silverlight multifile upload application, and i want to provide the user with some feedback, right now its only in a test phase and i dont have the webservice. Somehow i cant get the ...

Is reading from an XmlDocument object thread safe?

I was wondering if i could safely read from an XmlDocument object using SelectNodes() and SelectSingleNode() from multiple threads with no problems. MSDN says that they are not guaranteed to be ...

Terminating a thread gracefully not using TerminateThread()

My application creates a thread and that runs in the background all the time. I can only terminate the thread manually, not from within the thread callback function. At the moment I am using ...

热门标签