目标线程的join方法暂停当前线程,直到目标线程完成(从run()方法返回)。

 

Java代码:

package Threads;

import java.io.IOException;

/**
 * Created by Frank
 */
public class Join {
    public static void main(String[] args) {
        Thread t = new Thread() {
            public void run() {
                System.out.println("Reading");
                try {
                    System.in.read();
                } catch (IOException e) {
                    System.err.println(e);
                }
                System.out.println("Thread finished.");
            }
        };
        System.out.println("Starting");
        t.start();
        System.out.println("Joining");
        try {
            t.join();
        } catch (InterruptedException e) {
            // 不应该发生
            System.err.println("Who dares interrupt my sleep??");
        }
        System.err.println("Main Finished");
    }
}

 

相关文章:

  • 2021-12-12
  • 2022-02-22
  • 2021-12-12
  • 2021-12-12
  • 2021-05-27
  • 2021-12-12
  • 2021-12-12
  • 2021-12-12
猜你喜欢
  • 2021-06-29
  • 2021-12-12
  • 2022-12-23
  • 2021-12-05
  • 2022-12-23
  • 2021-09-02
相关资源
相似解决方案