方法Join 是干啥用的? 简单回答,同步,如何同步? 怎么实现的? 下面将逐个回答。
自从接触Java 多线程,一直对Join 理解不了。JDK 是这样说的:join public final void join (long millis )throws InterruptedException Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever. 大家能理解吗? 字面意思是等待一段时间直到这个线程死亡,我的疑问是那个线程,是它本身的线程还是调用它的线程的,上代码:
package concurrentstudy;
/**
*
* @author vma
*/
public class JoinTest {
public static void main(String[] args) {
Thread t = new Thread( new RunnableImpl());
t.start();
try {
t.join(1000) ; // 主线程只等1 秒,不管子线程什么时候结束
System.out.println("joinFinish");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class RunnableImpl implements Runnable {
@Override
public void run() {
try {
System.out.println("Begin sleep");
Thread.sleep(1000);
System.out.println("End sleep");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Java中join方法介绍与用法
分享到:
正在招生
友情链接