Java.lang.Thread提供了join()方法,该方法允许一个线程等到另一个线程完成其执行。如果t是当前正在执行其线程的Thread对象,那么t.join(); 它导致当前线程暂停其执行,直到它加入的线程完成其执行。
如果有多个线程调用join()方法,则表示连接上的重载允许程序员指定等待期。但是,与sleep一样,join依赖于OS进行计时,因此您不应该假设join将等待您指定的时间。
有三个重载的连接函数。

在Java多线程中,java.lang.Thread类的join()方法

  1. join():它将使当前线程等待,直到调用它的线程为空。如果线程被中断,那么它将抛出InterruptedException。
    句法:

    public final void join()
    
  2. join(long millis):它将使当前线程等待,直到调用它的线程为dead或等待指定的时间(毫秒)。
    句法:

    public final synchronized void join(long millis)
    
  3. join(long millis,int nanos):它将使当前线程等待,直到调用它的线程死或等待指定的时间(毫秒+ nanos)。
    句法:

    public final synchronized void join(long millis,int nanos)
    
    // Java program to explain the
    // concept of joining a thread.
    import java.io.*;
    
    // Creating thread by creating the
    // objects of that class
    class ThreadJoining extends Thread
    {
        @Override
        public void run()
        {
            for (int i = 0; i < 2; i++)
            {
                try
                {
                    Thread.sleep(500);
                    System.out.println("Current Thread: "
                            + Thread.currentThread().getName());
                }
    
                catch(Exception ex)
                {
                    System.out.println("Exception has" +
                                    " been caught" + ex);
                }
                System.out.println(i);
            }
        }
    }
    
    class GFG
    {
        public static void main (String[] args)
        {
    
            // creating two threads
            ThreadJoining t1 = new ThreadJoining();
            ThreadJoining t2 = new ThreadJoining();
            ThreadJoining t3 = new ThreadJoining();
    
            // thread t1 starts
            t1.start();
    
            // starts second thread after when
            // first thread t1 is died.
            try
            {
                System.out.println("Current Thread: "
                      + Thread.currentThread().getName());
                t1.join();
            }
    
            catch(Exception ex)
            {
                System.out.println("Exception has " +
                                    "been caught" + ex);
            }
    
            // t2 starts
            t2.start();
    
            // starts t3 after when thread t2 is died.
            try
            {
                System.out.println("Current Thread: "
                     + Thread.currentThread().getName());
                t2.join();
            }
    
            catch(Exception ex)
            {
                System.out.println("Exception has been" +
                                        " caught" + ex);
            }
    
            t3.start();
        }
    }
    
    

    输出:

    Current Thread: main
    Current Thread: Thread-0
    0
    Current Thread: Thread-0
    1
    Current Thread: main
    Current Thread: Thread-1
    0
    Current Thread: Thread-1
    1
    Current Thread: Thread-2
    0
    Current Thread: Thread-2
    1

在上面的例子中,我们可以清楚地看到第二个线程t2在第一个线程t1死亡后开始,而t3将在第二个线程t2死亡后开始执行。

 
转载请保留页面地址:https://www.breakyizhan.com/java/4956.html