2012-11-06 20:12:32 -05:00
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
2012-09-28 18:07:28 -04:00
|
|
|
public class jobThread extends Thread {
|
|
|
|
private final int sleepResolution = 200; //Milliseconds
|
|
|
|
private long jobTime; //Milliseconds
|
|
|
|
private long elapsedTime;
|
|
|
|
private boolean isPaused, pauseStateChanged;
|
|
|
|
private long startTime;
|
|
|
|
private int jobID;
|
|
|
|
private boolean jobDone;
|
2012-11-06 13:33:20 -05:00
|
|
|
private Method parentAlgorithmDeallocate; //Our parent to notify when we're done.
|
2012-09-28 18:07:28 -04:00
|
|
|
|
2012-11-06 20:12:32 -05:00
|
|
|
/* Fields that we need to know when calling the deallocate */
|
|
|
|
private int jobSize;
|
|
|
|
private int beginningLocation;
|
|
|
|
|
|
|
|
public jobThread(long jobTime, int jobID, Method parentAlgorithmDeallocate, int jobSize, int beginningLocation){
|
2012-09-28 18:07:28 -04:00
|
|
|
this.jobTime = jobTime;
|
2012-11-06 13:33:20 -05:00
|
|
|
this.parentAlgorithmDeallocate = parentAlgorithmDeallocate;
|
2012-11-06 20:12:32 -05:00
|
|
|
this.elapsedTime = 0;
|
|
|
|
this.isPaused = false;
|
|
|
|
this.startTime = 0;
|
2012-09-28 18:07:28 -04:00
|
|
|
this.jobID = jobID;
|
2012-11-06 20:12:32 -05:00
|
|
|
this.jobSize = jobSize;
|
|
|
|
this.beginningLocation = beginningLocation;
|
2012-09-28 18:07:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void pause(){
|
|
|
|
synchronized(this){
|
|
|
|
isPaused = true;
|
|
|
|
pauseStateChanged = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void jobResume(){
|
|
|
|
synchronized(this){
|
|
|
|
isPaused = false;
|
|
|
|
pauseStateChanged = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void run(){
|
|
|
|
//The event loop.
|
|
|
|
//Basically, check that elapsedTime plus current time delta
|
|
|
|
//are not greater than the time we're supposed to run.
|
|
|
|
//If paused, don't do anything until we resume
|
|
|
|
try{
|
|
|
|
while (!jobDone){
|
|
|
|
//Begin event logic
|
|
|
|
sleep(sleepResolution);
|
|
|
|
|
|
|
|
synchronized(this){
|
|
|
|
if (pauseStateChanged){
|
|
|
|
if (isPaused){
|
|
|
|
//We have just been paused, save the time that we've currently
|
|
|
|
//been running, and then go back to the event loop
|
|
|
|
elapsedTime += System.currentTimeMillis() - startTime;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
//We have just been resumed, restart the timer
|
|
|
|
startTime = System.currentTimeMillis();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (isPaused){
|
|
|
|
//Nothing much happening, we're still paused.
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
//Not paused, Check if we need to keep running
|
|
|
|
long currentDelta = System.currentTimeMillis() - startTime;
|
|
|
|
if (currentDelta + elapsedTime >= jobTime){
|
|
|
|
jobDone = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//We're done, go ahead and notify our algorithm to clean us up
|
2012-11-06 20:12:32 -05:00
|
|
|
parentAlgorithmDeallocate.invoke(this.jobSize, this.beginningLocation);
|
2012-09-28 18:07:28 -04:00
|
|
|
} catch (Exception e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|