class Test implements Runnable
{
int num=0;
public synchronized void run()
{
while(true)
{
if(num==20)
return;//完全可以把子线程完全控制住,使其终止。
System.out.println(Thread.currentThread().getName+num);
num++;
}
}
}
public class Demo
{
public static void main(String[] args)
{
Test t=new Test();
new Thread(t).start();
new Thread(t).start();
}
}
但是如下方式就不行: class Test implements Runnable
{
int num=0;
public synchronized void run()
{
while(true)
{ if(num++==20) return;//不可以把子线程完全控制住,使其终止!!郁闷!!!!求大神!!!。
System.out.println(Thread.currentThread().getName+num);
}
}
}
public class Demo
{
public static void main(String[] args)</P>
{
Test t=new Test();
new Thread(t).start();
new Thread(t).start();
} 作者: 刘海芳 时间: 2013-6-8 11:36 本帖最后由 刘海芳 于 2013-6-8 11:39 编辑