Java threadpool functionality

Posted by cpf on Stack Overflow See other posts from Stack Overflow or by cpf
Published on 2010-04-21T13:00:07Z Indexed on 2010/04/21 13:03 UTC
Read the original article Hit count: 310

Filed under:
|

Hi stackoverflow,

I need to make a program with a limited amount of threads (currently using newFixedThreadPool) but I have the problem that all threads get created from start, filling up memory at alarming rate.

I wish to prevent this. Threads should only be created shortly before they are executed.

e.g.: I call the program and instruct it to use 2 threads in the pool. The program should create & launch the first 2 Threads immediately (obviously), create the next 2 to wait for the previous 2, and at that point wait until one or both of the first 2 ended executing.

I thought about extending executor or FixedThreadPool or such. However I have no clue on how to start there and doubt it is the best solution. Easiest would have my main Thread sleeping on intervals, which is not really good either...

Thanks in advance!

© Stack Overflow or respective owner

Related posts about java

Related posts about multithreading