I have to create a program that simulates concurrent matrix addition and multiplication. I realize that if I have 3 matrices: A, B, and C, and I want to calculate A+B = C or A*B = C, then the maximum amount of threads I can create are (rows in C) * (columns in C), since each final position in matrix C can be calculated independent of the other positions.
My real question is this: if I have a interface MatrixMath
which has methods multiply(), add(), print()
, how can I ensure that when the add()
or multiply()
methods terminate, all the changes are done being written to the product or sum matrix?
Example:
class MatrixMathImplementation implements MatrixMath {
public void multiply(int[][]A, int[][]B, int[][]C) {
//multiply the two matrices, spawning m*n threads
//haven't coded this yet
}
public void add(int[][]A, int[][]B, int[][]C) {
//add the two matricies, spawning m*n threads
//First: Check that A, B, and C are all the same size
if (A.length == B.length && A.length == C.length &&
A[0].length == B[0].length && A[0].length == C[0].length) {
for (int row=0; row < A.length; row++) {
for (int col=0; col < A[0].length; col++) {
new MatrixSumThread(A,B,C,row,col);
}
}
} else {
System.out.println("ERROR: Arrays are not the same size.");
}
}
}
public void print() {
//print the given matrix
//doesn't need to be concurrent, haven't coded this yet either.
}
}
In the code, MatrixSumThread
creates a runnable that will calculate the sum needed for the specific row and column, and put it into that row and column in matrix C. I'll make a similar runnable class for MatrixProductThread
.
Any ideas on how to make sure that if I have:
someMatrixMathObject.add(A,B,C);
someMatrixMathObject.multiply(A,B,C);
That I can ensure the add
finishes before the multiply
, or vice versa? Thank you for any help.
Collection<Callable<Void>>
and then chuck all those intoExecutorService.invokeAll
- this will return when its done. P.S. do you really think it's worth using threads to add each pair of numbers? (hint; no, it's not). – Boris the Spider