Guest User

Untitled

a guest
May 31st, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 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.     // do we want to know when each thread starts and stops
  9.     public boolean quiet;
  10.     // which thread this is // an index
  11.     public int ThreadNumber;
  12.     // when this thread started and how long it took to finish
  13.     long starttime, calcTime;
  14.     // reference to the actual thread, necessary for joining/waiting
  15.     public Thread thisThread;
  16.     // array of results from all the processes, will be summed in the main
  17.     // thread
  18.     public static double results[];
  19.  
  20.     // constructor
  21.     public ThreadedMatrix(boolean _q, int _tn) {
  22.         quiet = _q;
  23.         ThreadNumber = _tn;
  24.     }
  25.  
  26.     // push a matrix into the que and tell it that it's being calculated by this
  27.     // thread
  28.     public void Push(Matrix m) {
  29.         m.thread = this;
  30.         que.add(m);
  31.     }
  32.  
  33.     // remove a matrix
  34.     public void Pop(Matrix m) {
  35.         que.remove(m);
  36.     }
  37.  
  38.     // remove the top matrix. never actually used but added for the heck of it
  39.     public void Pop() {
  40.         que.remove(que.size() - 1);
  41.     }
  42.  
  43.     // top matrix in the que
  44.     public Matrix Top() {
  45.         return que.get(que.size() - 1);
  46.     }
  47.  
  48.     // if it's finished, the thread loop will stop
  49.     public boolean finished = false;
  50.  
  51.     // called when the thread starts
  52.     @Override
  53.     public void run(){
  54.         // report when the thread started
  55.         if (!quiet) {
  56.             starttime = System.nanoTime();
  57.             System.out.println("Thread " + ThreadNumber + " started");
  58.         }
  59.         // every thread is running until every single thread has no more
  60.         // matrices in their ques
  61.         while (!finished) {
  62.             if (que.size() > 0) {
  63.                 Matrix _t = Top();
  64.                 _t.run();
  65.                 if (!quiet) {
  66.                     System.out.println(
  67.                             "working in thread " + ThreadNumber + ", processes left: " + Matrix.numberOfProcesses
  68.                                     + "/" + ThreadedMatrix.results.length + ", in que: " + que.size());
  69.                 }
  70.             } else {
  71.                 try {
  72.                     synchronized (thisThread) {
  73.                         thisThread.wait();
  74.                     }
  75.                 } catch (InterruptedException e) {
  76.                     e.printStackTrace();
  77.                 }
  78.             }
  79.         }
  80.         // report when the thread finished
  81.         if (!quiet) {
  82.             calcTime = System.nanoTime() - starttime;
  83.             System.out.println("Thread " + ThreadNumber + " stopped\nExecution time was " + calcTime);
  84.  
  85.         }
  86.     }
  87.  
  88. }
Add Comment
Please, Sign In to add comment