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;
22 System.out.println("Thread " + workMID + " has started");
23 st = System.currentTimeMillis();
33 isEmpty = tasks.isTodoListEmpty(); // flag > !keep assigning
36 currentWorkList[workMID] = tasks.grabTask(); /* grab the work from work pool */
40 chk = Work.checkCurrentWorkList(this);
44 if(chk == 1) { // still have work
46 tasks.setWork(currentWorkList[workMID]);
50 localTask.execution();
53 /* push into done list */
54 tasks.done(currentWorkList[workMID]);
55 currentWorkList[workMID] = null;
58 else if(chk == -1) { // finished all work
61 else { // wait for other thread
67 fi = System.currentTimeMillis();
69 /* for debugging purpose */
73 System.out.println("\n\n I'm done\n\n\n");
74 System.out.println("Time Elapse = " + (double)((fi-st)/1000));
76 RecoveryStat.printRecoveryStat();
83 public static int checkCurrentWorkList(Work mywork) {
94 num_threads = mywork.NUM_THREADS;
97 for(i = 0 ; (i < num_threads) && (index < 0); i++) {
102 status = Thread.getStatus(i);
106 s = mywork.currentWorkList[i];
108 if(status == -1 && null != s) {
109 mywork.currentWorkList[myID] = mywork.currentWorkList[i];
110 mywork.currentWorkList[i] = null;
119 if(index == 0) // grabbed dead machine's work
121 else if(i == num_threads && index < 0 && chk != true) // wait for other machine's work
124 return 0; // others are still working wait until they finish work
127 public static native void printRecoveryStat();