Guest User

Untitled

a guest
May 31st, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 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.     // total result of all the threads
  9.     public static double result;
  10.     // do we want to know when each thread starts and stops
  11.     public boolean quiet;
  12.     // which thread this is // an index
  13.     public int ThreadNumber;
  14.     // when this thread started and how long it took to finish
  15.     long starttime, calcTime;
  16.     // reference to the actual thread, necessary for joining/waiting
  17.     public Thread thisThread;
  18.     //array of results from all the processes, will be summed in the main thread
  19.     public static double results[];
  20.  
  21.     // constructor
  22.     public ThreadedMatrix(boolean _q, int _tn) {
  23.         quiet = _q;
  24.         ThreadNumber = _tn;
  25.     }
  26.  
  27.     // push a matrix into the que and tell it that it's being calculated by this
  28.     // thread
  29.     public void Push(Matrix m) {
  30.         m.thread = this;
  31.         que.add(m);
  32.     }
  33.  
  34.     // remove a matrix
  35.     public void Pop(Matrix m) {
  36.         que.remove(m);
  37.     }
  38.  
  39.     // remove the top matrix. never actually used but added for the heck of it
  40.     public void Pop() {
  41.         que.remove(que.size() - 1);
  42.     }
  43.  
  44.     // top matrix in the que
  45.     public Matrix Top() {
  46.         return que.get(que.size() - 1);
  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+", in que: "+que.size());
  68.             }
  69.  
  70.         }
  71.         // report when the thread finished
  72.  
  73.         if (!quiet) {
  74.             calcTime = System.nanoTime() - starttime;
  75.             System.out.println("Thread " + ThreadNumber + " stopped\nExecution time was " + calcTime);
  76.  
  77.         }
  78.     }
  79.  
  80. }
Add Comment
Please, Sign In to add comment