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);
}
}
}
}
这是监听主程序,程序运行后,进行监听,但是过了几个小时候,进程自动停了,这是怎么回事,观察了两天了,什么异常错误都没有抛出来,很奇怪啊。。高手们帮帮忙。。
// 初始化日志配置文件
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);
}
}
}
}
这是监听主程序,程序运行后,进行监听,但是过了几个小时候,进程自动停了,这是怎么回事,观察了两天了,什么异常错误都没有抛出来,很奇怪啊。。高手们帮帮忙。。
你的PooledExecutor会不会在某种条件下调用exit()的
所以client = server.accept(); 一直处于阻塞状态,根本就没往下执行。
那问题就很可能出在PooledExecutor上,可能线程池本身有Bug。
用JDK自己的ThreadPoolExecutor多好。
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释放这个对象.
所以我怀疑问题出在PooledExecutor本身有Bug,建议你使用java.util.concurrent.ThreadPoolExecutor,肯定比你用的好,而且也挺方便的。
不好意思,没看到这个描述。那跟while后面的代码没啥关系了
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.
补充:可以调用pool.setKeepAliveTime(1000 * 60 * 5);修改线程存活时间