eclipse插件开发:总结一下eclipse中的Job机制

总结一下eclipse中Job,Job可以说是eclipse中比较核心又在我们使用中经常碰到的一种机制。本质上是eclipse中的多线程的一种表现,与IRunnable功能类似。但是更加对象化,容易扩展和控制,属于eclipseUI的一部分。表现上的区别:Job会在右下角显示进度条,IRunnable则不会(只有靠MonitorDialog),更加的友好。可以是在前台显示,也可以不显示进度条setSystem(true);。我们最常见到的要属java的自动编译了。

Eclipse为Job提供了3个扩展:WorkspaceJob,UIJob,WorkbenchJob.算上Job本身构成了eclipse对多线程的支持。
WorkspaceJob是为修改资源文件增加的扩展,常见的对文件的打开,保存,等等操作一般需要在这个类中执行。与WorkspaceJob对应的是IWorkspaceRunnable。


UIJob是在 UI线程上干活,所以大家使用时就要注意效率了。因为这个UIJob运行时UI是不刷新的看上去可能像死掉一样。所以数据的处理(长时间运行的程序)需要使用Job基类在UI线程外部运行,SWT界面的刷新,需要在UIJob中。对应的是

Java代码:
display.asyncExec (new Runnable () {public
void run () {});

SWT对于所有来自非 UI 线程的调用,将触发SWTException。这些调用必须来自 UI 线程。

WorkbenchJob是UIJob的扩展,也是在UI线程上干活,目的是仅当工作台正在运行时才能调度或运行作业。


介绍一下Job的优先级:

• INTERACTIVE 作业通常优先于其他作业。它们应该是短时间运行或者较少使用处理器的作业,因此,它们不会阻碍其他 INTERACTIVE 作业运行。

• SHORT 作业通常会在一秒钟内完成,但是也可能会时间稍微长一点。它们在后台运行,并且优先于除了 INTERACTIVE 作业之外的所有作业。

• LONG 作业表示更长时间运行的后台作业。它们只有在 INTERACTIVE 作业和 SHORT 作业已经运行之后才运行。

• BUILD 作业表示与构建任务相关联的作业。它们比 LONG 作业的优先级更低。BUILD 作业只有在所有 LONG 作业完成之后才运行。

• DECORATE 作业在系统中的优先级最低。它们用于那些提供可以帮助补充 UI 的信息但是用户通常不会等待的任务。

默认的优先级是LONG。job.setPriority(Job.DECORATE);

做一个小例子:

Java代码:
class MYJob extends Job {
public MYJob() {
super("MY Job");
}

public IStatus run(IProgressMonitor monitor) {
monitor.beginTask("MYJob", 60000);
for (int i = 0; i < 60000; i++) {
System.out.println("This is a MYJob");
monitor.worked(1);
}
monitor.done();
return Status.OK_STATUS;
}
}
Job job = new MYJob();
job.schedule();


schedule方法就是将当前job加入到等候线程。至于什么时候运行我们就无法决定了。

与java线程一样Job也提供join()方法,阻断当前调用者,直到job运行完了或取消。常见用法:if (!job.cancel()) job.join();取消不成则死等。


我们可以给job加一个监听器:IJobChangeListener,可以根据job的各种状态变化获得响应。Job的状态:

• aboutToRun 是在作业将要运行时发送的。

• awake 是在先前处于休眠状态的作业现在正在等待运行时发送的。

• done 是在作业完成执行时发送的。

• running 是在作业开始运行时发送的。

• scheduled 是在作业已被调度并正在作业队列中等待时发送的。

• sleeping 是在将正在等待的作业置于休眠状态时发送的。


另外Job提供一个getState()方法,但这个方法的结果不可靠,仅仅取得的是相对当前线程的相对结果。不建议使用。


Eclipse也提供了一个全局的Job管理器:IJobManager。

使用Platform.getJobManager();和job.belongsTo()方法配合获得需要的job。


再说几个方法:job.setSystem(true),简单的说右下角的图标没了。工作可以偷偷的进行了。

Job.setUser(true),会弹出一个对话框,让我们选是隐藏、取消、详细。大家的svn/cvs同步就是这个了。但是这两个方法要在schedule()前运行。

可以把多个Job混为一组:


Java代码:
IJobManager jobMan = Platform.getJobManager();
group = jobMan.createProgressGroup();job.setProgressGroup(group, 600);
// 要在schedule()前运行


适当利用job可以提高界面的友好性,提高应用程序的整体性能


最后,放上两段测试代码。

Java代码:
package view.views;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.IJobManager;
import org.eclipse.core.runtime.jobs.Job;

public
class JobTester {
public
static
void run() {
IJobManager jobMan = Platform.getJobManager();
IProgressMonitor myGroup = jobMan.createProgressGroup();
myGroup.beginTask("aaaaaaa", 180000); // needs to show.job.schedule()
Job job = new MYJob();

job.setProgressGroup(myGroup, 60000); // specify the units of work the
// job
job.schedule();
// ...
// try {
// job.join();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
job = new MYJob2();
job.setProgressGroup(myGroup, 60000); // specify the units of work the
// job needs to
// show.job.schedule() ...
job.schedule();
// try {
// job.join();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
job = new MYJob3();

job.setProgressGroup(myGroup, 60000); // specify the units of work
// the job needs to
// show.job.schedule() ...
job.schedule();
// try {
// job.join();
// } catch (InterruptedException e) {
//
// e.printStackTrace();
// }
// myGroup.done();
}

static
class MYJob extends Job {
public MYJob() {
super("MY Job");
// this.setUser(true);
// this.setSystem(true);
}

public IStatus run(IProgressMonitor monitor) {
monitor.beginTask("MYJob", 60000);
for (int i = 0; i < 60000; i++) {
System.out.println("This is a MYJob");
monitor.worked(1);
}
monitor.done();
return Status.OK_STATUS;
}
}

static
class MYJob2 extends Job {
public MYJob2() {
super("MY Job2");
this.setUser(true);
}

public IStatus run(IProgressMonitor monitor) {
monitor.beginTask("MYJob2", 60000);
for (int i = 0; i < 60000; i++) {
System.out.println("This is a MYJob2");
monitor.worked(1);
}
monitor.done();
return Status.OK_STATUS;
}
}

static
class MYJob3 extends Job {
public MYJob3() {
super("MY Job3");
// this.setUser(true);
}

public IStatus run(IProgressMonitor monitor) {
monitor.beginTask("MYJob3", 60000);
for (int i = 0; i < 60000; i++) {
System.out.println("This is a MYJob3");
monitor.worked(1);
}
monitor.done();
return Status.OK_STATUS;
}
}

}

Java代码:
package view.views;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.IProgressConstants;
import org.eclipse.ui.progress.IProgressService;
import org.eclipse.ui.progress.IWorkbenchSiteProgressService;

import view.Activator;

public
class ProgressServiceTester {
public
static
void run(IWorkbenchPartSite site) {
IProgressService progressService = PlatformUI.getWorkbench()
.getProgressService();
IWorkbenchSiteProgressService siteService = (IWorkbenchSiteProgressService) site
.getAdapter(IWorkbenchSiteProgressService.class);
// progressService.registerIconForFamily(PlatformUI.getWorkbench().getSharedImages().
// getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK), "MY Job");
// siteService.registerIconForFamily(Activator.getImageDescriptor("/icons/sample.gif"),
// "MY Job");
siteService.schedule(new MYJob(), 0
/* now */, true
/*
* use the half-busy
* cursor in the
* part
*/);

}

public
static
void run() {
IProgressService service = PlatformUI.getWorkbench()
.getProgressService();
try {
runInUI();
} catch (InvocationTargetException e) {

e.printStackTrace();
} catch (InterruptedException e) {

e.printStackTrace();
}
}

static
void busyCursorWhile() throws InvocationTargetException,
InterruptedException {
IProgressService progressService = PlatformUI.getWorkbench()
.getProgressService();
progressService.busyCursorWhile(new IRunnableWithProgress() {
public
void run(IProgressMonitor monitor) {
// do non-UI work
for (int i = 0; i < 600000; i++)
System.out.println("This is a MYJob");
}
});
}

static
void runInUI() throws InvocationTargetException,
InterruptedException {
IProgressService progressService = PlatformUI.getWorkbench()
.getProgressService();
progressService.runInUI(PlatformUI.getWorkbench().getProgressService(),
new IRunnableWithProgress() {
public
void run(IProgressMonitor monitor) {
// do UI work
for (int i = 0; i < 100000; i++)
System.out.println("This is a MYJob");
}
}, null);

}

static
class MYJob extends Job {
public MYJob() {
super("MY Job");
// this.setUser(true);
// this.setSystem(true);
}

public IStatus run(IProgressMonitor monitor) {
monitor.beginTask("MYJob", 600000);
for (int i = 0; i < 600000; i++) {
System.out.println("This is a MYJob");
monitor.worked(1);
}
monitor.done();
return Status.OK_STATUS;
}

@Override
public
boolean belongsTo(Object family) {
return
"MY Job".equals(family);
}
}

public
static
void doPropertyTest() {
Job job = new Job("Do Work") {
public IStatus run(IProgressMonitor monitor) {
// do some work.
// Keep the finished job in the progress view only if it is not
// running in the progress dialog

// Boolean inDialog = (Boolean)
// getProperty(IProgressConstants.PROPERTY_IN_DIALOG);
// if (inDialog==null||!inDialog.booleanValue())
// setProperty(IProgressConstants.PROPERTY_IN_DIALOG,
// Boolean.TRUE);
// setProperty(IProgressConstants.KEEPONE_PROPERTY,
// Boolean.TRUE);
setProperty(IProgressConstants.KEEP_PROPERTY, Boolean.TRUE);
// System.out.println(this.getProperty(IProgressConstants.NO_IMMEDIATE_ERROR_PROMPT_PROPERTY));
for (int i = 0; i < 6; i++)
System.out.println("waiting");
return Status.OK_STATUS;
}
};
job.setProperty(IProgressConstants.ICON_PROPERTY, Activator
.getImageDescriptor("/icons/sample.gif"));
IAction gotoAction = new Action("Results") {
public
void run() {
// show the results
for (int i = 0; i < 6000; i++)
System.out.println("gotoAction");
}
};
job.setProperty(IProgressConstants.ACTION_PROPERTY, gotoAction);
job.setUser(true);
job.schedule();
}

}

原文地址:https://www.cnblogs.com/eclipsetech/p/8627015.html