java - 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);
}
}
}
解决方案
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
以明确发生了什么。
推荐阅读
- r - 多个 Linux 发行版中的共享 R 包
- java - 如何将图像(png)中的所有颜色转换为黑色?
- c++ - 为什么带值的模板不能不接受integral_constant并使用 ()() 来提取值?
- python - Tkinter Treeview 没有显示新插入的项目,没有给出错误
- google-sheets - 部分匹配vlookup造句
- javascript - 我的 ajax 调用返回带有 html 代码的数据
- javascript - 拖动png图像时是否可以删除背景颜色?
- java - 在运行时使用 ssl 证书在一个应用程序中进行多个 Secure As400 连接 - Java
- makefile - 如何为 Ocaml 项目生成正确的 makefile
- c++ - 在 C++ 中定义包含 TTF_Font 变量的向量时出现“未定义大小”错误