繁体   English   中英

Java中的线程池

[英]Thread pool in Java

我使用ThreadPool在Java中编写应用程序。 首先我创建新的ThreadPool:

private ExecutorService threadExecutor = Executors.newFixedThreadPool( 20 );

然后我创建了一些Runnable对象。 之后,我不时地执行我的ThreadPool传递相同的Runnable对象:

threadExecutor.execute(serverRunnable);

我每20秒执行一次这个ThreadPool。 我的问题是threadExecutor停止工作大约5分钟。 它只是不执行Runnable对象。 我注意到当我增加参数:

Executors.newFixedThreadPool( 20 );

从20到100个ThreadPool将工作更长时间。 任何人都可以解释为什么ThreadPool停止工作?

PS。 我在Android中编写此代码

首先,如果您希望每隔20秒安排执行任务,请尝试使用ScheduledThreadPoolExecutor: http//docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html

看来你的runnable没有终止 - 这样它会在一段固定的时间后超过20个线程。 如果您的runnable正常终止 - 您将能够无限期地使用您的执行程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM