[英]How can I start a new thread within a thread with the same body?
I want to start a thread within a thread with the same body. 我想在具有相同主体的线程内启动线程。 Once the new thread is created, I'd like to bypass the line starting up the new thread and run the rest of the code.
创建新线程后,我想绕过启动新线程的行,并运行其余代码。
The following code is my failure implementation. 以下代码是我的失败实现。 I expect there is "return" in the output.
我希望输出中有“返回”。 In fact, only "start" is printed.
实际上,仅打印“开始”。 How can I fix this?
我怎样才能解决这个问题?
Thanks in advance for the help! 先谢谢您的帮助!
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;
}
First, to create a thead, you pass the constructor a runnable. 首先,要创建一个主题,请向构造函数传递一个可运行的对象。 What you did is trying to pass it the value that
myThread()
returns, instead of a method reference. 您所做的是尝试将
myThread()
返回的值传递给它,而不是方法引用。
(DO NOT) try this: (it may crash your system as it spawns an infinite amount of threads) (请勿)尝试以下操作:(它会产生大量线程,因此可能会导致系统崩溃)
public static void main(String[] args) throws InterruptedException{
System.out.println("start");
new Thread(() -> myThread()).start(); // Added () ->
System.out.println("return");
}
private static void myThread() throws InterruptedException{
System.out.println("start");
Thread.sleep(1000);
new Thread(() -> myThread()).start(); // Added () ->
System.out.println("return");
}
I also made it return void
since returning null is pointless at this point. 我还使其返回了
void
因为此时返回null是没有意义的。
Then, as pointed out, you need to limit the amount of threads created. 然后,如前所述,您需要限制创建的线程数量。 For example, if you want two threads:
例如,如果要两个线程:
private static final int numThreads = 0; // added
public static void main(String[] args) throws InterruptedException{
System.out.println("start");
new Thread(() -> myThread()).start();
System.out.println("return");
}
private static void myThread() throws InterruptedException{
System.out.println("start");
Thread.sleep(1000);
if (++numThreads < 2) // added
new Thread(() -> myThread()).start();
System.out.println("return");
}
Try this : 尝试这个 :
public static void main(String[] args) throws InterruptedException{
System.out.println("start");
new Thread(myThread()).start();
System.out.println("return");
return;
}
static boolean threadStared = false;
private static Runnable myThread() throws InterruptedException{
System.out.println("start");
Thread.sleep(1000);
if(!threadStared){
new Thread(myThread()).start();
threadStared = true;
}
System.out.println("return");
return null;
}
I want to start a thread within a thread with the same body.
我想在具有相同主体的线程内启动线程。 Once the new thread is created, I'd like to bypass the line starting up the new thread and run the rest of the code.
创建新线程后,我想绕过启动新线程的行,并运行其余代码。
Something like this: 像这样:
public class Test {
public class Worker extends Runnable {
private boolean launchedSubthread = false;
public void run() {
if (!launchedSubthread) {
launchedSubthread = true;
new Thread(this).start();
}
// Now do some stuff.
}
}
public void main(String[] args) {
new Thread(new Worker()).start();
}
}
Notice that when we launch the second child thread in the Worker
class, we are passing this
as the Runnable
. 注意,当我们在
Worker
类中启动第二个子线程时,我们将this
作为Runnable
传递。 So the two child threads will be sharing a single Worker
instance as their "body". 因此,两个子线程将共享一个
Worker
实例作为它们的“ body”。 (I assume that is what you are trying to do.) (我想这就是您要尝试做的。)
In you want the two threads to read or update other variables of the Worker
, then you must use volatile
or synchronized
appropriately. 如果希望两个线程读取或更新
Worker
其他变量,则必须使用volatile
或适当地synchronized
。 This does not apply to the way that I have used launchedSubthread
. 这不适用于我使用
launchedSubthread
。 This is because there is a happens before between the start()
call and the run()
call on the newly started thread. 这是因为在新启动的线程上,在
start()
调用和run()
调用之间发生了之前的事情 。
There were a couple of problems with your attempt. 您的尝试有两个问题。
myThread
is misnamed. myThread
的名称错误。 It is returning a Runnable
not a Thread
. 它返回的是
Runnable
而不是Thread
。
You weren't doing anything to prevent an infinite chain of creation of MyThread
instances. 您没有做任何事情来阻止
MyThread
实例创建的无限链。
You weren't actually creating any threads. 您实际上并没有创建任何线程。 If you look carefully at
myThread()
, it is recursing (infinitely) before any threads can be created. 如果仔细看一下
myThread()
,它可以无限myThread()
,然后可以创建任何线程。
The myThread()
call returns null
. myThread()
调用返回null
。 If that was actually passed to the Thread(Runnable)
constructor, you would get an NPE. 如果将其实际传递给
Thread(Runnable)
构造函数,则将获得一个NPE。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.