Class JobGroup


  • public class JobGroup
    extends org.eclipse.core.internal.jobs.InternalJobGroup
    JobGroups support throttling, join, cancel, combined progress and error reporting on a group of jobs.
    • A JobGroup object represents a group of Jobs. Any number of jobs can be added to a group, but a Job can be part of only one group at a time.
    • A JobGroup can be configured with a throttling number, so that only that many jobs from the group are allowed to run in parallel.
    • One can join on all of the jobs in the group and observe the completion progress of those jobs.
    • One can cancel all the jobs in the group.
    • A JobGroup consolidates the return statuses of all the jobs in the group and a single MultiStatus message is available after all the jobs in the group are completed.

    JobGroups maintain state for the collective status of the jobs belonging to the group. When constructed, a job group starts with a state value of NONE. When any job belonging to the group is scheduled to run, the job group moves into the ACTIVE state. A job group enters the CANCELING state when cancellation of the whole group is requested. The group will be in this state until all the jobs in the group have finished either through cancellation or normal completion. When a job group is in the CANCELING state, newly scheduled jobs which are part of that group are immediately canceled. When execution of all the jobs belonging to a job group finishes (either normally or through cancellation), the job group state changes back to NONE.

    Since:
    3.7
    See Also:
    IJobManager
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int ACTIVE
      JobGroup state code (value 1) indicating that at least one job belonging to the group is running or scheduled to run.
      static int CANCELING
      JobGroup state code (value 2) indicating that cancellation of the whole group is requested.
      static int NONE
      JobGroup state code (value 0) indicating that none of the jobs belonging to the group are running or scheduled to run.
    • Constructor Summary

      Constructors 
      Constructor Description
      JobGroup​(String name, int maxThreads, int seedJobsCount)
      Creates a new job group with the specified name and maxThreads.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void cancel()
      Cancels all jobs belonging to this job group.
      protected MultiStatus computeGroupResult​(List<IStatus> jobResults)
      This method is called by the JobManager when all the jobs belonging to the group are completed.
      List<Job> getActiveJobs()
      Returns all waiting, executing and sleeping jobs belonging to this job group.
      int getMaxThreads()
      Returns the maximum number of threads allowed to be scheduled by the jobs belonging to the group at any given time, or zero to indicate that no throttling should be applied and all jobs should be allowed to run as soon as possible.
      String getName()
      Returns the human readable name of this job group.
      MultiStatus getResult()
      Returns the result of this job group's last run.
      int getState()
      Returns the state of the job group.
      boolean join​(long timeoutMillis, IProgressMonitor monitor)
      Waits until either all jobs belonging to this job group have finished or the given timeout has expired.
      protected boolean shouldCancel​(IStatus lastCompletedJobResult, int numberOfFailedJobs, int numberOfCanceledJobs)
      This method is called by the JobManager after the completion of every job belonging to this group, and is used to control the job group's cancellation policy.
    • Field Detail

      • NONE

        public static final int NONE
        JobGroup state code (value 0) indicating that none of the jobs belonging to the group are running or scheduled to run.
        See Also:
        getState(), Constant Field Values
      • ACTIVE

        public static final int ACTIVE
        JobGroup state code (value 1) indicating that at least one job belonging to the group is running or scheduled to run.
        See Also:
        getState(), Constant Field Values
      • CANCELING

        public static final int CANCELING
        JobGroup state code (value 2) indicating that cancellation of the whole group is requested. The group will be in this state until all the jobs in the group have finished either through cancellation or normal completion. When a job group is in this state, newly scheduled jobs which are part of that group are immediately canceled.
        See Also:
        Constant Field Values
    • Constructor Detail

      • JobGroup

        public JobGroup​(String name,
                        int maxThreads,
                        int seedJobsCount)
        Creates a new job group with the specified name and maxThreads. The job group name is a human-readable value that is displayed to users. The name does not need to be unique, but it must not be null. The maxThreads indicates the maximum number of threads allowed to be concurrently scheduled by the jobs belonging to the group at any given time, or zero to indicate that no throttling should be applied and all jobs should be allowed to run as soon as possible.
        Parameters:
        name - the name of the job group.
        maxThreads - the maximum number of threads allowed to be concurrently scheduled, or zero to indicate that no throttling should be applied and all jobs should be allowed to run as soon as possible.
        seedJobsCount - the initial number of jobs that will be added to the job group. This is the initial count of jobs with which the creator of the job group will "seed" the job group. Those initial jobs may discover more work and add yet more jobs, but those additional jobs should not be included in this initial "seed" count. If this value is set too high, the job group will never transition to the done (NONE) state, join(long, IProgressMonitor) calls will hang, and getResult() calls will return invalid results. If this value is set too low, the job group may transition to the (NONE) state before all of the jobs have been scheduled, causing a join(long, IProgressMonitor) call to return too early.
    • Method Detail

      • getName

        public final String getName()
        Returns the human readable name of this job group. The name is never null.
        Overrides:
        getName in class org.eclipse.core.internal.jobs.InternalJobGroup
        Returns:
        the name of this job group
      • getMaxThreads

        public final int getMaxThreads()
        Returns the maximum number of threads allowed to be scheduled by the jobs belonging to the group at any given time, or zero to indicate that no throttling should be applied and all jobs should be allowed to run as soon as possible.
        Overrides:
        getMaxThreads in class org.eclipse.core.internal.jobs.InternalJobGroup
        Returns:
        the maximum number of threads allowed to be used.
      • getResult

        public final MultiStatus getResult()
        Returns the result of this job group's last run. If a job group completes and then its jobs are rescheduled, this method returns the results of the previous run.
        Overrides:
        getResult in class org.eclipse.core.internal.jobs.InternalJobGroup
        Returns:
        the result of this job group's last run, or null if this job group has never finished running.
      • getState

        public final int getState()
        Returns the state of the job group. Result will be one of:
        • JobGroup.NONE - when the jobs belonging to the group are not yet scheduled to run.
        • JobGroup.ACTIVE - when the jobs belonging to the group are running or scheduled to run.
        • JobGroup.CANCELING - when the jobs belonging to the group are being canceled.

        Note that job group state is inherently volatile, and in most cases clients cannot rely on the result of this method being valid by the time the result is obtained. For example, if getState returns ACTIVE, the job group may have actually completed by the time the getState method returns. All that clients can infer from invoking this method is that the job group was recently in the returned state.

        Overrides:
        getState in class org.eclipse.core.internal.jobs.InternalJobGroup
        Returns:
        the job group state
      • getActiveJobs

        public final List<Job> getActiveJobs()
        Returns all waiting, executing and sleeping jobs belonging to this job group. If no jobs are found, an empty array is returned.
        Overrides:
        getActiveJobs in class org.eclipse.core.internal.jobs.InternalJobGroup
        Returns:
        the list of active jobs
        See Also:
        Job.setJobGroup(JobGroup)
      • cancel

        public final void cancel()
        Cancels all jobs belonging to this job group. Jobs belonging to this group that are currently in the WAITING state will be removed from the queue. Sleeping jobs will be discarded without having a chance to wake up. Currently executing jobs will be asked to cancel but there is no guarantee that they will do so.

        The job group will be placed into CANCELING state and will be in that state until all the jobs in the group have finished either through cancellation or normal completion. When a job group is in the CANCELING state, newly scheduled jobs which are part of the group are immediately canceled.

        Overrides:
        cancel in class org.eclipse.core.internal.jobs.InternalJobGroup
        See Also:
        Job.setJobGroup(JobGroup), getState()
      • join

        public final boolean join​(long timeoutMillis,
                                  IProgressMonitor monitor)
                           throws InterruptedException,
                                  OperationCanceledException
        Waits until either all jobs belonging to this job group have finished or the given timeout has expired. This method will block the calling thread until all such jobs have finished executing, or the given timeout is expired, or the given progress monitor is canceled by the user or the calling thread is interrupted. If there are no jobs belonging to the group that are currently waiting, running, or sleeping, this method returns immediately. Feedback on how the join is progressing is provided to the given progress monitor.

        If this method is called while the job manager is suspended, only jobs that are currently running will be joined. Once there are no jobs belongs to the group in the Job.RUNNING state, the method returns.

        Jobs may be added to this job group after the initial set of jobs are scheduled, and this method will wait for all newly added jobs to complete (or the given timeout has expired), even if they are added to the group after this method is invoked.

        Throws an OperationCanceledException when the given progress monitor is canceled. Canceling the monitor does not cancel the jobs belonging to the group and, if required, the group may be canceled explicitly using the cancel() method.

        NOTE: If the job manager is suspended, the result of the job group run may not be set when this method returns.

        Overrides:
        join in class org.eclipse.core.internal.jobs.InternalJobGroup
        Parameters:
        timeoutMillis - the maximum amount of time to wait for the join to complete, or zero for no timeout.
        monitor - the progress monitor for reporting progress on how the wait is progressing and to be able to cancel the join operation, or null if no progress monitoring is required.
        Returns:
        true when all the jobs in the group complete, or false when the operation is not completed within the given time.
        Throws:
        InterruptedException - if the calling thread is interrupted while waiting
        OperationCanceledException - if the progress monitor is canceled while waiting
        See Also:
        Job.setJobGroup(JobGroup), cancel()
      • shouldCancel

        protected boolean shouldCancel​(IStatus lastCompletedJobResult,
                                       int numberOfFailedJobs,
                                       int numberOfCanceledJobs)
        This method is called by the JobManager after the completion of every job belonging to this group, and is used to control the job group's cancellation policy. Returning true from this function causes all remaining running and scheduled jobs to be canceled.

        The default implementation returns true when numberOfFailedJobs > 0. Subclasses may override this method to implement a different cancellation strategy.

        Overrides:
        shouldCancel in class org.eclipse.core.internal.jobs.InternalJobGroup
        Parameters:
        lastCompletedJobResult - result of the last completed job belonging to this group.
        numberOfFailedJobs - the total number of jobs belonging to this group that are finished with status IStatus.ERROR.
        numberOfCanceledJobs - the total number of jobs belonging to this group that are finished with status IStatus.CANCEL.
        Returns:
        true when the remaining jobs belonging to this group should be canceled.
      • computeGroupResult

        protected MultiStatus computeGroupResult​(List<IStatus> jobResults)
        This method is called by the JobManager when all the jobs belonging to the group are completed. The combined status returned by this method is used as the result of the job group.

        The default implementation will return a MultiStatus object containing the returned statuses of the completed jobs. The results with IStatus.OK are omitted from the result since those statuses usually do not contain valuable information. Subclasses may override this method to implement custom status reporting, but should never return null.

        Overrides:
        computeGroupResult in class org.eclipse.core.internal.jobs.InternalJobGroup
        Parameters:
        jobResults - results of all the completed jobs belonging to this group.
        Returns:
        the combined status of the group, not null.
        See Also:
        getResult()