<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    java中處理http連接超時.

    Posted on 2006-07-06 15:50 Hally 閱讀(4921) 評論(0)  編輯  收藏 所屬分類: 學習隨筆

    http是無狀態的連接,不像socket可以有timeout的異常,因此在判斷超時上一直比較困擾.

    這段時間做j2me的一個小游戲.用到了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");
    ????}
    ?}


    不知道大家有沒有更好的方法

    望回復賜教.

    posts - 43, comments - 200, trackbacks - 0, articles - 2

    Copyright © Hally

    主站蜘蛛池模板: 亚洲一级二级三级不卡| 亚洲AⅤ永久无码精品AA| a级毛片毛片免费观看久潮喷| 你懂的在线免费观看| 亚洲精品视频在线看| 亚洲人成电影在线天堂| 丁香婷婷亚洲六月综合色| 999久久久免费精品播放| 国产乱子伦精品免费无码专区 | 亚洲国产精品无码久久一区二区 | 久久综合给合久久国产免费| 国产AV无码专区亚洲AV毛网站| 二个人看的www免费视频| 久久精品国产亚洲av麻豆 | 久久亚洲欧洲国产综合| 一边摸一边爽一边叫床免费视频| 亚洲精品456播放| 三上悠亚电影全集免费| 亚洲高清在线播放| 污视频网站在线免费看| 久久精品亚洲乱码伦伦中文| 中文字幕不卡高清免费| 亚洲精品高清国产麻豆专区| 国产精品色拉拉免费看| 亚洲色在线无码国产精品不卡| 日本媚薬痉挛在线观看免费| 亚洲精品第一国产综合精品| 青苹果乐园免费高清在线| 亚洲精品宾馆在线精品酒店| 在线a免费观看最新网站| 亚洲国产激情在线一区| 国产亚洲精品免费| 久久久久久久岛国免费播放| 亚洲AV无码无限在线观看不卡| 免费a级黄色毛片| av永久免费网站在线观看| 精品亚洲国产成人| 曰批全过程免费视频在线观看| 亚洲天堂一区二区| 国产又长又粗又爽免费视频| 亚洲xxxx18|