简体   繁体   中英

Thread pool in Java

I write application in Java using ThreadPool. Firstly I create new ThreadPool:

private ExecutorService threadExecutor = Executors.newFixedThreadPool( 20 );

Then I create some Runnable objects. After that I execute my ThreadPool from time to time passing him the same Runnable object:

threadExecutor.execute(serverRunnable);

I execute this ThreadPool every 20 seconds. My problem is that threadExecutor stops working for some 5 minutes. It just don't execute Runnable object. I notice that when I increase argument in:

Executors.newFixedThreadPool( 20 );

from 20 to eg 100 ThreadPool will work longer. Can anybody explain me why ThreadPool stop working?

Ps. I write this code in Android

firstly if you want to schedule as task for execution every 20 seconds try to use ScheduledThreadPoolExecutor instead : http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html

It seems your runnable does not terminate - in this way it will exceed the 20 threads after a fixed amount of time. If your runnable terminates normally - you will be able to use your executor infinitely long.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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