zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Java通过join方法来暂停当前线程详解编程语言

JAVA方法线程编程语言 详解 通过 当前 Join
2023-06-13 09:20:45 时间

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

}

 

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/15855.html

cjava