1
我無法理解下面的代碼。我的主要問題是爲什麼使用在線程中分配給「表」數組的值來更新「a」數組。爲了更具體一點,我想解釋爲什麼「a」數組不能打印最初的元素(0,1,2,3 ...)。線程的陣列更新
的主要方法的代碼和線程:
public class ThreadParSqrt
{
public static void main(String[] args)
{
double[] a = new double[1000];
for (int i = 0; i < 1000; i++)
a[i] = i;
SqrtThread threads[] = new SqrtThread[1000];
for (int i = 0; i < 1000; i++)
{
threads[i] = new SqrtThread(a,i);
threads[i].start();
}
for (int i = 0; i < 1000; i++)
{
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 0; i < 1000; i++)
{
System.out.println(a[i]);
}
}
}
public class SqrtThread extends Thread
{
private double [] table;
private int index;
public SqrtThread(double [] array, int ind)
{
table = array;
index = ind;
}
public void run()
{
table[index] = Math.sqrt(table[index]);
}
}