Guest User

Untitled

a guest
May 30th, 2017
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 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.     //constructor
  19.     public ThreadedMatrix(boolean _q, int _tn) {
  20.         quiet = _q;
  21.         ThreadNumber = _tn;
  22.     }
  23.     //push a matrix into the que and tell it that it's being calculated by this thread
  24.     public void Push(Matrix m) {
  25.         que.add(m);
  26.         m.thread = this;
  27.     }
  28.     //remove a matrix
  29.     public void Pop(Matrix m) {
  30.         que.remove(m);
  31.     }
  32.     //remove the top matrix. never actually used but added for the heck of it
  33.     public void Pop() {
  34.         que.remove(que.size() - 1);
  35.     }
  36.     //top matrix in the que
  37.     public Matrix Top() {
  38.         return que.get(que.size() - 1);
  39.     }
  40.     //called when the thread starts
  41.     @Override
  42.     public void run() {
  43.         //report when the thread started
  44.         if (!quiet) {
  45.             starttime = System.nanoTime();
  46.             System.out.println("Thread " + ThreadNumber + " started");
  47.         }
  48.         //every thread is running until every single thread has no more matrices in their ques
  49.         while (true) {
  50.             if (que.size() > 0) {
  51.                 Matrix _t = Top();
  52.                 result += _t.Determinant(quiet) * _t.valueInParent;
  53.             }
  54.             //check if every thread has no more matrices in it's que
  55.             boolean finished = true;
  56.             for (ThreadedMatrix thread : Matrix.threadedMatrices) {
  57.                 if (thread.que.size() > 0) {
  58.                     finished = false;
  59.                 }
  60.             }
  61.             //stop the threads
  62.             if (finished) {
  63.                 break;
  64.             }
  65.         }
  66.         //report when the thread finished
  67.         if (!quiet) {
  68.             calcTime = System.nanoTime() - starttime;
  69.             System.out.println("Thread " + ThreadNumber + " stopped\nExecution time was " + calcTime);
  70.         }
  71.     }
  72.  
  73. }
Add Comment
Please, Sign In to add comment