java中ThreadPool的编号 [英] Numbering of ThreadPool in java

查看:30
本文介绍了java中ThreadPool的编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在一个函数中使用带有两个线程的 threadPool.

该函数被多个客户端同步调用.

在那个函数中我正在使用

ExecutorService executor = Executors.newFixedThreadPool(2);

完成任务后,我将关闭执行程序.

executor.shutdown();

但在日志中我有这样的行

[pool-120069-thread-1]

这是否意味着有 120069 个池是活动的,或者只是对池进行编号的方式.即使在关闭它之后,它还会继续编号吗?

解决方案

这是否意味着 120069 个池还活着,或者它只是对池进行编号的方式.

别担心,是后者.系统中创建的每个新池都会获得一个新的序列号,这意味着您已经创建了 120069 个池,但可能之前的大部分 120068 池已经关闭.

另一方面,为每个方法调用创建一个线程池并不是一个好习惯,请考虑只有一个池并在多个并发方法调用中重用它.此外,您可能不需要同步.但这取决于您的实际用例.

最后,您可能希望在关机后调用 awaitTermination().shutdown() 只是关闭门"到池中,但正在运行的任务会继续运行.

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.

这篇关于java中ThreadPool的编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆