线程 interrupt()方法简介

class T implements Runnable

{

   Thread t1,t2;

   T()

   {

      t1=new Thread(this);

      t2=new Thread(this);

    }

   public void run()

   {

      if(Thread.currentThread()==t2)

      {

         t1.interrupt();

 

      }

      if(Thread.currentThread()==t1)

      {

        System.out.println("线程t1开始运行!");

try{

          System.out.println("线程t1开始休眠10s!");

         t1.sleep(10000);

         }

      catch(InterruptedException e)

         {

           System.out.println("线程t1被吵醒了!");

          } 

      }

   }

}

class A 

{

   public static void main(String args[])

   {   

      T t=new T();

      t.t1.start();

      t.t2.start();

    }

}

posted @ 2016-05-12 22:10  榆次老城  阅读(269)  评论(0编辑  收藏  举报