⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mapper.java

📁 this code is a parallel programming code . such code is esay but is very useful to understand how to
💻 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);  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -