无法理解这种Java参考

时间:2018-07-10 05:58:47

标签: java reference

我在Java中有问题,因为以下代码“名称”是一个引用,并且在线程运行期间更改了“名称”值,我的问题是,为什么“名称”不显示更改的值“你好,奥斯卡”? / p>

public class MakeThread implements Runnable{
    String name;
    public  MakeThread(String name) {
     this.name=name;
    }
    @Override
    public void run() {
    try {
        System.out.println("i am going to sleep 10 sec");
        TimeUnit.SECONDS.sleep(10);
        System.out.println(Thread.currentThread().getName()+"name is:"+name);
    } catch (InterruptedException e) {          
        e.printStackTrace();
    }   
    }
}


public class TestRefer {    
    public static void main(String[] args) {
    String name=new String("hello,world");
    Runnable runnable=new MakeThread(name); 
    Thread thread2=new Thread(runnable);
    thread2.start();
    name= new String("hello, oscar");   
    System.out.println("TestRefer name " +name);
    }  
}

打印结果是:

TestRefer name:hello, oscar
i am going to sleep 10 sec
Thread-0 name is:hello,world

0 个答案:

没有答案