Thread 的join的实现中有点迷惑的地方

	
package test;

/**
* Created by IntelliJ IDEA.
* Date: 11/06/28
* Time: 10:26
*/
public class IsThreadPool {
public static void main(String[] args) {
System.out.println("begin >>> " + Thread.currentThread().getName() + " isAlive: " + Thread.currentThread().isAlive());
ChildThread child1 = new ChildThread();
child1.start();
try {
child1.joinExtend(0);
// child1.join(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("end >>> " + Thread.currentThread().getName() + " isAlive: " + Thread.currentThread().isAlive());
}

}

class ChildThread extends Thread {
public void run() {
int i = 4;
while (--i >= 0) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("==============================================================");
System.out.println(Thread.currentThread().getName() + " isAlive: " + Thread.currentThread().isAlive());
ThreadGroup tg = Thread.currentThread().getThreadGroup();
System.out.println(tg.toString());
Thread[] threads = new Thread[tg.activeCount()];
tg.enumerate(threads);
for (Thread t : threads) {
System.out.println(" ThreadGroup_child: " + t.getName() + " state: " + t.getState());
}
tg = null;

}


}

/**
* 对Thread的join作修改,看看变量情况,
* isAlive():测试线程(并不是当前线程)是否处于活动状态。如果线程已经启动且尚未终止,则为活动状态。
* wait:在其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者超过指定的时间量前,导致当前线程等待
* @param millis
* @throws InterruptedException
*/
public final synchronized void joinExtend(long millis)
throws InterruptedException {

long base = System.currentTimeMillis();
long now = 0;
boolean isAlive = this.isAlive();
while (isAlive = this.isAlive()) {
System.out.println("joinExtend_currentThread---> " + Thread.currentThread().getName());
System.out.println("this ---> " + this.getName() + " alive---> " + isAlive + " currentThread---> " + Thread.currentThread().getName());
this.wait(0);
}

}
}



结果:

Connected to the target VM, address: '127.0.0.1:1759', transport: 'socket'
begin >>> main isAlive: true
joinExtend_currentThread---> main
this ---> Thread-0 alive---> true currentThread---> main
==============================================================
Thread-0 isAlive: true
java.lang.ThreadGroup[name=main,maxpri=10]
ThreadGroup_child: main state: WAITING
ThreadGroup_child: Thread-0 state: RUNNABLE
==============================================================
Thread-0 isAlive: true
java.lang.ThreadGroup[name=main,maxpri=10]
ThreadGroup_child: main state: WAITING
ThreadGroup_child: Thread-0 state: RUNNABLE
==============================================================
Thread-0 isAlive: true
java.lang.ThreadGroup[name=main,maxpri=10]
ThreadGroup_child: main state: WAITING
ThreadGroup_child: Thread-0 state: RUNNABLE
==============================================================
Thread-0 isAlive: true
java.lang.ThreadGroup[name=main,maxpri=10]
ThreadGroup_child: main state: WAITING
ThreadGroup_child: Thread-0 state: RUNNABLE
end >>> main isAlive: true
Disconnected from the target VM, address: '127.0.0.1:1759', transport: 'socket'

Process finished with exit code 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值