Java线程池-尝试生成n矩阵
我很乐意提供帮助,我正在尝试创建矩阵,并使用threadPool将它们放入列表中,但我有一些问题要解决 我尝试生成n个矩阵,当我运行程序时,我得到以下错误: 线程池因以下问题而中断:null 我试图解决这个问题,但我没能解决这个问题 这是我的代码: 主要内容: 线程池类:Java线程池-尝试生成n矩阵,java,multithreading,threadpool,Java,Multithreading,Threadpool,我很乐意提供帮助,我正在尝试创建矩阵,并使用threadPool将它们放入列表中,但我有一些问题要解决 我尝试生成n个矩阵,当我运行程序时,我得到以下错误: 线程池因以下问题而中断:null 我试图解决这个问题,但我没能解决这个问题 这是我的代码: 主要内容: 线程池类: import java.util.concurrent.LinkedBlockingQueue; public class ThreadPool { private final int nThreads; pr
import java.util.concurrent.LinkedBlockingQueue;
public class ThreadPool {
private final int nThreads;
private final PoolWorker[] threads;
private final LinkedBlockingQueue queue;
public ThreadPool(int nThreads) {
this.nThreads = nThreads;
queue = new LinkedBlockingQueue();
threads = new PoolWorker[nThreads];
for (int i = 0; i < nThreads; i++) {
threads[i] = new PoolWorker();
threads[i].start();
}
}
public void execute(Runnable task) {
synchronized (queue) {
queue.add(task);
queue.notify();
}
}
private class PoolWorker extends Thread {
public void run() {
Runnable task;
while (true) {
synchronized (queue) {
while (queue.isEmpty()) {
try {
queue.wait();
} catch (InterruptedException e) {
System.out.println("An error occurred while queue is
waiting: " + e.getMessage());
}
}
task = (Runnable) queue.poll();
}
// If we don't catch RuntimeException,
// the pool could leak threads
try {
task.run();
} catch (RuntimeException e) {
System.out.println("Thread pool is interrupted due to an
issue: " + e.getMessage());
}
}
}
}
}
import java.util.concurrent.LinkedBlockingQueue;
公共类线程池{
非公开最终审查;
私有最终池工作者[]线程;
私有最终LinkedBlockingQueue队列;
公共线程池(int-nThreads){
this.nThreads=nThreads;
队列=新的LinkedBlockingQueue();
线程=新的池工作者[nThreads];
对于(int i=0;i
这是我的GenerateMatrices课程:
import java.util.Queue;
public class GenerateMatrices implements Runnable {
private int numOfMatrices;
private int dimension;
private Queue<int[][]> matrices;
public GenerateMatrices(int n, int d) {
numOfMatrices = n;
dimension = d;
}
public Queue<int[][]> getMatrices() {
return matrices;
}
public void run() {
int[][] tempMatrix = new int[dimension][dimension];
for (int k = 0; k < numOfMatrices; k++) {
for (int i = 0; i < tempMatrix.length; i++) {
for (int j = 0; j < tempMatrix.length; j++) {
tempMatrix[i][j] = (int) (Math.random() + 1);
}
}
matrices.add(tempMatrix);
for (int i = 0; i < tempMatrix.length; i++) {
for (int j = 0; j < tempMatrix.length; j++) {
System.out.print(tempMatrix[i][j]);
}
System.out.println();
}
}
}
}
import java.util.Queue;
公共类GenerateMatrices实现可运行{
私有整数矩阵;
私有整数维;
专用队列矩阵;
公共生成矩阵(int n,int d){
numof矩阵=n;
尺寸=d;
}
公共队列getMatrix(){
返回矩阵;
}
公开募捐{
int[]tempMatrix=新的int[dimension][dimension];
对于(int k=0;k
谢谢 最有可能得到
NullPointerException
,因为在GenerateMatrices
类中未初始化属性矩阵
同样到目前为止,您的程序仍然在一个线程中生成矩阵,因为您只向线程池提交一个Runnable
,并且Runnable
在for循环中生成矩阵
将e.getMessage()更改为e.toString(),以获取有关运行时异常的信息。更好的是,在catch上设置一个断点,并在调试器中运行它。
import java.util.Queue;
public class GenerateMatrices implements Runnable {
private int numOfMatrices;
private int dimension;
private Queue<int[][]> matrices;
public GenerateMatrices(int n, int d) {
numOfMatrices = n;
dimension = d;
}
public Queue<int[][]> getMatrices() {
return matrices;
}
public void run() {
int[][] tempMatrix = new int[dimension][dimension];
for (int k = 0; k < numOfMatrices; k++) {
for (int i = 0; i < tempMatrix.length; i++) {
for (int j = 0; j < tempMatrix.length; j++) {
tempMatrix[i][j] = (int) (Math.random() + 1);
}
}
matrices.add(tempMatrix);
for (int i = 0; i < tempMatrix.length; i++) {
for (int j = 0; j < tempMatrix.length; j++) {
System.out.print(tempMatrix[i][j]);
}
System.out.println();
}
}
}
}