klemens
klemens

Reputation: 413

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 e.g. 100 ThreadPool will work longer. Can anybody explain me why ThreadPool stop working?

Ps. I write this code in Android

Upvotes: 5

Views: 2338

Answers (1)

light_303
light_303

Reputation: 2111

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.

Upvotes: 3

Related Questions