JAVA线程的超时处理

来源:互联网 发布:ins翻墙安卓软件免费 编辑:程序博客网 时间:2024/05/18 00:56
网络编程经常遇到的一种情况是:客户端通过Socket与服务器通信时,遇到断断续续的数据流,有时接收线程可能因为无法收到完整的数据而发生长时间阻塞。在这种情况下,即使设置socket超时也不见得有效(例如,HttpClient中虽然可以设置socketTimeout,但以本人的经验来看,遇到这种线程阻塞的情况仍然束手无策)。例如,下面这段代码可能长时间阻塞在readLine():
BufferedReaderin = new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream(),get.getResponseCharSet()));
StringinputLine = in.readLine();
while(inputLine != null) {
    resultBuffer.append(inputLine);
    resultBuffer.append("/n");
    inputLine = in.readLine();
}
in.close();
 
因此,必须想办法解决这种问题。
 
一种办法是调用JDK 1.2中提供的stop()方法强行终止线程,但此方法因为调用时线程内部的状态完全无法确定,已经被淘汰,不推荐使用。
 
当然,也可以查找一些现成的处理超时的Class,例如HttpClient中就提供了这样一个Class- TimeoutController(位于org.apache.commons.httpclient.util包内)查看该class的源代码可知其实现细节:
publicstatic void execute(Thread task, long timeout) throws TimeoutException {
        task.start();
        try {
       task.join(timeout);
        } catch (InterruptedException e) {
       /* if somebody interrupts us he knowswhat he is doing */
        }
        if (task.isAlive()) {
       task.interrupt();
       throw new TimeoutException();
        }
    }
其实就是通过join()和interrupt()方法实现这种功能,文档中强调了task的interrupt()方法必须重写(override),也就是说,这种方法还是无法保证线程超时结束时的状态稳定。
 
我们发现,问题的关键在于,如何保证线程超时关闭后进行相应的收尾工作。其实,这一点可以通过异常机制来解决:
提供给外部调用的关闭方法stopThread()首先检查输入流是否为空,如果不为空,则直接关闭输入流,此时从输入流读取数据的方法会捕捉到一个IOException异常,只要捕获了这个异常,就可以执行相应的收尾操作。
 
在线程中设置如下几个属性:
InputStreaminStream = null;
booleanisReady = false;
booleannoStopRequested = true;
 
在run()调用的方法中有如下代码:
……
try {
    client.executeMethod(get);
    inStream = get.getResponseBodyAsStream();
    String charSet = get.getResponseCharSet();
    InputStreamReader isReader = newInputStreamReader(inStream, charSet);
    BufferedReader buffIn = newBufferedReader(isReader);
    String inputLine = buffIn.readLine();
    while ((inputLine != null) &&noStopRequested) {
       resultBuffer.append(inputLine);
       resultBuffer.append("/n");
       inputLine = buffIn.readLine();
    }
    isReady = true;
} catch(IOException e) {
    //如果调用了stopThread()方法,就会捕获到IOException异常
    //此时noStopRequested为false,不用做任何处理
    if (noStopRequested) {
       e.printStackTrace();
    } else {
       //do nothing
    }
} finally{
    //分情况进行收尾工作
    try {
       if (noStopRequested) {
               inStream.close();
               inStream= null;
       }
       buffIn.close();
       isReader.close();
    } catch (Exception e) {
       e.printStackTrace();
    }
    if(isReady) {
       return resultBuffer.toString();
    } else {
       return "";
    }
    get.releaseConnection();
}
……
 
提供给外界调用的关闭方法:
publicvoid stopThread() {
    noStopRequested = false;
    if (inStream != null) {
       try {
           inStream.close();
       } catch (Exception e) {
                //
       } finally {
           inStream = null;
       }
    }
}
 
经过测试,这种技巧能有效地关闭超时线程。
网友评论:
上面的方面有严重的问题,不能有效的处理超时,start()的时候在主程序中是不能捕获错误的.下面是我写的.
msn:kingshiwei@hotmail.com


public class TimeoutThread implements Runnable {

    private Work work;
    private boolean isTimeout = false;
    private boolean hasDone = false;

    public TimeoutThread(Work work, long timeout) {
        this.work = work;
        final long out = timeout;
        Thread thread = new Thread() {
            public void run() {
                try {
                    System.out.println("Sleeping!");
                    this.sleep(out);
                    isTimeout = true;
                    System.out.println("Timeout!");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        thread.setDaemon(true);
        thread.start();
        Thread workThread = new Thread(this);
        workThread.start();
    }

    public void run() {
        work.run();
        hasDone = true;
    }

    public Object result() throws Exception {
        if (isTimeout) {
            throw new RuntimeException();
        }
        while (!hasDone) {
            if (isTimeout) {
                throw new RuntimeException("操作超时!");
            }
        }
        if (work.result() == null) {
            throw new RuntimeException("获取参数失败!");
        }
        return work.result();
    }
}

public interface Work extends Runnable {
    public void run();

    public Object result();
}



    public static void main(String[] args) {
        TimeoutThread tt = new TimeoutThread(new Work() {
            String info = null;

            public void run() {
                while (true) {
                    System.out.println("execute...");
                    try {
                        Thread.sleep(15000);
                        info = "Hello World";
                        break;
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

            public String result() {
                return info;
            }
        }, 10000);
        try {
            tt.result();
            System.out.println(tt.result());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
原创粉丝点击