简体   繁体   English

固定线程池中的线程阻塞

[英]thread blocking in fixed threadpool

I have two doubts: 我有两个疑问:

  1. Regarding fixed threadpool in Java. 关于Java中的固定线程池。 Assume I created a fixed threadpool with 5 threads and all threads are currently executing, and also assume there are 4 task waiting in the queue to finish the execution of these threads. 假设我创建了一个包含5个线程且所有线程当前正在执行的固定线程池,并且还假设队列中有4个任务等待完成这些线程的执行。 If all currently executing tasks got blocked what will happen? 如果所有当前正在执行的任务被阻止将会发生什么 Whether there is a way to take task from that queue and put the blocked task in queue? 是否有从该队列中获取任务并将阻塞的任务放入队列的方法?

  2. How we will come to know whether a task is blocked or executing? 我们如何知道任务是被阻止还是正在执行?

If all currently executing tasks got blocked what will happen? 如果所有当前正在执行的任务被阻止将会发生什么 Whether there is a way to take task from that queue and put the blocked task in queue? 是否有从该队列中获取任务并将阻塞的任务放入队列的方法?

No, there's no facility for this. 不,没有这方面的便利。 If a task begins executing and is blocked, then will block that thread until it completes normally, or is interrupted by a thread pool shutdown. 如果任务开始执行并被阻止,那么将阻塞该线程直到它正常完成,或者被线程池关闭中断。

How we will come to know whether a task is blocked or executing? 我们如何知道任务是被阻止还是正在执行?

If you need to know this, then you need to put some knowledge into the task code itself, which can then be queried by some other part of your application. 如果您需要了解这一点,那么您需要将一些知识放入任务代码本身,然后可以通过应用程序的其他部分查询。 Obviously, something else will need to keep hold of a reference to the task to allow this to work, before submitting it to the executor. 显然,在将其提交给执行者之前,还需要保留对任务的引用以允许其工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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