Guest User

Untitled

a guest
Jun 3rd, 2017
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. package project;
  2.  
  3. import java.util.Vector;
  4.  
  5. public class ThreadedMatrix implements Runnable {
  6.     // que of matrices that need to be calculated
  7.     public Vector<Matrix> que = new Vector<Matrix>();
  8.     // which thread this is // an index
  9.     public int ThreadNumber;
  10.     // when this thread started and how long it took to finish
  11.     long starttime, calcTime;
  12.     // reference to the actual thread, necessary for joining/waiting
  13.     public Thread thisThread;
  14.     // array of results from all the processes, will be summed in the main
  15.     // thread
  16.     public static double results[];
  17.  
  18.     // constructor
  19.     public ThreadedMatrix(int _tn) {
  20.         ThreadNumber = _tn;
  21.     }
  22.  
  23.     // push a matrix top of the que and tell it that it's being calculated by
  24.     // this
  25.     // thread
  26.     public void Push(Matrix m) {
  27.         m.thread = this;
  28.         que.add(m);
  29.     }
  30.  
  31.     // remove a matrix
  32.     public Matrix Pop(Matrix m) {
  33.         que.remove(m);
  34.         return m;
  35.     }
  36.  
  37.     // remove the bottom matrix
  38.     public Matrix Pop() {
  39.         Matrix m = Bottom();
  40.         return Pop(m);
  41.     }
  42.  
  43.     // top matrix in the que
  44.     public Matrix Top() {
  45.         return que.get(que.size() - 1);
  46.     }
  47.  
  48.     // bottom matrix in the que
  49.     public Matrix Bottom() {
  50.         return que.get(0);
  51.     }
  52.  
  53.     // if it's finished, the thread loop will stop
  54.     public boolean finished = false;
  55.     public boolean paused = false;
  56.  
  57.     // called when the thread starts
  58.     @Override
  59.     public void run() {
  60.         // report when the thread started
  61.         if (!test.quiet) {
  62.             starttime = System.nanoTime();
  63.             System.out.println("Thread " + ThreadNumber + " started");
  64.         }
  65.         // every thread is running until every single thread has no more
  66.         // matrices in their ques
  67.         while (!finished) {
  68.             // generate submatrices/calculate results
  69.             if (que.size() > 0) {
  70.                 Matrix _t = Bottom();
  71.                 _t.run();
  72.             } else {
  73.                 // or else wait until this thread has more than 0 elements in
  74.                 // the que
  75.                 try {
  76.                     synchronized (thisThread) {
  77.                         paused = true;
  78.                         thisThread.wait();
  79.                     }
  80.                 } catch (InterruptedException e) {
  81.                     e.printStackTrace();
  82.                 }
  83.             }
  84.         }
  85.         // report when the thread finished
  86.         if (!test.quiet) {
  87.             calcTime = System.nanoTime() - starttime;
  88.             System.out.println("Thread " + ThreadNumber + " stopped\nExecution time was " + calcTime + " ns");
  89.  
  90.         }
  91.     }
  92.  
  93. }
Add Comment
Please, Sign In to add comment