关于Thread.sleep休眠

fbmgua 2012-11-28 11:39:10
程序:
class Test{
public static void main(String[] args){
TestThread t = new TestThread();
new Thread(t).start();
try{Thread.sleep(1000);}catch(Exception e){}
t.str = new String("method");
new Thread(t).start();
System.out.println(Thread.currentThread().getName()
+ " is running");
}
}
class TestThread implements Runnable{
private int tickets = 20;
String str = new String("");
public void run(){
if(str.equals("method")){
while(true){
sale();
}
}
else{
synchronized(str){
if(tickets > 0){
try{
Thread.sleep(10);
}
catch(Exception e){
System.out.println(e.getMessage());
}
System.out.println("synchronized(str) is running");
System.out.println(Thread.currentThread().getName()
+" is salling ticket " + tickets--);
}
}
}
}
public synchronized void sale(){
if(tickets > 0){
try{
Thread.sleep(10);
}
catch(Exception e){
System.out.println(e.getMessage());
}
System.out.println("method");
System.out.println(Thread.currentThread().getName()
+ " is salling ticket " + tickets--);
}
}
}
输出:
synchronized(str) is running
Thread-0 is salling ticket 20
main is running
method
Thread-1 is salling ticket 19
method
Thread-1 is salling ticket 18
method
Thread-1 is salling ticket 17
method
Thread-1 is salling ticket 16
method
Thread-1 is salling ticket 15
method
Thread-1 is salling ticket 14
method
Thread-1 is salling ticket 13
method
Thread-1 is salling ticket 12
method
Thread-1 is salling ticket 11
method
Thread-1 is salling ticket 10
method
Thread-1 is salling ticket 9
method
Thread-1 is salling ticket 8
method
Thread-1 is salling ticket 7
method
Thread-1 is salling ticket 6
method
Thread-1 is salling ticket 5
method
Thread-1 is salling ticket 4
method
Thread-1 is salling ticket 3
method
Thread-1 is salling ticket 2
method
Thread-1 is salling ticket 1
问:
1.Thread.sleep休眠的是当前的线程,在上面程序中休眠的就是主线程main,
同时导致了线程Thread-1休眠期不能开启,这里的分析正确?
2.如果上面正解的话,try{Thread.sleep(1000);}catch(Exception e){}
这句代码为什么不能保证tickets在线程Thread-0中被完全售出?
...全文
184 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
fbmgua 2012-11-28
  • 打赏
  • 举报
回复
引用 6 楼 geminit2011 的回复:
是的,估计题意理解有点偏差……
嗯,你应该没看清楚吧, 我也是没看清楚,if没在循环里,我改成了 while(tickets > 0)就好了。 还是谢谢你,哈哈
Geminit 2012-11-28
  • 打赏
  • 举报
回复
是的,估计题意理解有点偏差……
fbmgua 2012-11-28
  • 打赏
  • 举报
回复
引用 3 楼 fbmgua 的回复:
引用 1 楼 geminit2011 的回复:1.你的代码Java code?1try{Thread.sleep(1000);}catch(Exception e){}是写在主线程Main当中的,所以休眠的是主线程。而Thread-1是线程,他是异步执行,不受主线程的影响,按照你定义的Java code?123while (true) { sale(……
这里开启Thread-1 的代码在主线程中,而且是在sleep之后。
fbmgua 2012-11-28
  • 打赏
  • 举报
回复
引用 2 楼 ldh911 的回复:
1.Thread.sleep休眠的是当前的线程,在上面程序中休眠的就是主线程main, 同时导致了线程Thread-1休眠期不能开启,这里的分析正确? ——正确 2.如果上面正解的话,try{Thread.sleep(1000);}catch(Exception e){} 这句代码为什么不能保证tickets在线程Thread-0中被完全售出……
对啊,我把 else{ synchronized(str){ if(tickets > 0)//改成while(tickets > 0)就达到我想要的效果了!
fbmgua 2012-11-28
  • 打赏
  • 举报
回复
引用 1 楼 geminit2011 的回复:
1.你的代码Java code?1try{Thread.sleep(1000);}catch(Exception e){}是写在主线程Main当中的,所以休眠的是主线程。而Thread-1是线程,他是异步执行,不受主线程的影响,按照你定义的Java code?123while (true) { sale(); } 售出。Thread-……
主线程Main休眠的时候Thread-1就已经无法开启了吧?
MiceRice 2012-11-28
  • 打赏
  • 举报
回复
1.Thread.sleep休眠的是当前的线程,在上面程序中休眠的就是主线程main, 同时导致了线程Thread-1休眠期不能开启,这里的分析正确? ——正确 2.如果上面正解的话,try{Thread.sleep(1000);}catch(Exception e){} 这句代码为什么不能保证tickets在线程Thread-0中被完全售出? ——你的代码逻辑就是这么控制的啊! public void run(){ if (str.equals("method")){ // Thread-0执行到此时,str还是空字符串,所以if不成立 while(true){sale();} // 循环卖掉所有票 } else{ synchronized(str){ if(tickets > 0){ // 只卖掉一张,if并不循环 ....
Geminit 2012-11-28
  • 打赏
  • 举报
回复
1.你的代码
try{Thread.sleep(1000);}catch(Exception e){}
是写在主线程Main当中的,所以休眠的是主线程。而Thread-1是线程,他是异步执行,不受主线程的影响,按照你定义的
	while (true) {
				sale();
			}
售出。Thread-1不会受到主线程的影响,因为他们是异步的。 2.你的票是已经卖完了为tickets=0,之所以打印出来是1,是因为是你用了tickets--,打印tickets=1时,他就执行tickets--,结果tickets=0,则下一次执行不满足if (tickets > 0) {所以不会执行。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧