هذا البرنامج لضرب مصفوفتين ولكنه يقوم بحساب كل ناتج فى المصفوفه الجديده فى ثريد مختلف
ولكن فيه مشكله ولا يطلع الناتج ارجو المساعده
public class MatrixMultibly{
static int A[][] = {{2,3,4,5},{4,1,4,5},{3,2,4,5},{5,5,5,5}};
static int B[][] = {{5,7,4,5},{6,8,4,5},{4,2,4,5},{5,5,5,5}};
static int M = A.length;
static int K = B[0].length;
static int C[][] = new int[M][K];
public static myThread[] task;
class myThread extends Thread{
int i,j,k;
int ID;
myThread(int index,int i,int j ){
ID = index;
this.i=i;
this.j=j;
//this.k=k;
}
public void run(){
//Display info about this particular thread
for(int k = 0; k < A[0].length; k++){
C[j]+=A[k]*B[k][j];
}
}
}
public MatrixMultibly() {
// int A[][] = {{2,3,4},{4,1,1}}; // 2 x 3
// int B[][] = {{5,4},{6,8},{1,1}}; // 3 x 2
assert (A[0].length != B.length);
int ThreadNumber =0;
for(int i = 0; i < M; i++) {
for(int j = 0; j < K; j++){
C[j] = 0;//A[0]*B[0][j];
ThreadNumber++;
}
}
System.out.println("Thread number" +ThreadNumber);
int taskid =1;
task = new myThread[ThreadNumber];
for(int i = 0; i < M; i++) {
for(int j = 0; j < K; j++){
//for(int k = 0; k < A[0].length; k++){
task[taskid] = new myThread(taskid,i,j);
task[taskid].start();//C[j] += A[k] * B[k][j];
System.out.println("Thread " +taskid + "started");
taskid++;
//}
}
}
System.out.println("Multiply of both matrix : ");
for(int i = 0; i < M; i++) {
for(int j = 0; j < K; j++) {
System.out.print(" "+C[j]);
}
System.out.println();
}
}
public static void main(String[] args) {
new MatrixMultibly();
// TODO code application logic here
}
}وشكرا
