【问题标题】:Java - ThreadPool synchronized problem when i try printingJava - 我尝试打印时出现线程池同步问题
【发布时间】:2019-08-16 10:31:21
【问题描述】:

我使用 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);
    }
}
}

【问题讨论】:

    标签: java multithreading synchronization threadpool


    【解决方案1】:

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

    Queue&lt;int[][]&gt; matrices = new LinkedList&lt;int[][]&gt;();

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

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

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

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

    附:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多