乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Http連接超時處理

       悟靜 2011-08-13
      聲明一個boolean公共變量,表明當前httpconnection是否得到服務(wù)器回應(yīng)。

      你的連接線程中在連接之前置這個變量為false;

      另起一個監(jiān)視線程,拿到那個HttpConnection的連接對象,并循環(huán)監(jiān)視這個boolean公共變量。如果指定時間內(nèi)(20秒后)你的boolean公共變量還是false,那么就主動置httpconnection=null。這樣,那邊連接線程就會拋出異常退出來。"  -----zhengyun

      寫了Timer類來實現(xiàn).(學(xué)習(xí)國外一個網(wǎng)站上的寫法)

      class Timer extends Thread {
        /** 每個多少毫秒檢測一次 */
        protected int m_rate = 100;

        /** 超時時間長度毫秒計算 */
        private int m_length;

        /** 已經(jīng)運行的時間 */
        private int m_elapsed;

        /**
         * 構(gòu)造函數(shù)
         * 
         * @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");
        }
        /**
         * 故意設(shè)置為超時,可以在服務(wù)器有返回,但是錯誤返回的時候直接調(diào)用這個,當成超時處理
         *
         */
        public synchronized void setTimeOut()
        {
         m_elapsed = m_length+1;
        }

        /** 
          */
        public void run() {
         // 循環(huán)

         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連接線程調(diào)用的同是調(diào)用 new Timer(20*1000).start();

      需要重新計時時候調(diào)用timer.reset();

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多