i use ThreadPool class and when i try to print the matrix this is what i get:
Enter number of threads:
5
Enter number of matrices:
3
Enter number diminesion:
2
[1][1][0][1]
[0][1]
-----------------------------------------------
[0][0]
[1][0]
-----------------------------------------------
[0][1]
-----------------------------------------------
i try to synchronized but is still not working good, why? and another question, when i try to use matrices queue from GenerateMatrix class in the main is say the queue is empty what id do wrong? because what i try to do is to generate n matrix and after the generate is finish multiply all the matrices.
main:
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Queue<int[][]> matrices = new LinkedList<int[][]>();
System.out.println("Enter number of threads:");
int numOfThreads = input.nextInt();
ThreadPool pool = new ThreadPool(numOfThreads);
System.out.println("Enter number of matrices:");
int numOfMatrices = input.nextInt();
System.out.println("Enter number diminesion:");
int diminesion = input.nextInt();
for (int i = 0; i < numOfMatrices; i++) {
GenerateMatrix generateMatrix = new GenerateMatrix(numOfMatrices,
diminesion);
pool.execute(generateMatrix);
}
}
}
GenerateMatrix class:
import java.util.LinkedList;
import java.util.Queue;
public class GenerateMatrix implements Runnable {
private int numOfMatrices;
private int dimension;
private static Queue<int[][]> matrices = new LinkedList<int[][]>();
public GenerateMatrix(int n, int d) {
numOfMatrices = n;
dimension = d;
}
public Queue<int[][]> getMatricesQueue() {
return matrices;
}
public void run() {
int[][] tempMatrix = new int[dimension][dimension];
for (int i = 0; i < tempMatrix.length; i++) {
for (int j = 0; j < tempMatrix.length; j++) {
tempMatrix[i][j] = (int) (Math.random() * 2);
}
}
synchronized (this) {
for (int k = 0; k < tempMatrix.length; k++) {
for (int j = 0; j < tempMatrix.length; j++) {
System.out.print("[" + tempMatrix[k][j] + "]");
}
System.out.println();
}
System.out.println("-----------------------------------------------
");
matrices.add(tempMatrix);
}
}
}