如何在具有相同主体的线程中启动新线程?

我想在具有相同主体的线程中启动一个线程。创建新线程后,我想绕过启动新线程的行并运行其余代码。


以下代码是我的失败实现。我希望输出中有“返回”。实际上,只打印了“start”。我怎样才能解决这个问题?


在此先感谢您的帮助!


public static void main(String[] args) throws InterruptedException{

    System.out.println("start");

    new Thread(myThread()).start();

    System.out.println("return");

    return;

}


private static Runnable myThread() throws InterruptedException{

    System.out.println("start");

    Thread.sleep(1000);

    new Thread(myThread()).start();

    System.out.println("return");

    return null;

}


慕虎7371278
浏览 91回答 3
3回答

慕田峪4524236

首先,要创建一个 thead,您将一个 runnable 传递给构造函数。您所做的是尝试将myThread()返回的值传递给它,而不是方法引用。(不要)试试这个:(它可能会导致系统崩溃,因为它会产生无限数量的线程)public static void main(String[] args) throws InterruptedException{&nbsp; &nbsp; System.out.println("start");&nbsp; &nbsp; new Thread(() -> myThread()).start(); // Added () ->&nbsp;&nbsp; &nbsp; System.out.println("return");}private static void myThread() throws InterruptedException{&nbsp; &nbsp; System.out.println("start");&nbsp; &nbsp; Thread.sleep(1000);&nbsp; &nbsp; new Thread(() -> myThread()).start(); // Added () ->&nbsp;&nbsp; &nbsp; System.out.println("return");}我也让它返回void,因为此时返回 null 毫无意义。然后,正如所指出的,您需要限制创建的线程数量。例如,如果你想要两个线程:private static final int numThreads = 0; // addedpublic static void main(String[] args) throws InterruptedException{&nbsp; &nbsp; System.out.println("start");&nbsp; &nbsp; new Thread(() -> myThread()).start();&nbsp; &nbsp; System.out.println("return");}private static void myThread() throws InterruptedException{&nbsp; &nbsp; System.out.println("start");&nbsp; &nbsp; Thread.sleep(1000);&nbsp; &nbsp; if (++numThreads < 2) // added&nbsp; &nbsp; &nbsp; &nbsp; new Thread(() -> myThread()).start();&nbsp; &nbsp; System.out.println("return");}

蝴蝶刀刀

我想在具有相同主体的线程中启动一个线程。创建新线程后,我想绕过启动新线程的行并运行其余代码。是这样的:public class Test {&nbsp; &nbsp; public class Worker extends Runnable {&nbsp; &nbsp; &nbsp; &nbsp; private boolean launchedSubthread = false;&nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!launchedSubthread) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; launchedSubthread = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Thread(this).start();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Now do some stuff.&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; new Thread(new Worker()).start();&nbsp; &nbsp; }}请注意,当我们启动类中的第二个子线程时Worker,我们将this作为Runnable. 所以这两个子线程将共享一个Worker实例作为它们的“主体”。(我假设这就是你想要做的。)在你想让两个线程读取或更新 的其他变量Worker,那么你必须适当地使用volatileor synchronized。这不适用于我使用的方式launchedSubthread。这是因为在调用和新启动的线程上的调用之间发生了happens before 。start()run()您的尝试有几个问题。myThread被错误命名。它返回的Runnable不是Thread.您没有做任何事情来阻止MyThread实例的无限链创建。您实际上并没有创建任何线程。如果仔细观察myThread(),它会在创建任何线程之前(无限地)递归。myThread()调用null返回。如果它实际上被传递给了Thread(Runnable)构造函数,你就会得到一个 NPE。

至尊宝的传说

尝试这个 :public static void main(String[] args) throws InterruptedException{&nbsp; &nbsp; System.out.println("start");&nbsp; &nbsp; new Thread(myThread()).start();&nbsp; &nbsp; System.out.println("return");&nbsp; &nbsp; return;}static boolean threadStared = false;private static Runnable myThread() throws InterruptedException{&nbsp; &nbsp; System.out.println("start");&nbsp; &nbsp; Thread.sleep(1000);&nbsp; &nbsp; if(!threadStared){&nbsp; &nbsp; &nbsp; &nbsp; new Thread(myThread()).start();&nbsp; &nbsp; &nbsp; &nbsp; threadStared = true;&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println("return");&nbsp; &nbsp; return null;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java