Reputation: 29
I want to add two matrices with each other using a thread pool that would result in the process completing faster. The program works fine when I don't using the executor framework for addition but crashes when I implement the thread pool. The problem has to be in the addition method any help in getting this to run would be helpful! Thanks :).
Here is the code from the matrix class. I haven't included the main class since all it does is get the dimensions to create the matrix. If you also want the main class let me know and I will include it.
package matrixproject;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Matrix {
int row;
int column;
int [][] matrixArray;
public Matrix(){
}
public Matrix (int numberofRows,int numberofColumns,int maxNumber,int minNumber){
this.row = numberofRows;
this.column = numberofColumns;
matrixArray = new int [row][column];
populateMatrix(minNumber,maxNumber);
}
public Matrix(int [][] matrixArrayCopy)
{
this.row = matrixArrayCopy.length;
this.column = (matrixArrayCopy[0]).length;
matrixArray = new int[row][column];
for(int i=0; i<row; i++)
{
System.arraycopy(matrixArrayCopy[i], 0, matrixArray[i], 0, column);
}
}
private void populateMatrix(int min, int max)
{
Random rand = new Random();
for (int i=0; i<row; i++)
{
for (int j=0; j<column; j++)
{
matrixArray[i][j] = rand.nextInt((max - min) + 1) + min;
}
}
}
public Matrix addition (Matrix additionMatrix){
int threadnum = Runtime.getRuntime().availableProcessors();
ExecutorService executor = Executors.newFixedThreadPool(threadnum);
int [][] matrixAddResult = new int [this.row][this.column];
for (int i = 0; i <row; i+=1){
for (int j =0; j <column; j+=1){
//made new variables equal to i and j to get around needing a final variable for the lambda function
int index = i;
int jndex = j;
executor.submit(() -> {
matrixAddResult[index][jndex] += this.matrixArray[index][jndex] + additionMatrix.matrixArray[index][jndex];
});
executor.shutdown();
}
}
return new Matrix(matrixAddResult);
}
Here are the errors I get when I reach the addition method:
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@452b3a41 rejected from java.util.concurrent.ThreadPoolExecutor@4a574795[Shutting down, pool size = 1, active threads = 1, queued tasks = 0, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
at matrixproject.Matrix.addition(Matrix.java:65)
at matrixproject.MatrixProject.main(MatrixProject.java:73)
Java Result: 1
Upvotes: 0
Views: 843
Reputation: 533930
Instead of creating and shutting down a thread pool repeatedly, you might be better off using the built in CommonForkJoinPool via parallel Stream
public Matrix addition (Matrix additionMatrix){
int[][] matrixAddResult = new int[this.row][this.column];
IntStream.range(0, row).parallel()
.forEach(i -> {
for (int j = 0; j < column; j++)
matrixAddResult[i][j] = this.matrixArray[i][j]
+ additionMatrix.matrixArray[i][j];
});
return new Matrix(matrixAddResult);
}
Upvotes: 0
Reputation: 3063
It literally says : "Shutting down" in the exception. :)
You are calling shutdown()
method inside the inner-loop which mean you're trying to submit new tasks after in the next iterations after the pool has already received the shutdown command, and that's why you're getting the rejection exception.
Move your executor.shutdown();
outside the top-level for-loop
Upvotes: 1