當幸福來敲門

          我就會牢牢抓住!
          隨筆 - 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 閱讀(207) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 天峻县| 上虞市| 邮箱| 靖远县| 镇安县| 湖南省| 阿瓦提县| 鹤山市| 周口市| 通河县| 长寿区| 外汇| 怀安县| 朔州市| 合江县| 纳雍县| 腾冲县| 甘洛县| 昆明市| 台江县| 乌恰县| 景洪市| 武冈市| 扬州市| 阿拉善左旗| 革吉县| 曲水县| 弋阳县| 娄烦县| 广平县| 衡南县| 台北市| 文成县| 临高县| 芜湖县| 吴川市| 河池市| 广汉市| 葵青区| 吉林省| 岱山县|