views:

76

answers:

2

I have two UI threads. I want to make sure the first one is finishing running, and then run the other one. How could I do that? Thanks a lot!

UIJob uiJob = new UIJob("settext1") {
    public IStatus runInUIThread(IProgressMonitor ipm) {
        webBrowser.setText(finalContent);
        return Status.OK_STATUS;
    }
};
uiJob.schedule();

UIJob uiJob2 = new UIJob("settext2") {
    public IStatus runInUIThread(IProgressMonitor ipm) {
        webBrowser.execute(executeMoreFunction);
        return Status.OK_STATUS;
    }
};
uiJob2.schedule();
+1  A: 

From eclipse sdk help:

The UIJob is a Job that runs within the UI Thread via an asyncExec.

It is only one UI thread, two UIJob:s cannot wait for eachother, it will create a deadlock.

dacwe
A: 

Call the join method after schedule.

Waits until this job is finished. This method will block the calling thread until the job has finished executing, or until this thread has been interrupted. If the job has not been scheduled, this method returns immediately. A job must not be joined from within the scope of its run method. If this method is called on a job that reschedules itself from within the run method, the join will return at the end of the first execution. In other words, join will return the first time this job exits the RUNNING state, or as soon as this job enters the NONE state. If this method is called while the job manager is suspended, this job will only be joined if it is already running; if this job is waiting or sleeping, this method returns immediately. Note that there is a deadlock risk when using join. If the calling thread owns a lock or object monitor that the joined thread is waiting for, deadlock will occur.

Read more: http://kickjava.com/src/org/eclipse/core/runtime/jobs/Job.java.htm#ixzz0qWh6Ahhe

Pedro Silva
Can I use this then? private ISchedulingRule schedulingRule = new ISchedulingRule() { public boolean contains(ISchedulingRule rule) { return (rule == this); } public boolean isConflicting(ISchedulingRule rule) { return (rule == this); } };Then set the rule to the each job before scheduling: job1.setRule(schedulingRule); job1.schedule(); job2.setRule(schedulingRule); job2.schedule();
Java Doe