Search code examples
javaeclipse-rcpjobsjob-scheduling

How can i set order for Eclipse Job execution?


I have 2 methods contain selft jobs, for example

protected void method1(){
 String name=getName();//return name based on combobox selection
 Job job= new Job("first job"){
            @Override
            protected IStatus run(IProgressMonitor monitor) {
            someActions();
      }
   }
}

protected void method2(){
 String name=getName();//return name based on combobox selection
 Job job= new Job("second job"){
            @Override
            protected IStatus run(IProgressMonitor monitor) {
            someActions();
      }
   }
}

method1 and method2 can invoked seperated.With own progress bar

I have also some button to invoke both methods

 btnUpdate.addListener(SWT.MouseUp, new Listener() {

            @Override
            public void handleEvent(Event event) {
                         method1();
                         method2();
                    }
     }

but this construction works incorrect becouse job from method2() start execute early then job from method1() finished. Also if i mark job from method1() as join then method2() will not start till the method1() execute, but progress bar doesn't displayed

How to make it work correctly?)


Solution

  • Prevent the 2 jobs from running simultanously by setting a job-rule:

    public class Mutex implements ISchedulingRule {
    
            public boolean contains(ISchedulingRule rule) {
                return (rule == this);
            }
    
            public boolean isConflicting(ISchedulingRule rule) {
                return (rule == this);
            }
    
    }// Mutex
    
    public static final ISchedulingRule mutex = new Mutex();
    

    for both of your jobs:

    job.setRule(<your mutex instance>);
    

    Make sure that both jobs use the same mutex-instance!

    This should:

    1. Prevent 2 jobs from running at the same time (if they both make use of the same rule)
    2. Execute them in the order you schedule them

    Btw.: You are not scheduling your jobs using job.schedule(), so actually they should not even be executed using the code you posted...