Eclipse >> Core >> Scheduling rule
public class FileLock implements ISchedulingRule {
private String path;
public FileLock(java.io.File file) {
this.path = file.getAbsolutePath();
}
//*** The Job can begin another rule if it is contained in the obtained rule
public boolean contains(ISchedulingRule rule) {
if (this == rule)
return true;
if (rule instanceof FileLock)
return ((FileLock)rule).path.startsWith(path);
if (rule instanceof MultiRule) {
MultiRule multi = (MultiRule) rule;
ISchedulingRule[] children = multi.getChildren();
for (int i = 0; i < children.length; i++)
if (!contains(children[i]))
return false;
return true;
}
return false;
}
//*** creating a simple scheduling rule that acts as a mutex (also known as a binary semaphore)
class Mutex implements ISchedulingRule {
//*** 2 Jobs cannot run concurrently if they hold the same rule
public boolean isConflicting(ISchedulingRule rule) {
return rule == this;
}
public boolean contains(ISchedulingRule rule) {
return rule == this;
}
}
//*** This rule is then added to the two light switch jobs
public class LightSwitch {
final MutextRule rule = new MutexRule();
...
class LightOn extends Job {
public LightOn() {
super("Turning on the light");
//*** The sqme rule is applied
setRule(rule);
}
...
}
class LightOff extends Job {
public LightOff() {
super("Turning off the light");
//*** The sqme rule is applied
setRule(rule);
}
...
}
}
>> Usage:
- Used by jobs to indicate when they need exclusive access to a resource.
- Applied synchronously to a thread using IJobManager.BEGINRULE(ISchedulingRule) and IJobManager.ENDRULE(ISchedulingRule). The job manager guarantees that no two jobs with conflicting scheduling rules will run concurrently.
Multiple rules can be applied to a given thread only if the outer rule explicitly allows the nesting as specified by the CONTAINS method.