簡體   English   中英

Java線程池-嘗試生成n矩陣

[英]Java Thread Pool - try to generate n matrix

我很樂意提供幫助,我正在嘗試創建矩陣並將其使用threadPool放入列表中,但途中存在一些問題。

我嘗試生成n個矩陣,並且在運行程序時出現此錯誤:線程池由於一個問題而中斷:null,我嘗試解決此問題,但我無法解決該問題。

這是我的代碼:

主要:

public class main {
    public static void main(String[] args) {
        int numOfThreads = 5;
        ThreadPool pool = new ThreadPool(numOfThreads);
        int numOfMatrices = 5;
        int diminesion = 3;

        GenerateMatrices generateMatrices = new GenerateMatrices(numOfMatrices, 
    diminesion);
        pool.execute(generateMatrices);

    }
}

ThreadPool類:

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

這是我的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();
            }
        }
    }
}

謝謝!!!!

很可能會收到NullPointerException因為您沒有在GenerateMatrices類中初始化屬性matrices

另外,到目前為止,您的程序仍然在一個線程中生成矩陣,因為您僅向線程池提交了一個Runnable ,並且該Runnablefor-loop生成了矩陣

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM