?? mapper.java
字號:
public class Mapper{ CubbyHole [] storage; // data parallel "machine" "pvar" int inArrayLength; // length of input array public BarrierSynch waiting; // object for barrier synchronisation. MapThread nthread; // temp var for starting threads ApplyObjUnary applic; // the function used as a parameter Object [] outArray; // for the result of the map int i; /* ========================================================== Main Method.. initialize; start up threads for DP step ========================================================== */ public Object [] Map(ApplyObjUnary applic,Object[] inArray){ inArrayLength = inArray.length; this.applic = applic; storage = new CubbyHole[inArrayLength]; initializeStorage(inArray);/* DATA PARALLEL STEPS -- make sure code for all required steps is complete*//* this code is for one step -- edit if you need more */ waiting = new BarrierSynch(inArrayLength+1 ); startThreads(); waiting.iveArrived(999); System.out.println("************ Map step ends "); outArray = new Object[inArrayLength]; for (i=0; i< inArrayLength; i++) { outArray[i] = (Object) storage[i].get(); System.out.println( "^^" + outArray[i]); } // return the actual array for map ops return outArray; } /* ========================================================== Fill up CubbyHoles with data from inArray ========================================================== */ private void initializeStorage(Object[] inArray){ int i; for (i=0;i< inArrayLength;i++){ storage[i] = new CubbyHole(); (storage[i]).put(inArray[i]); } } /* ============================================================== Start up threads with a pointer to this object plus a thread number. ============================================================== */ private void startThreads(){ int i = 0; for (i=0;i < inArrayLength; i++){ nthread = new MapThread(this,i); nthread.start(); } }}class MapThread extends Thread{ int myId; BarrierSynch myBarrier; CubbyHole myCubby; Mapper myParent; ApplyObjUnary myApplic; MapThread(Mapper parent,int id){ myId = id; myBarrier = parent.waiting; myCubby = parent.storage[id]; myParent = parent; myApplic = parent.applic; } /* ================================================================== The run method is invoked by running start on each thread. You must complete the code to be run by each thread. ================================================================== */ public void run(){ Object tmp1; // object holder. /* ================================================================= Get value from my cubby hole apply the mapped function to it put the value back in the cubby hole ================================================================= */ System.out.println("Hello world from map thread: " + myId); // <insert code here> myCubby.put(myApplic.f(myCubby.get())); myBarrier.iveArrived (myId); }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -