当幸福来敲门

我就会牢牢抓住!
随笔 - 50, 文章 - 3, 评论 - 8, 引用 - 0
数据加载中……

线程等待问题处理

class MyThread implements Runnable {
@Override
public void run() {
  System.out.println("1、进入run()方法休眠");
  try {
   System.out.println("2、线程休眠20秒");
   Thread.sleep(20000);//这里休眠20秒
   System.out.println("3、线程正常休眠完毕");
  } catch (InterruptedException e) {
   System.out.println("4、线程发生异常休眠被中断");
   return;//返回方法调用处
  }
  System.out.println("5、线程正常结束run()方法体");
}
}
public class InterruptDemo {

public static void main(String[] args) {
  MyThread mt = new MyThread();
  Thread t = new Thread(mt,"线程A");
  t.start();//启动线程
//========================================================
  try {
   Thread.sleep(2000);  //保证线程至少执行2秒
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
//========================================================
  t.interrupt();//中断线程
}
}

posted on 2012-06-21 15:21 wyx 阅读(189) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: