Jump to: navigation, search

Difference between revisions of "FAQ Does the platform have support for concurrency?"

m
 
(2 intermediate revisions by 2 users not shown)
Line 17: Line 17:
 
       public IStatus run(IProgressMonitor monitor) {
 
       public IStatus run(IProgressMonitor monitor) {
 
         System.out.println("This is running in a job");
 
         System.out.println("This is running in a job");
 +
        return Status.OK_STATUS;
 
       }
 
       }
 
   };
 
   };
Line 38: Line 39:
 
*[[FAQ How do I switch from using a Progress dialog to the Progress view?]]
 
*[[FAQ How do I switch from using a Progress dialog to the Progress view?]]
 
*[[FAQ Actions, commands, operations, jobs: What does it all mean?]]
 
*[[FAQ Actions, commands, operations, jobs: What does it all mean?]]
 
+
*[http://www.eclipse.org/articles/Article-Concurrency/jobs-api.html On the Job: The Eclipse Jobs API]
 +
*[http://www.vogella.de/articles/EclipseJobs/article.html Eclipse Jobs and Background processing Tutorial]
 
{{Template:FAQ_Tagline}}
 
{{Template:FAQ_Tagline}}

Latest revision as of 15:09, 11 February 2011

In Eclipse 3.0, infrastructure was added to support running application code concurrently. This support is useful when your plug-in has to perform some CPU-intensive work and you want to allow the user to continue working while it goes on. For example, a Web browser or mail client typically fetches content from a server in the background, allowing the user to browse existing content while waiting.

The basic unit of concurrent activity in Eclipse is provided by the Job class. Jobs are a cross between the interface java.lang.Runnable and the class java.lang.Thread. Jobs are similar to runnables because they encapsulate behavior inside a run method and are similar to threads because they are not executed in the calling thread. This example illustrates how a job is used:

   Job myJob = new Job("Sample Job") {
      public IStatus run(IProgressMonitor monitor) {
         System.out.println("This is running in a job");
         return Status.OK_STATUS;
      }
   };
   myJob.schedule();

When schedule is called, the job is added to a queue of jobs waiting to be run. Worker threads then remove them from the queue and invoke their run method. This system has a number of advantages over creating and starting a Java thread:

  • Less overhead. Creating a new thread every time you want to run something can be expensive. The job infrastructure uses a thread pool that reuses the same threads for many jobs.
  • Support for progress and cancellation. Jobs are provided with a progress monitor object, allowing them to respond to cancellation requests and to report how much work they have done. The UI can listen to these progress messages and display feedback as the job executes.
  • Support for priorities and mutual exclusion. Jobs can be configured with varying priorities and with scheduling rules that describe when jobs can be run concurrently with other jobs.
  • Advanced scheduling features. You can schedule a job to run at any time in the future and to reschedule itself after completing.

Note that the same job instance can be rerun as many times as you like, but you cannot schedule a job that is already sleeping or waiting to run. Jobs are often written as singletons, both to avoid the possibility of the same job being scheduled multiple times and to avoid the overhead of creating a new object every time it is run.

See Also:


This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.