run.start()方法永远不会调用run()

Ani*_*kar 1 java multithreading constructor

我写了一个小的多线程程序.

public class NewThread implements Runnable {
    Thread t;

    public NewThread() {
        t = new Thread(this, "Thread created by Thread Class.");
        System.out.println("Created by constuctor:"+ t);
        t.start(); // This will call run, because t has context as this
    }   

    @Override
    public void run() {     
        System.out.println("run() method called.");     
    }

    public static void main(String[] args) {
        new NewThread();
    }
}
Run Code Online (Sandbox Code Playgroud)

这就是本书所说的写作方式.但是,我从来没有run() method called在控制台中获得该声明.因此,似乎run()永远不会被称为.这怎么可能是真的?

编辑:是的,从构造函数启动一个Thread是不好的做法,但这不会影响问题.(我得到的票数太多了)

Gra*_*ray 9

run.start()方法永远不会调用run()

您的代码实际上可以在我的系统上运行,但它不适用于您的系统,表明您有一个经典的竞争条件.

在内部main(),NewThread构造,但Java语言说它可以重新排序操作,以便构造函数中的操作可以构造函数完成发生.因此,有可能main()会结束之前NewThread实际上已经开始这会导致JVM关闭不运行的线程.

由于指令重新排序,你不应该在构造函数内部自动启动一个线程.请参阅:为什么不在构造函数中启动线程?如何终止?

你应该这样做:

public NewThread() {
    t = new Thread(this, "Thread created by Thread Class.");
    System.out.println("Created by constuctor:" + t);
    // don't start here
}
public void start() {
    // start in another method
    t.start();
}
public void run() {     
    System.out.println("run() method called.");     
}
...

public static void main(String[] args) {
    NewThread nt = new NewThread();
    nt.start();
}
Run Code Online (Sandbox Code Playgroud)

由于与NewThread主线程(非守护进程)具有相同的守护程序状态,因此JVM在nt.run()完成之前不会关闭.