java--多线程课程源码4

上一篇 / 下一篇  2010-11-07 21:24:10 / 个人分类:JAVA

class TestThread
{
 public static void main(String[] args)
 {
  Thread1 t1=new Thread1();
  t1.start();
  int index=0;
  while(true)
  {
   if(index++==500)
   {
    t1.stopThread();
    t1.interrupt();
    break;
   }
   System.out.println(Thread.currentThread().getName());
  }
  System.out.println("main() exit");
 }
}

class Thread1 extends Thread
{
 private boolean bStop=false;
 public synchronized void run()
 {
  while(!bStop)
  {
   try
   {
    wait();
   }
   catch(InterruptedException e)
   {
    //e.printStackTrace();
    if(bStop)
     return;
   }
   System.out.println(getName());
  }
 }
 public void stopThread()
 {
  bStop=true;
 }
}


TAG:

 

评分:0

我来说两句

日历

« 2024-04-30  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 85880
  • 日志数: 218
  • 书签数: 1
  • 建立时间: 2010-11-06
  • 更新时间: 2011-03-21

RSS订阅

Open Toolbar