简体   繁体   中英

Numbering of ThreadPool in java

Hello I am using threadPool with two threads in a funciton.

This funciton is called by multiple clients in a synchronized way.

in that function I am using

ExecutorService executor = Executors.newFixedThreadPool(2);

and after finishing the task I am shutting down the executor.

executor.shutdown();

But in logs I have lines like this

[pool-120069-thread-1]

Does that mean 120069 pools are alive, or its just way of numbering the pool. Even after shutting it down it continue with the numbering ?

Does that mean 120069 pools are alive, or its just way of numbering the pool.

Don't worry, it's the latter. Every new pool created in the system gets a new sequence number, which means you have created 120069 pools, but probably most of the previous 120068 pools are already shut down.

On the other hand it's not a good practice to create a thread pool per each method invocation, consider having just one pool and reuse it across several concurrent method calls. Also you probably don't need synchronization. But that depends on your actual use case.

Finally, you probably want to call awaitTermination() after shutdown. shutdown() only " closes the door " to the pool, but running tasks continue to run.

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