本帖最后由 邹海洋 于 2012-11-3 08:25 编辑
class StopThread implements Runnable
{
private boolean flag = true;
public synchronized void run()
{
while (flag)
{
try
{
wait(); //1.线程0进来,就挂在这里了
}//线程1不是就进不来了吗?怎么老师讲的t1,t2都挂这了?
catch (InterruptedException e)
{
}
System.out.println(Thread.currentThread()
.getName()+"....run");
}
}
public void change()
{
flag = false;
}
}
class StopThreadDemo
{
public static void main(String[] args)
{
StopThread st = new StopThread();
Thread t1 = new Thread(st);
Thread t2= new Thread(st);
t1.start();
t2.start();
int num = 0;
while (true)
{
if (num++ == 100)
{
st.change();
break;
}
System.out.println(Thread.currentThread().
getName()+".....'.....main"+num);
}
}
}
|
|