代码如下,想知道为什么main方法中的if语句无法执行?想的是让if判断程序是否结束,但是程序好像一直结束不了classLeftextendsThread{intn=0;publicvoidrun(){while(true){n++;System...
代码如下,想知道为什么main方法中的if语句无法执行?想的是让if判断程序是否结束,但是程序好像一直结束不了
class Left extends Thread
{
int n = 0;
public void run()
{
while(true)
{
n++;
System.out.printf("\n%s","Left");
try
{
sleep((int)(Math.random()*100)+100);
}
catch(InterruptedException e) {}
}
}
}
class Right extends Thread
{
int n = 0;
public void run()
{
while(true)
{
n++;
System.out.printf("\n%40s","Right");
try
{
sleep((int)(Math.random()*100)+100);
}
catch(InterruptedException e){}
}
}
}
public class Example8_3
{
public static void main(String args[])
{
Left left = new Left();
Right right = new Right();
left.start();
right.start();
while(true)
{
if(left.n>=8 || right.n>=8) // 这一句好像永远也运行不到
System.exit(0);
}
}
}
展开