java socket 监听程序总是隔几个小时就自动死掉。。在线等。。。

chenwei023 2009-01-09 09:22:27
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);
}
}
}
}
这是监听主程序,程序运行后,进行监听,但是过了几个小时候,进程自动停了,这是怎么回事,观察了两天了,什么异常错误都没有抛出来,很奇怪啊。。高手们帮帮忙。。
...全文
942 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
guestdaocao 2009-03-24
  • 打赏
  • 举报
回复
不知道lz是不是使用的JDK1.4?
chenwei023 2009-01-09
  • 打赏
  • 举报
回复
PooledExecutor是个开源的线程池,不是我自己写的类,它会自动调用exit()吗?不会吧
MT502 2009-01-09
  • 打赏
  • 举报
回复
PooledExecutor是你自己的类阿,我看成是ThreadPoolExecutor了。。。
你的PooledExecutor会不会在某种条件下调用exit()的
chenwei023 2009-01-09
  • 打赏
  • 举报
回复
死掉就是这个监控程序的进程kill掉了。。我是放到服务器上就启动程序,让程序监听,在监听了几个小时没有任何事件调用的情况下进程还是自动kill掉了,如果监听到请求调用了还可以打日志什么的,可以向线程泄露那方面找原因,但是没有任何请求任何连接,过几个小时还是kill掉了。。到底什么原因呢?
axman 2009-01-09
  • 打赏
  • 举报
回复
死掉是什么意思?系统崩掉了?程序没有反映了?加了定时调试信息没有?
chenwei023 2009-01-09
  • 打赏
  • 举报
回复
昨天观察了几个小时,一个连接都没有,因为现在处于调试阶段,一个连接都没有的情况下,还是几个小时就死掉了。。郁闷。。
yanhan0615 2009-01-09
  • 打赏
  • 举报
回复
观察一下两天内有多少连接,如果连接数不多,就考虑线程问题,如果连接数很多的话,有可能是内存问题
liuhua19841201 2009-01-09
  • 打赏
  • 举报
回复
是不是有些资源流没有关闭 内存溢出?
Kevin-林 2009-01-09
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 ljc2008110 的回复:]
我刚才查一下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 issu…
[/Quote]

补充:可以调用pool.setKeepAliveTime(1000 * 60 * 5);修改线程存活时间
Kevin-林 2009-01-09
  • 打赏
  • 举报
回复
我刚才查一下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.
LQQ 2009-01-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 chenwei023 的回复:]
昨天观察了几个小时,一个连接都没有,因为现在处于调试阶段,一个连接都没有的情况下,还是几个小时就死掉了。。郁闷。。
[/Quote]

不好意思,没看到这个描述。那跟while后面的代码没啥关系了
MT502 2009-01-09
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 chenwei023 的回复:]
"client 接受到请求===== " + client 这个肯定没有打出来啊,因为没有接收到访问请求,程序一直处于监听状态嘛,一旦有请求了,这个就会打印出来,执行它该做的操作了
[/Quote]
那就是阿,进程终止应该是跟while后面的代码没关系的。
所以我怀疑问题出在PooledExecutor本身有Bug,建议你使用java.util.concurrent.ThreadPoolExecutor,肯定比你用的好,而且也挺方便的。
LQQ 2009-01-09
  • 打赏
  • 举报
回复
仔细看了一下,应该确实是内存溢出,注意:
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释放这个对象.
chenwei023 2009-01-09
  • 打赏
  • 举报
回复
程序只是一直处于监听状态,会有内存溢出???
LQQ 2009-01-09
  • 打赏
  • 举报
回复
第一感觉是内存溢出
chenwei023 2009-01-09
  • 打赏
  • 举报
回复
"client 接受到请求===== " + client 这个肯定没有打出来啊,因为没有接收到访问请求,程序一直处于监听状态嘛,一旦有请求了,这个就会打印出来,执行它该做的操作了
fuyueyue 2009-01-09
  • 打赏
  • 举报
回复
学习
MT502 2009-01-09
  • 打赏
  • 举报
回复
因为一个连接都没有,那么"client 接受到请求===== " + client 肯定没有打印出来吧?
所以client = server.accept(); 一直处于阻塞状态,根本就没往下执行。
那问题就很可能出在PooledExecutor上,可能线程池本身有Bug。
用JDK自己的ThreadPoolExecutor多好。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧