'I need help accessing this class
I really need to fix this and I don't have any idea how, so any kind of help would be much appreciated. I am kind of a newbie to Java, yet I have knowledge in writing code. I want to add a new thread using the class AddComputeThread, but I am not sure how to access it or call the methods in it. Any idea? When trying to create a new instance of the class, it gives me the error that the constructor of the AddComputeThread is not defined.
Here is the java file
package pgdp.pingulib.math.matrix;
import java.util.concurrent.atomic.AtomicInteger;
public class SquareMatrixAdd {
private static final int MIN_DIM = 1 << 1;
/**
* Sequential matrix addition of A and B
*
* @param A matrix
* @param B matrix
* @return result of the matrix addition in a new matrix
*/
public static SquareMatrix addSequential(SquareMatrix A, SquareMatrix B) {
if (A == null) {
throw new IllegalArgumentException("A can not be null");
}
if (B == null) {
throw new IllegalArgumentException("B can not be null");
}
if (A.getDimension() != B.getDimension()) {
throw new IllegalArgumentException("A and B have different dimensions");
}
SquareMatrix C = new SquareMatrix(A.getDimension());
for (int i = 1; i <= A.getDimension(); i++) {
for (int j = 1; j <= A.getDimension(); j++) {
C.set(i, j, A.get(i, j).add(B.get(i, j)));
}
}
return C;
}
/**
* Parallel matrix addition of A and B. If the dimensions are smaller or equal
* than the default value <MIN_DIM>, the sequential matrix addition will be
* applied.
*
* @param A matrix
* @param B matrix
* @return result of the matrix addition in a new matrix
*/
public static SquareMatrix addParallel(SquareMatrix A, SquareMatrix B) {
return addParallel(A, B, MIN_DIM);
}
/**
* Parallel matrix addition of A and B. If <minDim> is smaller than the default
* value <MIN_DIM>, the default value will be used. If the dimensions are
* smaller or equal than <minDim>, the sequential matrix addition will be
* applied.
*
* @param A matrix
* @param B matrix
* @param minDim dimension
* @return result of the matrix addition in a new matrix
*/
public static SquareMatrix addParallel(SquareMatrix A, SquareMatrix B, int minDim) {
// TODO
SquareMatrix C = new SquareMatrix(A.getDimension());
if(minDim < MIN_DIM)
minDim = MIN_DIM;
else if (A == null) {
throw new IllegalArgumentException("A can not be null");
}
if (B == null) {
throw new IllegalArgumentException("B can not be null");
}
if (A.getDimension() != B.getDimension()) {
throw new IllegalArgumentException("A and B have different dimensions");
}
if (A.getDimension() <= minDim) {
C = addSequential(A, B);
} else {
for( int i = 1; i < A.getDimension()/2; ++i ) {
for( int j = 1; j < A.getDimension()/2; ++j ) {
AddComputeThread trd = new AddComputeThread(1, A, B, minDim, C);
}
}
}
return C;
}
private static class AddComputeThread extends ComputeThread {
private static AtomicInteger computeThreadCounter = new AtomicInteger(0);
/**
* initialize compute thread for matrix addition.
*
* @param threadID index where result will be stored
* @param A first matrix
* @param B second matrix
* @param minDim threshold for sequential computation
* @param results array reference where the result will be stored
*/
AddComputeThread(int threadID, SquareMatrix A, SquareMatrix B, int minDim, SquareMatrix[] results) {
super(threadID, A, B, minDim, results);
this.setName("AddComputeThread-" + computeThreadCounter.incrementAndGet());
}
/**
* resets the thread counter
*/
public static synchronized void resetThreadCount() {
computeThreadCounter = new AtomicInteger(0);
}
/**
* retrieves the current thread count
*
* @return thread count
*/
public static AtomicInteger getThreadCount() {
return computeThreadCounter;
}
@Override
public void run() {
// TODO
this.start();
}
}
}
Solution 1:[1]
Here:
AddComputeThread trd = new AddComputeThread(1, A, B, minDim, C);
you are trying to call this AddComputeThread constructor:
AddComputeThread(int threadID, SquareMatrix A, SquareMatrix B, int minDim, SquareMatrix[] results)
But C, the fifth argument you give, is an instance of SquareMatrix, not an array as required.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | khelwood |
