标题:看一下这个程序吧,我有两个问题
只看楼主
Grace_TT
Rank: 1
等 级:新手上路
威 望:1
帖 子:324
专家分:0
注 册:2005-12-21
 问题点数:0 回复次数:5 
看一下这个程序吧,我有两个问题

public class JoinThread
{
public static void main(String args[])
{
ThreadTest t=new ThreadTest();
Thread pp=new Thread(t);
pp.start();
int i=0;
while(true)
{
if(i==100)

try
{
pp.join();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}

System.out.println("main Thread "+i++);
System.out.println("main Thread2 "+i);
}
}
}
class ThreadTest implements Runnable
{
private int tickets=100;
public void run()
{
while(true)
{
if(tickets>0)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
System.out.println(Thread.currentThread().getName()+"is saling ticket "+tickets--);
}
}
}
}

程序的运行结果:
main Thread2 1
main Thread 1
main Thread2 2
main Thread 2
main Thread2 3


main Thread 8
main Thread2 9
main Thread 9


main Thread 12
main Thread2 13
main Thread 13
Thread-0is saling ticket 100
main Thread2 14

main Thread 16
main Thread2 17
Thread-0is saling ticket 99
main Thread 17
main Thread2 18
main Thread 18

main Thread 21
main Thread2 22
Thread-0is saling ticket 98
main Thread 22
main Thread2 23

main Thread2 26
main Thread 26
main Thread2 27
Thread-0is saling ticket 97
main Thread 27
main Thread2 28

main Thread2 31
main Thread 31
main Thread2 32
Thread-0is saling ticket 96
main Thread 32
main Thread2 33

main Thread2 36
main Thread 36
main Thread2 37
Thread-0is saling ticket 95
main Thread 37
main Thread2 38

main Thread2 41
main Thread 41
main Thread2 42
Thread-0is saling ticket 94
main Thread 42
main Thread2 43

main Thread2 46
main Thread 46
main Thread2 47
Thread-0is saling ticket 93
main Thread 47
main Thread2 48

main Thread 51
main Thread2 52
Thread-0is saling ticket 92
main Thread 52
main Thread2 53

main Thread 56
main Thread2 57
Thread-0is saling ticket 91
main Thread 57
main Thread2 58
main Thread2 62

Thread-0is saling ticket 90
main Thread 62
main Thread2 63

main Thread2 66
main Thread 66
main Thread2 67
Thread-0is saling ticket 89
main Thread 67
main Thread2 68
main Thread 68
main Thread2 69
main Thread 69
main Thread2 70
main Thread 70
Thread-0is saling ticket 88
main Thread2 71
main Thread 71
Thread-0is saling ticket 87
main Thread2 72
main Thread 72

main Thread 75
main Thread2 76
main Thread 76
Thread-0is saling ticket 86
main Thread2 77
main Thread 77
main Thread 80

main Thread2 81
main Thread 81
Thread-0is saling ticket 85
main Thread2 82
main Thread 82
main Thread2 83
main Thread 83
main Thread2 84
main Thread 84
main Thread2 85
main Thread 85
main Thread2 86
Thread-0is saling ticket 84
main Thread 86
main Thread2 87

main Thread 90
main Thread2 91
main Thread 91
Thread-0is saling ticket 83
main Thread2 92
main Thread 92

main Thread2 96
main Thread 96
Thread-0is saling ticket 82
main Thread2 97
main Thread 97
main Thread2 98
main Thread 98
main Thread2 99
main Thread 99
main Thread2 100
Thread-0is saling ticket 81
Thread-0is saling ticket 80
Thread-0is saling ticket 79
Thread-0is saling ticket 78
Thread-0is saling ticket 77



Thread-0is saling ticket 5
Thread-0is saling ticket 4
Thread-0is saling ticket 3
Thread-0is saling ticket 2
Thread-0is saling ticket 1

问题:
1。我想知道为什么程序在调用了pp.join()后,程序应该返回到main()中继续执行,也就是接下来的System.out.println("main Thread "+i++);和System.out.println("main Thread2 "+i);语句;但是并没继续执行,程序也没有结束而是好像是死在哪里了,因为运行结果并没显示:Press any key to continue... 这是怎么回事呢?
2。我明明是将System.out.println("main Thread2 "+i);写在System.out.println("main Thread "+i++);之前的,应该先显示main Thread再显示main Thread2才对,可为什么先显示的main Thread2呢?

这个程序要怎么修改呢??谢谢了!!!

搜索更多相关主题的帖子: Thread main println System 
2006-02-01 13:58
枫江沧月
Rank: 1
等 级:新手上路
帖 子:59
专家分:0
注 册:2005-11-17
得分:0 
你好,我看了你的程序,先回答你的第二个问题,并不是像你说的先显示main Thread2,而是你的程序运行结果太多,窗口容纳不下,就把第一个main Thread顶到最上面了,你是看不见的,你可以把i==100,改为i==10或i==20,你就能看到了。

第一个问题是由于你的润run()方法是一个死循环,所以程序并没有结束,而是当ticke<=0以后,就什么也不做了,这是因为join()这个方法是让main()方法无限期的等待run()方法结束,然后继续运行main()方法,你可以用join(long)这个方法进行测试就明白了,join(long)是让main()方法等待一定时间后继续运行,建议为join(5000),就是5秒。

按照下面的改法也许能达到你的想法
注:我不知道你想干什么,我觉得你是想测试join()这个方法

public class JoinThread
{
public static void main(String args[])
{
ThreadTest t=new ThreadTest();
Thread pp=new Thread(t);
pp.start();
int i=0;
while(i<50)////////////////////////////////////////
{
if(i==20)
{
try
{

pp.join();

}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
System.out.println("main Thread "+i++);
System.out.println("main Thread2 "+i);
}
}
}
class ThreadTest implements Runnable
{
private int tickets=100;
public void run()
{

while(true)
{
if(tickets>0)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
System.out.println(Thread.currentThread().getName()+"is saling ticket "+tickets--);
}
else return; ////////////////////////////////////
}
}
}

20年后超过比尔盖茨
2006-02-04 16:51
Grace_TT
Rank: 1
等 级:新手上路
威 望:1
帖 子:324
专家分:0
注 册:2005-12-21
得分:0 
呵呵,楼上的你好厉害呀!

注:我不知道你想干什么,我觉得你是想测试join()这个方法
---------你觉得的是对的。
2006-02-04 22:05
250697812
Rank: 1
等 级:新手上路
帖 子:112
专家分:0
注 册:2005-10-5
得分:0 
不错啊!!!!

2006-02-10 10:27
神的孩子
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-2-11
得分:0 

高手..不要跟你学啊!

2006-02-11 15:59
神的孩子
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-2-11
得分:0 

高手..还要跟你学啊!

2006-02-11 15:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-44287-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.364945 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved