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
62 /* for debugging purpose */
66 System.out.println("\n\n I'm done\n\n\n");
68 RecoveryStat.printRecoveryStat();
75 public static int checkCurrentWorkList(Work mywork) {
86 num_threads = mywork.NUM_THREADS;
89 for(i = 0 ; (i < num_threads) && (index < 0); i++) {
93 status = Thread.getStatus(i);
97 s = mywork.currentWorkList[i];
99 if(status == -1 && null != s) {
100 mywork.currentWorkList[myID] = mywork.currentWorkList[i];
101 mywork.currentWorkList[i] = null;
111 if(index == 0) // grabbed dead machine's work
113 else if(i == num_threads && index < 0 && chk != true) // wait for other machine's work
116 return 0; // others are still working wait until they finish work
119 public static native void printRecoveryStat();