今天研究了一下,join()方法在API中的解释是“等待线程终止”,那等待的线程当然是main thread。 在main thread 中创建的 t 和 t2 thread,都是独立的,所以当t join 到main thread 中,t2 依然执行自己的,并不会停止。
解决方案 »
- jdom解析XML 出现的蛋疼问题 求高手进来帮忙解答下
- struts实现多上传
- 用Tomcat调试jsp程序出现如下:“cannot be resolved” ,请高手帮帮忙阿,急
- 急!!!!!我想知道ResultSet换做LIST怎么改啊??????
- 在线等——我再servlet里查询到一个结果集?怎么封装成bean,然后再jsp里调用这个bean输出所有的数据?
- 大家好,我做了个web应用,直接放在tomcat的root目录中,访问地址为http://localhost:8080,后台管理为http://localhost:8080/admin,但是为什
- 在JTable单元格中实现可选择的JComboBox,并能取得其值,谁能给我个例子?
- 关于hibernate多表之间的映射问题!
- 用JDOM处理XML,却出现如下错误,在JB7,如何找org.xml的包呢?急……
- 新手!!Hibernate查询的问题,求大神们指点一下!!
- EXTJS+Struts2异常处理怎么做
- Spring+Hibernate整合后的问题
那这个例子中,t2 thread 应该不会被t thread 影响的哦
1 t2
0 t
2 t2
3 t2
4 t2
5 t2
6 t2
7 t2
8 t2
9 t2
1 t
2 t
3 t
4 t
5 t
6 t
7 t
8 t
9 t
main thread: 0
main thread: 1
main thread: 2
main thread: 3
main thread: 4这是循环10次我跑的结果,循环1000次就乱套了,3个线程在竞争资源
我凌乱了,不知道要得出了神马结论。
没有3个线程在竞争呀,main thread 是等待t thread 结束之后运行的,这没错。t thread 和 t2 thread 它们是相互独立运行,但是打印的时间有快有慢,所以看到的结果好像是t 和 t2 thread 在相互竞争。
Thread原码里,是这样定义的 : public final void join() throws InterruptedException {
join(0);
}
。。
public final synchronized void join(long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0; if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
} if (millis == 0) {
while (isAlive()) {
wait(0);//这里运行的wait
}
} else {
。