0

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);
    }
}
}
Vinay Hegde
  • 1,047
  • 1
  • 6
  • 20
liran
  • 35
  • 5

1 Answers1

0

The reason why matrices is empty in the class Main is because you never add anything to it. You create 2 variables called matrices, one in Main, one in GenerateMatrices.If you look in Main, you call:

Queue<int[][]> matrices = new LinkedList<int[][]>();

and then you never use it again, so it remains empty. If however you called GenerateMatrices.getMatrices().length, it should be non-zero (assuming you define getMatrices().

As for why it is printing weirdly, the following SO answer should help: Java: System.out.println and System.err.println out of order

In short, calls to System.out.println() are not immediately written to output, due to the high overhead of the system call. Instead, they are written to a buffer and, when the JVM is happy that more isn't going to arrive in the near future, the contents of the buffer is written to output. System.out.flush() forces this process to happen sooner, which may help you with what you need.

Alternatively, instead of using synchronized (this) {...} you can use syncrhonized (System.out) {...} to ensure no interleaving.

P.S.

GenerateMatrices.matrices probably shouldn't be static. If you create multiple instances of GenerateMatrices, you probably want them to have separate copies of matrices. And if not, you should probably create a class such as MatrixStore to make it clear what is happening.

cameron1024
  • 2,931
  • 1
  • 8
  • 19