동시 행렬 덧셈과 곱셈을 시뮬레이트하는 프로그램을 만들어야합니다. 만약 내가 3 행렬 : A, B, C를 가지고 있고 A + B = C 또는 A * B = C를 계산하고 싶다면, 내가 만들 수있는 최대 스레드 수는 (C 행) * 행렬 C의 각 최종 위치는 다른 위치와 독립적으로 계산 될 수 있기 때문에 C의 행). 행렬 곱셈/덧셈 동시성
내 진짜 문제
이있다 : 나는 방법multiply(), add(), print()
이있는 인터페이스
MatrixMath
이있는 경우, 나는
add()
또는
multiply()
방법이 종료 할 때, 모든 변경이 완료 제품 또는 합 매트릭스에 기록되는 것을 보장 할 수있는 방법?
는
예 :
코드에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.
}
}
, MatrixSumThread
특정 행과 열에 필요한 합계를 계산하고, I 만들 것이다 매트릭스 C.에 그 로우 및 컬럼에 넣어하는 실행 가능을 생성 MatrixProductThread
에 대한 비슷한 실행 가능 클래스.
someMatrixMathObject.add(A,B,C);
someMatrixMathObject.multiply(A,B,C);
나는 그 반대의 경우도 마찬가지 multiply
, 또는 전에 add
완료를 보장 할 수 있습니다 : 내가 가지고있는 경우 있는지 확인하는 방법에 대한
어떤 아이디어? 어떤 도움을 주셔서 감사합니다.
Thread t = new Thread(); // or subclass thereof
t.start(); // make sure to not start threads in the constructor; start explicitly
t.join(); // waits for the thread to finish
귀하의 경우 :
// create a list to hold all your threads, above the for loops
List<MatrixSumThread> threads = new ArrayList<MatrixSumThread>();
// for() { ...
// make sure MatrixSumThread doesn't call start() in its constructor
MatrixSumThread t = new MatrixSumThread(A,B,C,row,col);
threads.add(t);
t.start();
나중에 작업을 완료 한 후 대한-루프, 모든 스레드에 가입, 당신은 원시 스레드를 작동하는 방법
작업을'Collection>'에 넣은 다음 모든 것을'Exec utorService.invokeAll' - 완료되면 반환됩니다. 추신 스레드를 사용하여 각 쌍의 숫자를 추가하는 것이 가치 있다고 생각합니까? (암시, 그렇지 않습니다). –
나는 @BoristheSpider를 듣겠다.하지만 모든 것을 직접 처리하고 싶다면 [this] (http://javahowto.blogspot.no/2007/05/when-to-join-threads.html) 문서를 읽어야한다. 스레드 연결에 대해. "작업을 수행하기 위해 여러 스레드를 생성해야하고 모든 작업이 완료된 후에 다음 단계로 진행한다고 가정 해 봅시다. 요점은 Thread.join() 메서드를 사용하는 것입니다." – atomman
@BoristheSpider가 팁 주셔서 감사합니다, 나는 그것에 대해 조사 할 것입니다! 현실적으로 : 아니오, 여러 스레드를 사용할 가치가 있다고 생각하지 않습니다. 이것은 대학 과제를위한 것이지만, 현실적인 시나리오를 만들 필요는 없지만이 상황에서 다른 스레드 제어를 최대화하는 것이 중요합니다. –