简体   繁体   English

在Runnable中启动Runnable?

[英]Start a Runnable within a Runnable?

Ok my questions could sound confusing but it's actually very simple. 好吧,我的问题可能听起来令人困惑,但实际上非常简单。 I have a Runnable that starts another Runnable instance. 我有一个Runnable启动另一个Runnable实例。 So basically: 所以基本上:

runnable1 -> runnable2 runnable1 - > runnable2

Does runnable1 stay alive as long as runnable2 is alive or does runnable1 finish when runnable2 is started? 只要runnable2处于活动状态,runnable1是否保持活动状态,或者runnable2启动时runnable1是否完成?

Runnable runnable1;
Runnable runnable2;

runnable1 = new Runnable()
    {
        public void run()
        {
            runnable2 = new Runnable()
            {
                public void run()
                {
                    //Code here
                }
            };
            (new Thread(runnable2)).start();//Now that runnable 2 is started, does runnable 1 temrinate??
        }
    };

(new Thread(runnable1)).start();//This starts first

A Thread is independent of the code that started it. Thread独立于启动它的代码。 So in your code, even if runnable2 is still running, the Thread executing runnable1 will stop once the end of run() has been reached. 因此,在您的代码中,即使runnable2仍在运行,执行runnable1Thread也会在达到run()结束时停止。

If that isn't what you want, then you should use Thread.join() on the other thread. 如果那不是你想要的,那么你应该在另一个线程上使用Thread.join()

Short answer is no. 简短的回答是否定的。

Long answer. 答案很长。 Threads execute independently. 线程独立执行。 Just like you have a main method and you start a thread in it, the main method will just call start and continue thread will run independently. 就像你有一个main方法,并在其中启动一个threadmain方法将只调用start并continue线程将独立运行。

If you want runnable1 to wait until runnable2 finishes. 如果你想让runnable1等到runnable2完成。 the you can use join() 你可以使用join()

To be more clear 更清楚

"Does runnable1 stay alive as long as runnable2 is alive or does runnable1 finish when runnable2 is started?" “只要runnable2处于活动状态,runnable1是否保持活动状态,或者runnable2启动时runnable1是否完成?”

, either is not correct it depends on which thread gets CPU time first. ,要么是不正确的,这取决于哪个线程首先获得CPU时间。

Not an answer (others have already done that) but,... 不是答案(其他人已经这样做了)但是......

...You said "Runanble" but your question really is about threads. ...你说“Runanble”但你的问题确实是关于线程的。 Runnable is an interface. Runnable是一个界面。 If an object implements Runnable, it merely means that the object has a public void run() method. 如果对象实现Runnable,则仅表示该对象具有public void run()方法。 It doesn't mean anything else. 这并不意味着什么 If it's a class that you wrote, then the run() method will do what you wrote it to do, nothing more, and nothing less. 如果它是你编写的类,那么run()方法将执行你所写的操作,仅此而已。

If you give your Runnable object to a new Thread, and then you call the thread's start() function, that's where things get interesting. 如果你将Runnable对象赋予一个新的Thread,然后你调用线程的start()函数,这就是事情变得有趣的地方。 But before I get to that, It's important for you to know the difference between a thread (little t), and a Thread (big T) object. 但在我开始之前,了解线程(小t)和Thread (大T)对象之间的区别非常重要。 A little-t thread is an independent path of execution through your program's code. 一个小t线程是通过程序代码执行的独立路径。 A big-T Thread is a Java library object that can be used to create and manage a little-t thread. big-T Thread是一个Java库对象,可用于创建和管理一个小线程。

The little-t thread does not exist until you call t.start(). 在调用t.start()之前,little-t线程不存在。 Once it's started, it will do some initialization, and then it will call your run() method, and then if and when your run() method returns, it will do some cleanup before it ceases to exist. 一旦它启动,它将进行一些初始化,然后它将调用你的run()方法,然后如果你的run()方法返回,它将在它不再存在之前进行一些清理。


As the other answers have already mentioned, the Java language does not recognize any kind of parent/child relationship between threads. 正如其他答案已经提到的那样,Java语言无法识别线程之间的任何类型的父/子关系。 When some Thread object, t, exists, any other thread that happens to know about it can wait for its thread to die by calling t.join()... 当一些Thread对象t存在时,碰巧知道它的任何其他线程可以通过调用t.join()来等待其线程死掉...

...or not. ...或不。 It's entirely up to you. 这完全取决于你。 If your code is written to respect a parent/child relationship, you can do that. 如果您的代码是为了尊重父/子关系而编写的,那么您可以这样做。 It's actually a pretty common practice, but it is not forced on you by the language. 这实际上是一种非常常见的做法,但语言并没有强迫你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM