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;
28 isEmpty = tasks.isTodoListEmpty(); // flag > !keep assigning
32 currentWorkList[workMID] = tasks.grabTask(); /* grab the work from work pool */
37 chk = Work.checkCurrentWorkList(this);
41 if(chk == 1) { // still have work
44 tasks.execute(currentWorkList[workMID]);
45 /* push into done list */
46 tasks.done(currentWorkList[workMID]);
47 currentWorkList[workMID] = null;
51 cc = ((Drinker)tasks).ownTotal;
54 // System.out.println("CC = " + cc);
57 else if(chk == -1) { // finished all work
60 else { // wait for other thread
66 /* for debugging purpose */
68 System.out.println("\n\nDoneSize = " + tasks.doneList.size());
70 System.out.println("\n\n\n I'm done");
74 public static int checkCurrentWorkList(Work mywork) {
85 num_threads = mywork.NUM_THREADS;
88 for(i = 0 ; (i < num_threads) && (index < 0); i++) {
92 status = Thread.getStatus(i);
96 s = (Segment)mywork.currentWorkList[i];
98 if(status == -1 && null != s) {
99 mywork.currentWorkList[myID] = mywork.currentWorkList[i];
100 mywork.currentWorkList[i] = null;
110 if(index == 0) // grabbed dead machine's work
112 else if(i == num_threads && index < 0 && chk != true) // wait for other machine's work
115 return 0; // others are still working wait until they finish work