简体   繁体   中英

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. So basically:

runnable1 -> runnable2

Does runnable1 stay alive as long as runnable2 is alive or does runnable1 finish when runnable2 is started?

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. So in your code, even if runnable2 is still running, the Thread executing runnable1 will stop once the end of run() has been reached.

If that isn't what you want, then you should use Thread.join() on the other thread.

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.

If you want runnable1 to wait until runnable2 finishes. the you can use join()

To be more clear

"Does runnable1 stay alive as long as runnable2 is alive or does runnable1 finish when runnable2 is started?"

, either is not correct it depends on which thread gets CPU time first.

Not an answer (others have already done that) but,...

...You said "Runanble" but your question really is about threads. Runnable is an interface. If an object implements Runnable, it merely means that the object has a public void run() method. 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.

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. 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. A little-t thread is an independent path of execution through your program's code. A big-T Thread is a Java library object that can be used to create and manage a little-t thread.

The little-t thread does not exist until you call t.start(). 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.


As the other answers have already mentioned, the Java language does not recognize any kind of parent/child relationship between threads. 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()...

...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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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