Friday, April 19, 2013

Controlling number of parallel jobs based on number of available processors for JVM



//No. of processors available for JVM
private static final int NO_OF_PROCESSORS = Runtime.getRuntime().availableProcessors();
private static final String JS_FAMILY = "js_typechecking_family"; //$NON-NLS-1$


Override belongsTo method in Job.
@Override

public boolean belongsTo(Object family) {
return family == JS_FAMILY;
}


Execution call:


try {
monitor.beginTask("Project: " + project.getName(), jsModules.size());

//Create number of parallel jobs based on the number of processors available to the Java virtual machine.
IJobManager jobManager = Job.getJobManager();
for (IFile iFile : jsModules) {
Job[] find = jobManager.find(JS_FAMILY);
if (find.length == NO_OF_PROCESSORS) {
while (true) {
try {
Thread.sleep(100);//sleep for some time buddy!!
} catch (InterruptedException e) {
//nothing
}
Job[] queueJobs = jobManager.find(JS_FAMILY);
if (queueJobs.length < NO_OF_PROCESSORS) {
break;
}
}
}

if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
monitor.setTaskName("Identifying xxx: " + iFile.getName());
try {
startExecution(...);//args here
} catch (Exception e) {
EditorPlugin.logError(e);
}
monitor.worked(1);
}

} finally {
monitor.done();
}






No comments:

Post a Comment