1 public class Work extends Thread {
3 Object[] currentWorkList;
7 Work (Task tasks, int num_threads, int mid, Object[] currentWorkList) {
9 this.currentWorkList = currentWorkList;
10 NUM_THREADS = num_threads;
20 System.out.println("Thread " + workMID + " has started");
29 isEmpty = tasks.isTodoListEmpty(); // flag > !keep assigning
32 currentWorkList[workMID] = tasks.grabTask(); /* grab the work from work pool */
36 chk = Work.checkCurrentWorkList(this);
40 if(chk == 1) { // still have work
42 tasks.setWork(currentWorkList[workMID]);
46 localTask.execution();
49 /* push into done list */
50 tasks.done(currentWorkList[workMID]);
51 currentWorkList[workMID] = null;
54 else if(chk == -1) { // finished all work
57 else { // wait for other thread
63 /* for debugging purpose */
67 System.out.println("\n\n\n I'm done");
71 public static int checkCurrentWorkList(Work mywork) {
82 num_threads = mywork.NUM_THREADS;
85 for(i = 0 ; (i < num_threads) && (index < 0); i++) {
89 status = Thread.getStatus(i);
93 s = mywork.currentWorkList[i];
95 if(status == -1 && null != s) {
96 mywork.currentWorkList[myID] = mywork.currentWorkList[i];
97 mywork.currentWorkList[i] = null;
107 if(index == 0) // grabbed dead machine's work
109 else if(i == num_threads && index < 0 && chk != true) // wait for other machine's work
112 return 0; // others are still working wait until they finish work