我使用多线程下载网页,写了一个判断连接超时的类.不过有时候下载过程中线程会出现阻塞,各位帮忙看看我的代码有什么问题吗?这是根据网上的代码改的,大家帮我看看会不会导致阻塞.
//判断下载过程是否已经超时
public class JTimeOut {
    
public InputStream  getConnection(String strUrl, int timeout, boolean isQuery) {
Connection connect = new Connection(strUrl);
connect.start();
Timer time = new Timer(timeout * 1000, connect.httpConnection);
time.start();
while (time.isAlive()) {
try {
if (isQuery)
System.out.println("等待连接" + strUrl);
Thread.sleep(1000);
} catch (InterruptedException ioe) {
continue;
}
}
if (connect.getisConn()) {
return connect.in;
} else {
return null;
}
}

public boolean isConnActive = false;

class Connection extends Thread {
public HttpURLConnection httpConnection = null;
public String strUrl = null;
public InputStream in = null;
public Connection(String strUrl) {
this.strUrl = strUrl;
}

public boolean getisConn() {
return isConnActive;
}

public void run() {
URL url = null;
try {
isConnActive = false;
url = new URL(strUrl);
httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.addRequestProperty("User-Agent", "IcewolfHttp/1.0");
httpConnection.addRequestProperty("Accept",
                                     "www/source; text/html; image/gif; */*");
httpConnection.connect();
            in = httpConnection.getInputStream();
isConnActive = true;
} catch (Exception e) {
         isConnActive = false;
        }
}
}

class Timer extends Thread {
/** 每个多少毫秒检测一次 */
protected int m_rate = 100; 
/** 超时时间长度毫秒计算 */
private int m_length; /** 已经运行的时间 */
private int m_elapsed;
private HttpURLConnection httpConnection = null; /**
 * 构造函数
 * 
 * @param length
 *            Length of time before timeout occurs
 */
public Timer(int length, HttpURLConnection httpConnection) {
m_length = length;
this.httpConnection = httpConnection;
// Set time elapsed
m_elapsed = 0;
}
public void run() {
// 循环 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) { 
//System.out.println("连接完成");
break;
}
} }
}
}
}

解决方案 »

  1.   

    public void run() { 
    URL url = null; 
    try { 
    isConnActive = false; 
    url = new URL(strUrl); 
    httpConnection = (HttpURLConnection) url.openConnection(); 
    httpConnection.addRequestProperty("User-Agent", "IcewolfHttp/1.0"); 
    httpConnection.addRequestProperty("Accept", 
                                         "www/source; text/html; image/gif; */*"); 
    httpConnection.connect(); 
                in = httpConnection.getInputStream(); 
    isConnActive = true; 
    } catch (Exception e) { 
             isConnActive = false; 
            } 

    } class Timer extends Thread { 
    /** 每个多少毫秒检测一次 */ 
    protected int m_rate = 100;  
    /** 超时时间长度毫秒计算 */ 
    private int m_length; /** 已经运行的时间 */ 
    private int m_elapsed; 
    private HttpURLConnection httpConnection = null; /** 
     * 构造函数 
     *  
     * @param length 
     *            Length of time before timeout occurs 
     */ 
    public Timer(int length, HttpURLConnection httpConnection) { 
    m_length = length; 
    this.httpConnection = httpConnection; 
    // Set time elapsed 
    m_elapsed = 0; 

      

  2.   

    public Timer(int length, HttpURLConnection httpConnection) { 
    m_length = length; 
    this.httpConnection = httpConnection; 
    // Set time elapsed 
    m_elapsed = 0; 

      

  3.   

    ls,你改了什么地方,我怎么没看出来?还是你的意思是,你指出的那段代码有问题?能否明示,3q
      

  4.   

    如果你是一个连接对应一个监听线程的话不用加synchronized,,但是阻塞的原因尚未发现不过发现一个地方很有可能发生死循环帮你简单改了下 public void run() {
    try {
    while (!Thread.interrupted()) {
    // Put the timer to sleep
    Thread.sleep(m_rate);
    // Increment time remaining
    m_elapsed += m_rate; // Check to see if the time has been exceeded
    if (m_elapsed > m_length || isConnActive) {
    // System.out.println("连接完成");
    break;
    }
    }
    } catch (Throwable e) {
    e.printStackTrace();
    }
    }
      

  5.   

    当你监听到超时的连接你也没干什么事啊,
    我劝你还是别自己弄了,用apache的httpclient吧,你要的功能都用现成的,我上周写了个抓股票的程序,核对代码段仅是你的代码长度的三分之一