Jump to: navigation, search

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

 
m
Line 5: Line 5:
 
typically fetches content from a server in the background, allowing the user
 
typically fetches content from a server in the background, allowing the user
 
to browse existing content while waiting.
 
to browse existing content while waiting.
 
 
 
  
 
The basic unit of concurrent activity in Eclipse is provided by the <tt>Job</tt> class.  
 
The basic unit of concurrent activity in Eclipse is provided by the <tt>Job</tt> class.  
Line 15: Line 12:
 
are similar to threads because they are not  executed in the
 
are similar to threads because they are not  executed in the
 
calling thread.  This example illustrates how a job is used:
 
calling thread.  This example illustrates how a job is used:
 +
 
<pre>
 
<pre>
 
   Job myJob = new Job("Sample Job") {
 
   Job myJob = new Job("Sample Job") {
Line 24: Line 22:
 
</pre>
 
</pre>
  
 +
When <tt>schedule</tt> 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 <tt>run</tt> method.  This system has a number of advantages over creating and starting a Java thread:
  
 +
* <i>Less overhead</i>.  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.
  
When <tt>schedule</tt> is called, the job is added to a queue of jobs
+
* <i>Support for progress and cancellation</i>.  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.
waiting to be run.  Worker threads then remove them from the queue
+
and invoke their <tt>run</tt> method.  This system has a number of
+
advantages over creating and starting a Java thread:
+
 
+
 
+
* <i>Less overhead</i>.  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.</li>
+
 
+
 
+
 
+
 
+
* <i>Support for progress and cancellation</i>.  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.</li>
+
 
+
 
+
 
+
 
+
* <i>Support for priorities and mutual exclusion</i>.  Jobs can be
+
configured with varying priorities and with scheduling rules that
+
describe when jobs can be run concurrently with other jobs.</li>
+
 
+
 
+
 
+
 
+
* <i>Advanced scheduling features</i>.  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.
+
 
+
 
+
  
 +
* <i>Support for priorities and mutual exclusion</i>.  Jobs can be configured with varying priorities and with scheduling rules that describe when jobs can be run concurrently with other jobs.
  
 +
* <i>Advanced scheduling features</i>.  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: ==
 
== See Also: ==
 
+
*[[FAQ_How_do_I_show_progress_for_things_happening_in_the_background%3F]]
 
+
*[[FAQ_How_do_I_switch_from_using_a_Progress_dialog_to_the_Progress_view%3F]]
[[FAQ_How_do_I_show_progress_for_things_happening_in_the_background%3F]]
+
*[[FAQ_Actions%2C_commands%2C_operations%2C_jobs%3A_What_does_it_all_mean%3F]]
 
+
 
+
[[FAQ_How_do_I_switch_from_using_a_Progress_dialog_to_the_Progress_view%3F]]
+
 
+
 
+
[[FAQ_Actions%2C_commands%2C_operations%2C_jobs%3A_What_does_it_all_mean%3F]]
+
  
 
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>
 
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>

Revision as of 22:11, 29 May 2006

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");
      }
   };
   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.