برگزیده های پرشین تولز

سوال در مورد یک برنامه همروند در جاوا

jopeter

کاربر تازه وارد
تاریخ عضویت
20 جولای 2006
نوشته‌ها
42
لایک‌ها
0
سلام

من برنامه زیر رو که به زبان جاوا و برای ضرب دو ماتریس به صورت همروند است رو دارم

البته ارور داره


این برنامه با فرض بودن
n*n
پردازه نوشته شده حالا اگه
n/2


پردازه داشته باشم چطور باید
به
thread
ها بگم که تعداد سظر و ستون بیشتری رو باید ضرب کنن؟
ممنون



package matrixmul;

import java.lang.Integer;


public class Mul extends Thread{
private int A[][],B[][],R[][];
int n,row,col;



public Mul(String s)
{
this.setName (s);
}



public void setMatrix (int A[][],int B[][],int R[][],int n )
{
this.A=A;
this.B=B;
this.R=R;
this.n=n;
}



public void delay()
{
try{
this.sleep(1000);
}catch(Exception e)
{
System.out.println(e.toString());
}
}



public void setIndics (int r,int c)
{
row=r;
col=c;
}




public void run()
{
int i;
R[row][col]=0;
for(i=0;i<n;i++)
R[row][col]+=A[row]*B[col];
System.out.println("["+Integer.toString(row)+"]["+Integer.toString(col)+"]"+Integer.toString(R[row][col]));

}



// private String Integer(int col) {
// return null;
// }

}

---------------------------------------------------

package matrixmul;
public class Main {

public Main() {
}

public static void main(String[] args)
{int n=4;
int A[][]=new int [n][n];
int B[][]=new int [n][n];
int R[][]=new int [n][n];
Keyboard key=new Keyboard();
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
A[j]=Keyboard.readInt();
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
B[j]=Keyboard.readInt();
Mul threads[]=new Mul[n*n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
threads[i*n+j]=new Mul["Thread"+Integer.toString(i*n+j)];
threads[i*n+j].setMatrix(A,B,R,n);
threads[i*n+j].setIndics(i,j);
}


for(int i=0;i<n*n;i++)
threads.start();
for(int i=0;i<n*n;i++)
try{
threads.join();
}catch(Exception e)
{

}

for(int i=0;i<n;i++)
{for(int j=0;j<n;j++)
{System.out.print(R[j]);
System.out.println(" "); }

System.out.println(); }



}

}
 
بالا