Java Thread.sleep泄漏线程? [英] Java Thread.sleep leaking threads?

查看:126
本文介绍了Java Thread.sleep泄漏线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我继承了一些代码,正在等待来自网络源的通信.

So I inherited a bit of code that's waiting for communication from a network source.

在等待来自网络套接字的更多数据时,将调用Thread.sleep(10).正如jconsole和我的此处的线程转储所报告的那样,这似乎正在引起线程泄漏(Thread-68,Thread-385等有数百个条目,但是为了简洁起见,我将其缩短了):

While it's waiting for more data from the network socket, Thread.sleep(10) is called. This appears to be causing a thread leak, as reported by jconsole and my thread dump here (there are hundreds of entries for Thread-68, Thread-385, etc... but I shortened for brevity):

Wed Jan 18 09:14:40 PST 2012
2012-01-18 09:14:50
Full thread dump Java HotSpot(TM) 64-Bit Server VM (20.0-b11 mixed mode):

"Thread-69" daemon prio=10 tid=0x00007f01a047c800 nid=0x3725 waiting on condition [0x00007f019eaf4000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep(Native Method)
        at com.unitt.framework.websocket.simple.NetworkSocket.run(NetworkSocket.java:304)
        at java.lang.Thread.run(Thread.java:662)

"Thread-68" daemon prio=10 tid=0x00007f01a0500000 nid=0x371c waiting on condition [0x00007f019ecf6000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep(Native Method)
        at com.unitt.framework.websocket.simple.NetworkSocket.run(NetworkSocket.java:304)
        at java.lang.Thread.run(Thread.java:662)

相关代码:

public class NetworkSocket implements NetworkSocketFacade, Runnable
{

... removed many irrelevant methods

public void run()
{
    byte[] readBuffer = new byte[512 * 1024];
    while (isRunning)
    {
        //ioLogger.debug("in while(isRunning) loop");
        try
        {
            int length = input.available();
            if (length > 0)
            {
                int read = input.read(readBuffer, 0, readBuffer.length);

                if (read < 0)
                {
                    isRunning = false;
                    //@todo: do we disconnect?
                    ioLogger.debug("setting isRunning FALSE after read < 0");
                }
                else
                {
                   //read data and process
                }
            }
            else
            {
                //ioLogger.debug("nothing to read, sleeping");
                try
                {
                    Thread.sleep( 10 );
                }
                catch ( InterruptedException e )
                {
                    //do nothing, keep going
                }
            }
        }
    // some catch blocks and logging after this

我有一些担心,以这种频率呼叫睡眠会引起问题,并且我试图将睡眠时间从10增加到250,以缓解这种情况.确实可以改善一些问题,但是随着时间的流逝,我仍然遇到相同的问题-我不断地泄漏线程,直到没有堆空间为止.

I have some worries that calling sleep with this frequency can cause problems and I've tried increasing the sleep time from 10 to 250 just to allay the situation. That does improve matters somewhat, but over time I still end up with the same problem - I steadily leak threads until I am out of heap space.

有人对此行为有见识吗?我不认为像Thread.sleep()这样基本的东西会引起这样的问题.

Does anyone have any insights into this behavior? I wouldn't think that something as basic as Thread.sleep() would cause problems like this.

推荐答案

问题不在于Thread.sleep(),而是线程的逻辑.

The problem isn't with Thread.sleep(), it's with the thread's logic.

根据您发布的代码,该线程将在isRunning = false时终止.现在,将isRunning设置为false的唯一方法是input.available()返回正值,然后input.read()返回负值.

From the code that you've posted, the thread will terminate when isRunning = false. Now, the only way for isRunning to be set to false is when input.available() returns a positive value, followed by input.read() returning a negative value.

在这种情况下,似乎世界上没有任何状态.

There doesn't appear to be any state of the world when that would be the case.

结果,使用此run()方法的所有线程将一直存活到该进程生存,并将其大部分时间都花费在Thread.sleep()中.

As a result, all of the threads using this run() method will live for as long as the process lives, spending most of their time in Thread.sleep().

P.S.这基于您发布的代码.如果您没有将设置为false 方式,请更新您的问题.

P.S. This is based on the code that you've posted. If there are ways for isRunning to be set to false that you're not currently showing, please update your question.

这篇关于Java Thread.sleep泄漏线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆