如何告知Eclipse RCP作業管理最多同時運行x個作業?Eclipse RCP IJob:限制併發執行作業的數量
編輯:
它的工作原理是利用在回答中描述的自定義調度規則。
重要的是永遠不要將相同的規則引用傳遞給多個作業。
這裏是我使用的類:
公共類JobMaster {
private class Listener implements IJobChangeListener {
@Override
public void aboutToRun(final IJobChangeEvent event) {
}
@Override
public void awake(final IJobChangeEvent event) {
}
@Override
public void done(final IJobChangeEvent event) {
synchronized (JobMaster.this) {
running--;
System.err.println("now running " + running);
}
}
@Override
public void running(final IJobChangeEvent event) {
synchronized (JobMaster.this) {
running++;
System.err.println("now running " + running);
}
}
@Override
public void scheduled(final IJobChangeEvent event) {
}
@Override
public void sleeping(final IJobChangeEvent event) {
}
}
private class MyRule implements ISchedulingRule {
public MyRule() {
}
@Override
public boolean contains(final ISchedulingRule rule) {
if (rule == this) {
return true;
}
return false;
}
@Override
public boolean isConflicting(final ISchedulingRule rule) {
synchronized (JobMaster.this) {
if (rule == this) {
return true;
}
if (rule instanceof MyRule) {
return running >= maxRun;
}
return false;
}
}
}
private final Listener l = new Listener();
private final int maxRun;
private volatile int running = 0;
public JobMaster(final int maxRun) {
this.maxRun = maxRun;
}
public synchronized void add(final Job j) {
j.setRule(new MyRule());
j.addJobChangeListener(l);
j.schedule();
}
}
我已經嘗試過使用SchedulingRule。不幸的是,這是行不通的: java.lang.IllegalArgumentException: \t at org.eclipse.core.runtime.Assert.isLegal(Assert.java:63) \t at org.eclipse.core.runtime.Assert.isLegal(Assert .java:47) \t at org.eclipse.core.internal.jobs.JobManager.validateRule(JobManager.java:1573) Assert.isLegal(rule.isConflicting(rule)); – kerner1000
這是告訴你,你的規則的邏輯是不正確的。該規則必須是'反身' - 帶有相同規則的'isConflicting',因爲該參數必須返回true。添加了要求的答案 –