Java 从线程更新数组

Java 从线程更新数组,java,arrays,multithreading,Java,Arrays,Multithreading,我很难理解下面的代码。我的主要问题是为什么“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;

我很难理解下面的代码。我的主要问题是为什么“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]);
        }
    }
}

因为
a
通过引用传递给
SqrtThread
的构造函数(按引用传递搜索/按值传递)。在该构造函数中,现在称为
array
的引用随后存储在私有成员
表中。但由于它是一个引用,对
表的任何更改也将是
a
中的更改(因为两个引用都指向内存中的相同数组)

我可能还应该提醒您线程安全等问题,但您似乎仍在学习基础知识。一旦你掌握了这些,你可能想要阅读线程同步、锁、事件等

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]);
    }
}