Guest User

TestingThreads

a guest
Dec 30th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1.  
  2. public class TestingMatrixThreads {
  3.  
  4.     public static void main(String[] arr) throws InterruptedException {
  5.  
  6.         int[][] a = new int[67][6];
  7.         // class.Count works with class.Matrix, that's why I've made it this way
  8.         Matrix m = new Matrix(a);
  9.         m.start();
  10.  
  11.         Thread.sleep(1000); // Here comes the BIG question -> how to avoid these
  12.                             // manually created pauses
  13.  
  14.         Count c;
  15.         Thread t;
  16.         // Creating new threads for each row of the matrix
  17.         for (int i = 0; i < Matrix.matr.length; i++) {
  18.             c = new Count(i);
  19.             t = new Thread(c);
  20.             t.start();
  21.  
  22.         }
  23.  
  24.         //Again - the same question
  25.         System.out.println("Main - Sleep!");
  26.         Thread.sleep(50);
  27.         System.out.println("\t\t\t\t\tMain - Alive!");
  28.  
  29.         int sum = 0;
  30.         for (int i = 0; i < Count.encounters.length; i++) {
  31.             System.out.println(i + "->" + Count.encounters[i]);
  32.             sum += Count.encounters[i];
  33.         }
  34.         System.out.println("Total numbers of digits: " + sum);
  35.  
  36.     }
  37.  
  38. }
  39.  
  40. class Count implements Runnable {
  41.  
  42.     int row;
  43.     public static int[] encounters = new int[10]; // here I store the number of each digit's(array's index) encounters
  44.  
  45.     public Count(int row) {
  46.         this.row = row;
  47.     }
  48.  
  49.     public synchronized static void increment(int number) {
  50.         encounters[number]++;
  51.     }
  52.  
  53.     @Override
  54.     public void run() {
  55.         System.out.println(Thread.currentThread().getName() + ", searching in row " + row + " STARTED");
  56.        
  57.         for (int col = 0; col < Matrix.matr[0].length; col++) {
  58.             increment(Matrix.matr[row][col]);
  59.         }
  60.        
  61.         try {
  62.             Thread.sleep(1); // If it's missing threads are starting and stopping consequently
  63.         } catch (InterruptedException e) {
  64.         }
  65.         System.out.println(Thread.currentThread().getName() + " stopped!");
  66.     }
  67.  
  68. }
  69.  
  70.  
  71. class Matrix extends Thread {
  72.  
  73.     static int[][] matr;
  74.  
  75.     public Matrix(int[][] matr) {
  76.         Matrix.matr = matr;
  77.     }
  78.  
  79.     @Override
  80.     public void run() {
  81.         //print();
  82.         fill();
  83.         System.out.println("matrix filled");
  84.         print();
  85.     }
  86.  
  87.     public static void fill() {
  88.         for (int i = 0; i < matr.length; i++) {
  89.             for (int j = 0; j < matr[0].length; j++) {
  90.                 matr[i][j] = (int) (Math.random() * 10);
  91.             }
  92.         }
  93.     }
  94.  
  95.     public static void print() {
  96.         for (int i = 0; i < matr.length; i++) {
  97.             for (int j = 0; j < matr[0].length; j++) {
  98.                 System.out.print(matr[i][j] + " ");
  99.             }
  100.             System.out.println();
  101.         }
  102.     }
  103.  
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment