当前位置:主页 > 软件编程 > JAVA代码 >

Java使用join方法暂停当前线程

时间:2021-06-11 08:11:00 | 栏目:JAVA代码 | 点击:

目标线程的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");
 }
}

您可能感兴趣的文章:

相关文章