📜  在Java中加入线程

📅  最后修改于: 2022-05-13 01:54:44.997000             🧑  作者: Mango

在Java中加入线程

Java.lang.Thread类提供了 join() 方法,该方法允许一个线程等待另一个线程完成其执行。如果t是其线程当前正在执行的 Thread 对象,则t.join()将确保t在程序执行下一条指令之前终止。
如果有多个线程调用 join() 方法,这意味着连接上的重载允许程序员指定等待时间。但是,与 sleep 一样,join 的时间取决于操作系统,因此您不应假设 join 将等待您指定的时间。
有三个重载的连接函数。

  1. join():它将使当前线程处于等待状态,直到调用它的线程死亡。如果线程被中断,那么它将抛出 InterruptedException。
    句法:
    public final void join()
    
  2. join(long millis) :它将使当前线程处于等待状态,直到调用它的线程死亡或等待指定的时间(毫秒)。
    句法:
    public final synchronized void join(long millis)
    
  3. join(long millis, int 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 has 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 has 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