春風(fēng)博客

          春天里,百花香...

          導(dǎo)航

          <2007年11月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          統(tǒng)計(jì)

          公告

          MAIL: junglesong@gmail.com
          MSN: junglesong_5@hotmail.com

          Locations of visitors to this page

          常用鏈接

          留言簿(11)

          隨筆分類(224)

          隨筆檔案(126)

          個(gè)人軟件下載

          我的其它博客

          我的鄰居們

          最新隨筆

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          限時(shí)線程回調(diào)方式的實(shí)現(xiàn)

          線程回調(diào)方式我們已經(jīng)在"使用回調(diào)和線程處理一個(gè)耗時(shí)響應(yīng)過程"文中進(jìn)行了講述,但是有些情況下用戶希望在指定時(shí)間內(nèi)返回一個(gè)結(jié)果,免得無休止的等待下去.這時(shí)我們需要使用"限時(shí)線程回調(diào)方式",它在原有線程回調(diào)的基礎(chǔ)上加上了一個(gè)Timer以計(jì)算消耗的時(shí)間,如果時(shí)間期限到了任務(wù)還沒有執(zhí)行完的話即中斷線程,示例代碼如下:

          package com.sitinspring;

          import java.awt.event.ActionEvent;
          import java.awt.event.ActionListener;

          import javax.swing.Timer;

          /**
           * 定時(shí)回調(diào)線程類
           * 
           * 
          @author sitinspring(junglesong@gmail.com)
           * 
           * @date 2007-11-6
           
          */

          public class TimedCallBackThread implements Runnable {
              
          // 一秒的毫秒數(shù)常量
              private final static int ONE_SECOND = 1000;

              
          // 限制時(shí)間,以秒為單位
              private final int waitTime;

              
          // 已經(jīng)流逝的時(shí)間
              private int passedTime;

              
          private Timer timer;

              
          private Thread thread;

              
          private MvcTcModel model;

              
          private MvcTcView view;

              
          public TimedCallBackThread(MvcTcModel model, MvcTcView view, int waitTime) {
                  
          this.model = model;
                  
          this.view = view;
                  
          this.waitTime = waitTime;
                  
          this.passedTime = 0;

                  
          // 創(chuàng)建并啟動(dòng)定時(shí)器
                  timer = new Timer(ONE_SECOND, new ActionListener() {
                      
          public void actionPerformed(ActionEvent evt) {
                          timeListener();
                      }

                  }
          );
                  timer.start();

                  
          // 創(chuàng)建并啟動(dòng)線程來完成任務(wù)
                  thread = new Thread(this);
                  thread.start();
              }


              
          private void timeListener() {
                  passedTime
          ++;

                  
          // 動(dòng)態(tài)顯示狀態(tài)
                  int modSeed = passedTime % 3;
                  
          if (modSeed == 0{
                      view.getLabel2().setText(
          "響應(yīng)中");
                  }
           else if (modSeed == 1{
                      view.getLabel2().setText(
          "響應(yīng)中..");
                  }
           else if (modSeed == 2{
                      view.getLabel2().setText(
          "響應(yīng)中.");
                  }


                  
          // 如果流逝時(shí)間大于規(guī)定時(shí)間則中斷線程
                  if (passedTime > waitTime) {
                      passedTime 
          = waitTime;
                      thread.interrupt();
                  }

              }


              
          public void run() {
                  
          while (passedTime < waitTime) {
                      
          try {
                          Thread.sleep(
          10000);// 模擬一個(gè)耗時(shí)相應(yīng)過程
                          timer.stop();// 任務(wù)完成,停止Timer

                          view.getLabel2().setText(model.getText2());
                      }
           catch (InterruptedException ex) {
                          timer.stop();
          // 線程中斷,停止Timer
                          view.getLabel2().setText("在指定時(shí)間內(nèi)未響應(yīng)");
                      }
           catch (Exception ex) {
                          ex.printStackTrace();
                      }


                      
          return;
                  }

              }

          }

          執(zhí)行效果如下:





          本文代碼下載(點(diǎn)擊第二個(gè)按鈕):
          http://www.aygfsteel.com/Files/sitinspring/TimedThreadCallBack20071106194506.rar

          posted on 2007-11-06 12:05 sitinspring 閱讀(1143) 評(píng)論(0)  編輯  收藏 所屬分類: 線程Thread


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          sitinspring(http://www.aygfsteel.com)原創(chuàng),轉(zhuǎn)載請(qǐng)注明出處.
          主站蜘蛛池模板: 济宁市| 金川县| 肇源县| 阆中市| 正宁县| 辽宁省| 长阳| 黄梅县| 长白| 华宁县| 墨玉县| 乌兰察布市| 年辖:市辖区| 民县| 读书| 田林县| 黔江区| 巴马| 句容市| 随州市| 潮安县| 桂东县| 紫金县| 微山县| 南皮县| 香港| 启东市| 卢龙县| 大姚县| 南江县| 赤城县| 当雄县| 新安县| 卢龙县| 泗水县| 白玉县| 松桃| 辽阳县| 滨州市| 元阳县| 岗巴县|