How does join() work in java? Does it guarantee the execution before main()?

后端 未结 2 606
孤独总比滥情好
孤独总比滥情好 2020-12-31 21:48

I am trying to understand the code flow with join().

public class Multi extends Thread {

    public void run() {
        for (int i = 0; i < 5; i++) {
           


        
2条回答
  •  伪装坚强ぢ
    2020-12-31 22:42

    Joins

    The join method allows one thread to wait for the completion of another. If t is a Thread object whose thread is currently executing,

    t.join(); causes the current thread to pause execution until t's thread terminates. Overloads of join allow the programmer to specify a waiting period. However, as with sleep, join is dependent on the OS for timing, so you should not assume that join will wait exactly as long as you specify.

    Like sleep, join responds to an interrupt by exiting with an InterruptedException.

提交回复
热议问题