线程过早退出

时间:2017-05-13 13:52:00

标签: java multithreading thread-safety

我有一台正在运行的Bodi服务器并且它一直在某条线路上退出,就像该功能提前退出一样。我假设在这里(在尝试使用相同问题的Eclipse和NetBeans之后),它本质上是程序化的原因。父线程轮询checkinputqueue()和checkoutputqueue(),然后检查这些服务器是否有数据标记为准备好处理。

Bodi服务器是共享对象服务器。您可以向本地JVM询问一个对象并获得非常类似于静态引用的信息,或远程地向另一个JVM请求一个对象,并且您将获得一个对象引用以进行转换。

无法使其正常工作。你们能帮忙吗?主要问题是在readLine()方法完成退出之前退出的线程。对readLine()的调用只是退出调试器会话,好像没有更多的执行要跟踪!?!?

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package apml.system.bodi.remote;

/**
*
* @author Max Rupplin
*/

class Listenerthread extends Thread
{
public volatile Connection connection;

public Boolean running = true;

//public Basicserver server;

public volatile Inputlistenerthread inputlistenerthrread;

public volatile Outputlistenerthread outputlistenerthread;

public Listenerthread(Connection connection)
{
    this.connection = connection;

    /*---------------------------------------------------------------------*/

    this.setName("Listenerthread");

    /*---------------------------------------------------------------------*/

    this.inputlistenerthrread = new Inputlistenerthread(this);

    this.inputlistenerthrread.start();        

    /*---------------------------------------------------------------------*/

    this.outputlistenerthread = new Outputlistenerthread(this);                

    this.outputlistenerthread.start();
}

@Override
public void run()
{
    System.out.println(">   Server main thread started...");

    while(running)            
    {
        System.err.println("Listner thread now looping...");

        try
        {
            synchronized(this.inputlistenerthrread.lock)
            {
                if(this.inputlistenerthrread.hasreadready)
                {                    
                    this.inputlistenerthrread.checkinputqueue();                        

                    this.connection.isdonereading = true;
                }
            }

            synchronized(this.outputlistenerthread.lock)
            {
                if(this.outputlistenerthread.haswriteready)
                {
                    this.outputlistenerthread.checkoutputqueue();

                    this.connection.isdonewriting = true;
                }
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                Thread.sleep(400);
            }
            catch(InterruptedException ie)
            {
                running = false;
            }
            catch(Exception e)
            {                    
                e.printStackTrace();                                        
            }
        }
    }

    System.out.println("Basicserver listener thread exiting...");
}       
}

/ ---- /

package apml.system.bodi.remote;

/**
*
* @author Max Rupplin
 */
class Inputlistenerthread extends Thread  
{
public Boolean hasreadready = false;

public Boolean isnotchecking = true;

public Boolean running = true;

public volatile Listenerthread parent;

public volatile Object lock = new Object();

public Inputlistenerthread(Listenerthread parent)
{
    this.parent = parent;

    this.setName("Inputlistenerthread");                
}

public Boolean checkinputqueue()
{                
    synchronized(this.lock)
    {       
        StringBuffer inputbuffer = new StringBuffer();

        try
        {     
            //this.lock.wait();                           

            String line = null;                                

            while( (line = this.parent.connection.reader.readLine()) != null) 
            {
                inputbuffer.append(line);

                this.parent.connection.isdonereading = false;
            }

            if(inputbuffer.toString().length()>0) 
            {
                this.parent.connection.inqueue.append(inputbuffer);

                this.parent.connection.server.inputqueue.add(this.parent.connection);                                    

                this.parent.connection.hasreadready = true;

                this.parent.connection.isdonereading = true;    
            }                                   
        }
        catch(Error e)
        {
            e.printStackTrace();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            inputbuffer = null;

            //this.lock.notifyAll();
        }           

        return true;            
    }
}  

@Override
public void run()
{
    System.out.println(">   Inputlistenerthread started...");

    try
    {
        while(running)
        {                
            try
            {
                //if(this.parent.server.reader.lines().count()>0 || true)
                if(true)
                {
                    this.hasreadready = true;
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            finally
            {
                Thread.currentThread().sleep(250);
            }
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

}

1 个答案:

答案 0 :(得分:0)

您需要查看调试器,但显示的行已注释掉

//this.lock.wait();

对于保持线程运行至关重要,直到有东西要处理。否则无事可做(队列为空),线程确实立即退出。

相关问题