mirror of
https://github.com/bspeice/itcs3146-project
synced 2024-11-09 09:38:23 -05:00
Add code for the Job itself
This commit is contained in:
parent
470e720197
commit
a1be9111fa
59
Job.java
59
Job.java
@ -1,22 +1,37 @@
|
|||||||
/*Job Class
|
/*
|
||||||
* Contains Int Size and Int Time
|
* Author: Bradlee Speice
|
||||||
*/
|
* Job Class
|
||||||
|
*
|
||||||
public class Job {
|
* The purpose of this class is to simulate a running "Job" in memory. To create a new Job, call the
|
||||||
private int size,time;
|
* "start" method, and inform the job of how long it should run, and give it an identifier.
|
||||||
|
* The job will use this to report back that it is done. If you need to pause the job, you
|
||||||
public Job(int s, int t){
|
* can do so. Finally, the job will report back to whoever called it that it needs to be destroyed.
|
||||||
size = s;
|
* If there are any questions, email bspeice@uncc.edu
|
||||||
time = t;
|
*/
|
||||||
}
|
public class Job {
|
||||||
|
private jobThread myThread; //Reference to the thread we control
|
||||||
public void setSize(int s){size = s;}
|
private boolean myThreadPaused; //Used to keep track of the execution state of our thread
|
||||||
public void setTime(int t){time = t;}
|
//private BaseAlgorithm parentAlgorithm; //Used for knowing who to tell that we're done.
|
||||||
|
|
||||||
public int getSize(){return size;}
|
public Job(int jobTime, int jobID/*, BaseAlgorithm parentAlgorithm */){
|
||||||
public int getTime(){return time;}
|
//Create a new job, and start it running
|
||||||
|
myThread = new jobThread(jobTime, jobID /*, parentAlgorithm*/);
|
||||||
public String toString(){
|
myThread.start();
|
||||||
return "("+size+","+time+")";
|
}
|
||||||
}
|
|
||||||
}
|
public void pause(){
|
||||||
|
if (!myThreadPaused){
|
||||||
|
myThread.pause();
|
||||||
|
myThreadPaused = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void resume(){
|
||||||
|
if (myThreadPaused){
|
||||||
|
myThread.jobResume();
|
||||||
|
myThreadPaused = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
80
jobThread.java
Normal file
80
jobThread.java
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
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;
|
||||||
|
/* private BaseAlgorithm parentAlgorithm *///Our parent to notify when we're done.
|
||||||
|
|
||||||
|
public jobThread(long jobTime, int jobID /*, BaseAlgorithm parentAlgorithm*/){
|
||||||
|
this.jobTime = jobTime;
|
||||||
|
elapsedTime = 0;
|
||||||
|
isPaused = false;
|
||||||
|
startTime = 0;
|
||||||
|
this.jobID = jobID;
|
||||||
|
//this.parentAlgorithm = parentAlgorithm;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
/* parentAlgorithm.deallocate(jobID); */
|
||||||
|
} catch (Exception e) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user