Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Thread.start()方法从不调用run()_Java_Multithreading_Constructor - Fatal编程技术网

Java Thread.start()方法从不调用run()

Java Thread.start()方法从不调用run(),java,multithreading,constructor,Java,Multithreading,Constructor,我已经写了一个小的多线程程序 公共类NewThread实现Runnable{ 螺纹t; 公共新线程{ t=新线程,该线程由Thread类创建。; 构造函数创建的System.out.PrintLn:+t; t、 start;//这将调用run,因为t的上下文如下 } @凌驾 公共无效运行{ 调用System.out.printlnrun方法。; } 公共静态无效字符串[]args{ 新线程; } } 这本书是这样写的。但是,我从未在控制台中获得名为语句的run方法。因此,似乎从来没有叫过r

我已经写了一个小的多线程程序

公共类NewThread实现Runnable{ 螺纹t; 公共新线程{ t=新线程,该线程由Thread类创建。; 构造函数创建的System.out.PrintLn:+t; t、 start;//这将调用run,因为t的上下文如下 } @凌驾 公共无效运行{ 调用System.out.printlnrun方法。; } 公共静态无效字符串[]args{ 新线程; } } 这本书是这样写的。但是,我从未在控制台中获得名为语句的run方法。因此,似乎从来没有叫过run。这怎么可能是真的

编辑:是的,从构造函数启动线程是不好的做法,但这不会影响问题。我为此得到了太多的反对票

Thread.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();
}
由于NewThread具有与主线程相同的守护程序状态(非守护程序),因此JVM将在nt.run完成之前不会关闭

Thread.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();
}

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

我强烈建议不要在构造函数中启动线程。除此之外,您不必等待新线程启动,它也没有机会打印任何内容—您需要加入它。您的问题到底是什么,您的代码返回由构造函数创建的结果:调用了thread[thread Created by thread Class.,5,main]run方法。@YouceFladani否。不会。结果是竞争危险,结果未知。啊,好的,你的问题不清楚。它不返回调用的运行方法。我强烈建议不要在构造函数中启动线程。除此之外,您不必等待新线程启动,它也没有机会打印任何内容—您需要加入它。您的问题到底是什么,您的代码返回由构造函数创建的结果:调用了thread[thread Created by thread Class.,5,main]run方法。@YouceFladani否。不会。结果是比赛危险,结果未知。啊,好的,你的问题不清楚。当你从答案中删除它时,它不会返回名为“安全链接”的跑步方法,将其放在此处供以后参考。谢谢@AnilBhaskar。我仍然在寻找一个关于这个主题的更好的页面。有用的链接把它放在这里供以后参考,因为你从答案中删除了它。谢谢@AnilBhaskar。我仍然在寻找一个更好的页面。