public static void main(String args[]) {
// 初始化日志配置文件
PropertyConfigurator.configure(com.ccit.util.StaticValue.log4jFilePath);
logger.info("***************************************************");
logger.info("*          SockServerApp Service Start            *");
logger.info("***************************************************");
// 初始化配置文件变量
Socket client = null; try { PooledExecutor pool = new PooledExecutor(new BoundedBuffer(
(ReadConfig.getInstance().getMaxpoolsize()).intValue()),
(ReadConfig.getInstance().getPoolqueue()).intValue()); // 设置线程池的最小线程数
pool.setMinimumPoolSize((ReadConfig.getInstance().getMinpoolsize())
.intValue());
// 设置线程池中线程的存活时间
pool.setKeepAliveTime((ReadConfig.getInstance().getCallTimeOut())
.intValue());
int port = (ReadConfig.getInstance().getPort()).intValue(); logger.debug("port =" + port);
// 建立监听socket
ServerSocket server = new ServerSocket(port);
while (true) {
try {
client = server.accept();
logger.debug("client 接受到请求===== " + client);
logger.debug("client 进入程序进行处理===== " + client);
pool.execute(new RSThread(client));
} catch (Throwable e) {
logger.error(e.toString(), e);
}
} } catch (Throwable e) {
logger.error(e.toString(), e);
} finally {
if (client != null) {
try {
client.close();
} catch (Exception e) {
logger.error(e.toString(), e);
}
}
}
}
这是监听主程序,程序运行后,进行监听,但是过了几个小时候,进程自动停了,这是怎么回事,观察了两天了,什么异常错误都没有抛出来,很奇怪啊。。高手们帮帮忙。。

解决方案 »

  1.   

    死掉就是这个监控程序的进程kill掉了。。我是放到服务器上就启动程序,让程序监听,在监听了几个小时没有任何事件调用的情况下进程还是自动kill掉了,如果监听到请求调用了还可以打日志什么的,可以向线程泄露那方面找原因,但是没有任何请求任何连接,过几个小时还是kill掉了。。到底什么原因呢?
      

  2.   

    PooledExecutor是你自己的类阿,我看成是ThreadPoolExecutor了
    你的PooledExecutor会不会在某种条件下调用exit()的
      

  3.   

    PooledExecutor是个开源的线程池,不是我自己写的类,它会自动调用exit()吗?不会吧
      

  4.   

    因为一个连接都没有,那么"client 接受到请求===== " + client 肯定没有打印出来吧?
    所以client = server.accept(); 一直处于阻塞状态,根本就没往下执行。
    那问题就很可能出在PooledExecutor上,可能线程池本身有Bug。
    用JDK自己的ThreadPoolExecutor多好。
      

  5.   

    "client 接受到请求===== " + client 这个肯定没有打出来啊,因为没有接收到访问请求,程序一直处于监听状态嘛,一旦有请求了,这个就会打印出来,执行它该做的操作了
      

  6.   

    仔细看了一下,应该确实是内存溢出,注意:
    while (true) {
    try {
    client = server.accept();
    logger.debug("client 接受到请求===== " + client);
    logger.debug("client 进入程序进行处理===== " + client);
    pool.execute(new RSThread(client)); } catch (Throwable e) {
    logger.error(e.toString(), e);
    }
    }可以看出,每次循环client 指向 server.accept();生成的对象,然而这个对象又被pool.execute()继续使用,问题就在于pool.execute()是不是有可能一直没有释放这个对象呢?如果是,则问题就在于此了,要想办法让pool释放这个对象.
      

  7.   

    那就是阿,进程终止应该是跟while后面的代码没关系的。
    所以我怀疑问题出在PooledExecutor本身有Bug,建议你使用java.util.concurrent.ThreadPoolExecutor,肯定比你用的好,而且也挺方便的。
      

  8.   


    不好意思,没看到这个描述。那跟while后面的代码没啥关系了
      

  9.   

    我刚才查一下API发现一下这一段描述,可能对你有帮助,你可以看一下
    Keep-alive time 
    If the pool maintained references to a fixed set of threads in the pool, then it would impede garbage collection of otherwise idle threads. This would defeat the resource-management aspects of pools. One solution would be to use weak references. However, this would impose costly and difficult synchronization issues. Instead, threads are simply allowed to terminate and thus be GCable if they have been idle for the given keep-alive time. The value of this parameter represents a trade-off between GCability and construction time. In most current Java VMs, thread construction and cleanup overhead is on the order of milliseconds. The default keep-alive value is one minute, which means that the time needed to construct and then GC a thread is expended at most once per minute. 
    To establish worker threads permanently, use a negative argument to setKeepAliveTime.
      

  10.   


    补充:可以调用pool.setKeepAliveTime(1000 * 60 * 5);修改线程存活时间