【问题标题】:ThreadPoolExecutor Error Matrix JavaThreadPoolExecutor 错误矩阵 Java
【发布时间】:2015-05-27 04:21:03
【问题描述】:

我的代码出现以下错误:

线程“main”中的异常 java.util.concurrent.RejectedExecutionException:任务 java.util.concurrent.FutureTask@4cd98b00 从 java.util.concurrent.ThreadPoolExecutor@5e34d46a 被拒绝 [已终止,池大小 = 0,活动线程 = 0 , 排队的任务 = 0, 完成的任务 = 1] 在 matrix.ParallelMatrix.main(ParallelMatrix.java:38)

我不确定哪里出了问题,因为代码中似乎没有错误,而且我不熟悉这个错误,因为以前从未遇到过。

第 38 行是:

pool.submit(new Multi(N,i,j,a,b,c));

这里是所有代码:

package matrix;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ParallelMatrix {

    public final static int N = 1000; // Random size of matrix

    public static void main(String[] args) throws InterruptedException {
        long startTime = System.currentTimeMillis();

        //Create and multiply matrix of random size N.   
        double [][] a = new double [N][N];
        double [][] b = new double [N][N];
        double [][] c = new double [N][N];

        int i,j,k;

        for(i = 0; i < N ; i++) {
            for(j = 0; j < N ; j++){
                a[i][j] = i + j;
                b[i][j] = i * j;
            }
        }

        ExecutorService pool = Executors.newFixedThreadPool(1);
        for(i = 0; i < N; i++) {
            for(j = 0; j < N; j++) { 
                c[i][j] = 0;
                for(k = 0; k < N; k++) {
                    c[i][j] += a[i][k] * b[k][j]; //C matrix calculated from a and B matrices.
                }
                pool.submit(new Multi(N,i,j,a,b,c));
                pool.shutdown();
                pool.awaitTermination(1, TimeUnit.DAYS);
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("Calculation completed in " + (endTime - startTime) + " milliseconds");
    }

    static class Multi implements Runnable {
        final int N;
        final double [][] a;
        final double [][] b;
        final double [][] c;
        final int i;
        final int j;

        public Multi(int N, int i, int j, double[][] a, double[][] b, double[][] c) {
            this.N=N;
            this.i=i;
            this.j=j;
            this.a=a;
            this.b=b;
            this.c=c;
        }

        @Override
        public void run() {
            for(int k = 0; k < N; k++)
                c[i][j] += a[i][k] * b[k][j];
        }
    }
}

【问题讨论】:

  • 这个错误不是直接的错误,而是一个异常。更多内容可以阅读here。 “当无法接受任务执行时,Executor 抛出的异常。”
  • 那么我该如何避免这种情况呢?抱歉有点糊涂了。
  • 这可能是由于您调用了shutdown()。提交任务后,如果您只想等到任务完成,您应该在关闭之前使用生成的 Future 来获取()结果。

标签: java multithreading threadpool


【解决方案1】:

要等待提交的作业完成,请调用执行程序的 awaitTermination() 方法。见How to wait for all threads to finish, using ExecutorService?

【讨论】:

  • 我添加了 try { pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { } 在 pool.shutdown 之后,但错误仍然存​​在。我也已经调用了方法。
猜你喜欢
  • 2017-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多