学习网考试学习资料

Gzu521.com

如何安全关闭超时的Java线程

Java教程   点击:次   发布时间:2005-8-10   【字体: 】   来源:Gzu521.com
贵 州 学 习 网
网络编程经常遇到的一种情况是:客户端通过socket与服务器通信时,遇到断断续续的数据流,有时接收线程可能因为无法收到完整的数据而发生长时间阻塞。在这种情况下,即使设置socket超时也不见得有效(例如,httpclient中虽然可以设置sockettimeout,但以本人的经验来看,遇到这种线程阻塞的情况仍然束手无策)。例如,下面这段代码可能长时间阻塞在readline():

bufferedreader in = new bufferedreader(new inputstreamreader(

                  get.getresponsebodyasstream(),

                  get.getresponsecharset()));

string inputline = 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的源代码可知其实现细节:

public static void execute(thread task, long timeout) throws timeoutexception {

        task.start();

        try {

       task.join(timeout);

        } catch (interruptedexception e) {

       /* if somebody interrupts us he knows what he is doing */

        }

        if (task.isalive()) {

       task.interrupt();

       throw new timeoutexception();

        }

    }

其实就是通过join()和interrupt()方法实现这种功能,文档中强调了task的interrupt()方法必须重写(override),也就是说,这种方法还是无法保证线程超时结束时的状态稳定。

 
我们发现,问题的关键在于,如何保证线程超时关闭后进行相应的收尾工作。其实,这一点可以通过异常机制来解决:

提供给外部调用的关闭方法stopthread()首先检查输入流是否为空,如果不为空,则直接关闭输入流,此时从输入流读取数据的方法会捕捉到一个ioexception异常,只要捕获了这个异常,就可以执行相应的收尾操作。

 
在线程中设置如下几个属性:

inputstream instream = null;

boolean isready = false;

boolean nostoprequested = true;

 
在run()调用的方法中有如下代码:

……

try {

    client.executemethod(get);

    instream = get.getresponsebodyasstream();

    string charset = get.getresponsecharset();

    inputstreamreader isreader = new inputstreamreader(instream, charset);

    bufferedreader buffin = new bufferedreader(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();

}

……

 

提供给外界调用的关闭方法:

public void stopthread() {

    nostoprequested = false;

    if (instream != null) {

       try {

           instream.close();

       } catch (exception e) {

                //

       } finally {

           instream = null;

       }

    }

}

 
经过测试,这种技巧能有效地关闭超时线程。

责任编辑:gzu521

网络编程分类
ASP教程
.Net教程
Java教程
PHP教程
数据库基础
ACCESS教程
SQL Server教程
MySQL教程
Oracle教程
分类推荐信息
更多...
大类最新文章
更多...