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

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
Grace_TT
Rank: 1
等 级:新手上路
威 望:1
帖 子:324
专家分:0
注 册:2005-12-21
得分:0 
呵呵,楼上的你好厉害呀!

注:我不知道你想干什么,我觉得你是想测试join()这个方法
---------你觉得的是对的。
2006-02-04 22:05



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




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

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