中文JAVA技術平等自由協作創造

          Java專題文章博客和開源

          常用鏈接

          統計

          最新評論

          java中處理http連接超時的方法

            聲明一個boolean公共變量,表明當前httpconnection是否得到服務器回應。
           
             你的連接線程中在連接之前置這個變量為false;
           
             另起一個監視線程,拿到那個HttpConnection的連接對象,并循環監視這個boolean公共變量。如果指定時間內(20秒后)你的 boolean公共變量還是false,那么就主動置httpconnection=null.這樣,那邊連接線程就會拋出異常退出來。托福答案
           
             寫了Timer類來實現。(學習國外一個網站上的寫法)
           
             class Timer extends Thread {
           
             /** 每個多少毫秒檢測一次 */
           
             protected int m_rate = 100;
           
             /** 超時時間長度毫秒計算 */
           
             private int m_length;
           
             /** 已經運行的時間 */
           
             private int m_elapsed;
           
             /**
           
             * 構造函數
           
             *
           
             * @param length
           
             * Length of time before timeout occurs
           
             */
           
             public Timer(int length) {
           
             // Assign to member variable
           
             m_length = length;
           
             // Set time elapsed
           
             m_elapsed = 0;
           
             }
           
             /**
           
             * 重新計時
           
             *
           
             */
           
             public synchronized void reset() {
           
             m_elapsed = 0;
           
             System.out.println("reset timer");
           
             }
           
             /**
           
             * 故意設置為超時,可以在服務器有返回,但是錯誤返回的時候直接調用這個,當成超時處理托福答案
           
             *
           
             */
           
             public synchronized void setTimeOut()
           
             {
           
             m_elapsed = m_length+1;
           
             }
           
             /**
           
             */
           
             public void run() {
           
             // 循環
           
             System.out.println("timer running");
           
             for (;;) {
           
             // Put the timer to sleep
           
             try {
           
             Thread.sleep(m_rate);
           
             } catch (InterruptedException ioe) {
           
             continue;
           
             }
           
             synchronized (this) {
           
             // Increment time remaining
           
             m_elapsed += m_rate;
           
             // Check to see if the time has been exceeded
           
             if (m_elapsed > m_length && !isConnActive) { //isConnActive 為全局變量
           
             // Trigger a timeout
           
             timeout();
           
             break;
           
             }
           
             }
           
             }
           
             }
           
             /**
           
             * 超時時候的處理
           
             *
           
             */
           
             public void timeout() {
           
             httpConnection = null;
           
             System.out.println("conn time > " + TIME_OUT + " ms");
           
             }
           
             }
           
             在http連接線程調用的同是調用 new Timer(20*1000)。start();
           
             需要重新計時時候調用timer.reset();
           
           

          posted on 2013-10-08 19:55 好不容易 閱讀(222) 評論(0)  編輯  收藏


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


          網站導航:
           
          PK10開獎 PK10開獎
          主站蜘蛛池模板: 海阳市| 崇左市| 平邑县| 静乐县| 辽阳市| 顺平县| 十堰市| 长宁区| 武平县| 大英县| 阜康市| 吕梁市| 武川县| 江西省| 冀州市| 木兰县| 本溪市| 虹口区| 迭部县| 日土县| 普洱| 武胜县| 峨眉山市| 阳朔县| 临城县| 治多县| 赤城县| 六枝特区| 乐至县| 金湖县| 罗定市| 岢岚县| 梓潼县| 松江区| 红安县| 沁阳市| 罗江县| 昌宁县| 南郑县| 柏乡县| 萝北县|