我正在尝试运行该程序,但它会引发异常,我无法修复。
public class Multithread extends Thread {
static int [][]array1;
static int [][]array2;
static int [][]array3;
static final int i=10;
static final int j=10;
static final int k=10;
volatile int start;
volatile int end;
Multithread(int start,int end){
this.start=start;
this.end=end;
}
private static void multiplication() {
int processors=Runtime.getRuntime().availableProcessors();
int threadcount=processors>=i ? i:processors;
Thread [] t=new Thread[threadcount];
int [] idx= new int[threadcount+1];
for(int a=0;a<=threadcount;a++){
idx[a]=(a*i)/threadcount;
//System.out.println("idx:"+idx[a]);
}
for(int a=0;a<threadcount;a++){
t[a]=new Multithread(idx[a],idx[a+1]);
//thread 1=start 0 end 9, thread 2 = 9 end =19
t[a].start();
}
}
public static void main(String[] args) {
for(int a = 0; a < i; a++)
for(int b = 0; b < j; b++)
array1[a][b]= (int)(Math.random()*10);
for(int a=0;a<j;a++)
for(int b=0;b<k;b++)
array2[a][b]= (int)(Math.random()*10);
long starttime=System.nanoTime();
multiplication();
long endtime=System.nanoTime();
System.out.println("Total time taken is "+((endtime-starttime)/1000));
}
public void run(){
for(int a=start;a<end;a++){
for(int b=0;b<k;b++){
for(int c=0;c<j;c++){
array3[a][b]=(array3[a][b]+(array1[a][c]*array2[c][b]));
}
}
}
}
}
此代码将为并行编程中的矩阵提供执行时间。它给了我NullPointerException
。我无法解决这个问题。它给出了array1 [a] [b]的例外。我试过解决它,但不能。
答案 0 :(得分:0)
您没有正确初始化变量。只需将您的2d数组初始化为某个内容,以便稍后可以通过引用。
array1 = blah blah //this is right
array1[a][b] //you are creating it like this everytime which is wrong.