Java Multithreading concept and join() method

后端 未结 12 1421
太阳男子
太阳男子 2020-11-28 01:39

I\'m confused in join() method used in Threads in Java. In the following code:

// Using join() to wait for threads to finish.
class NewThread im         


        
12条回答
  •  感动是毒
    2020-11-28 02:05

    First of all, when you create ob1 then constructor is called and it starts execution. At that time t.start() also runs in separate thread. Remember when a new thread is created, it runs parallely to main thread. And thats why main start execution again with next statement.

    And Join() statement is used to prevent the child thread from becoming orphan. Means if you did'nt call join() in your main class, then main thread will exit after its execution and child thread will be still there executing the statements. Join() will wait until all child thread complete its execution and then only main method will exit.

    Go through this article, helps a lot.

提交回复
热议问题