UIJob是Job的子类,对Job进行简单的封装,使得方法都在asyncDisplay.asyncExec() 下运行
以下为UIJob的源码,关键在run方法中的
asyncDisplay.asyncExec(new Runnable() {...}
public abstract classUIJob extends Job { private Display cachedDisplay; public UIJob(String name) { super(name); } public UIJob(Display jobDisplay, String name) { this(name); setDisplay(jobDisplay); } public static IStatus errorStatus(Throwable exception) { return WorkbenchPlugin.getStatus(exception); } public final IStatus run(final IProgressMonitor monitor) { if (monitor.isCanceled()) { return Status.CANCEL_STATUS; } Display asyncDisplay = getDisplay(); if (asyncDisplay == null || asyncDisplay.isDisposed()) { return Status.CANCEL_STATUS; } asyncDisplay.asyncExec(new Runnable() { public void run() { IStatus result = null; Throwable throwable = null; try { //As we are in the UI Thread we can //always know what to tell the job. setThread(Thread.currentThread()); if (monitor.isCanceled()) { result = Status.CANCEL_STATUS; } else { UIStats.start(UIStats.UI_JOB, getName()); result = runInUIThread(monitor); } } catch(Throwable t){ throwable = t; } finally { UIStats.end(UIStats.UI_JOB, UIJob.this, getName()); if (result == null) { result = new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, IStatus.ERROR, ProgressMessages.InternalError, throwable); } done(result); } } }); return Job.ASYNC_FINISH; } public abstract IStatus runInUIThread(IProgressMonitor monitor); public void setDisplay(Display runDisplay) { Assert.isNotNull(runDisplay); cachedDisplay = runDisplay; } public Display getDisplay() { //If it was not set get it from the workbench if (cachedDisplay == null && PlatformUI.isWorkbenchRunning()) { return PlatformUI.getWorkbench().getDisplay(); } return cachedDisplay; }}