首页 > 解决方案 > Java - 我尝试打印时出现线程池同步问题

问题描述

我使用 ThreadPool 类,当我尝试打印矩阵时,这就是我得到的:

 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]
 -----------------------------------------------

我尝试同步但仍然无法正常工作,为什么?还有一个问题,当我尝试在主要的 GenerateMatrix 类中使用矩阵队列时说队列是空的,什么 id 做错了?因为我尝试做的是生成 n 矩阵,并在生成完成后将所有矩阵相乘。

主要的:

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);

    }

}
}  

生成矩阵类:

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);
    }
}
}

标签: javamultithreadingsynchronizationthreadpool

解决方案


matrices类中为空的原因Main是因为您从未向其中添加任何内容。您创建了 2 个名为 的变量matrices,一个 in Main,一个 in GenerateMatrices。如果您查看Main,则调用:

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

然后你再也不会使用它,所以它仍然是空的。但是,如果您调用GenerateMatrices.getMatrices().length了 ,它应该是非零的(假设您定义了getMatrices().

至于为什么打印很奇怪,下面的 SO 答案应该会有所帮助:Java: System.out.println and System.err.println out of order

简而言之,System.out.println()由于系统调用的高开销,调用不会立即写入输出。相反,它们被写入缓冲区,当 JVM 很高兴在不久的将来不会有更多内容到达时,缓冲区的内容被写入输出。System.out.flush()迫使此过程更快发生,这可能会帮助您满足您的需求。

或者,synchronized (this) {...}您可以使用syncrhonized (System.out) {...}以确保没有交错,而不是使用。

附言

GenerateMatrices.matrices可能不应该是静态的。如果您创建 的多个实例GenerateMatrices,您可能希望它们具有单独的matrices. 如果没有,您可能应该创建一个类,MatrixStore以明确发生了什么。


推荐阅读