0

I am getting the following error from my code:

Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@4cd98b00 rejected from java.util.concurrent.ThreadPoolExecutor@5e34d46a[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 1] at matrix.ParallelMatrix.main(ParallelMatrix.java:38)

Im not sure what is wrong as there seems to be no errors in the code and am unfamiliar with this error as have never come across it before.

Line 38 is:

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

Here is all code:

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];
        }
    }
}
Emz
  • 1,280
  • 1
  • 14
  • 29
  • The error is not directly an error per see, it is an exception. More of it can be read [here](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/RejectedExecutionException.html). "Exception thrown by an Executor when a task cannot be accepted for execution." – Emz Mar 24 '15 at 00:16
  • So how do i avoid this? Sorry bit confused. – Ali mohammad Mar 24 '15 at 00:20
  • It's probably due to your call to shutdown(). After submitting the task you should use the resulting Future to get() the result before shutting things down if all you want to do is wait until the task completes. – user268396 Mar 24 '15 at 00:28

1 Answers1

0

To wait for the submitted jobs to complete, you call the executors awaitTermination() method. See How to wait for all threads to finish, using ExecutorService?

Community
  • 1
  • 1
Zagrev
  • 2,000
  • 11
  • 8
  • I added try { pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { } after pool.shutdown but the error still there. I also already have called the methodd. – Ali mohammad Mar 24 '15 at 03:05