固定线程池中的线程阻塞

发布于 2024-12-16 11:07:07 字数 211 浏览 0 评论 0原文

我有两个疑问:

  1. 关于Java中的固定线程池。假设我创建了一个包含 5 个线程的固定线程池,并且所有线程当前都在执行,并且还假设队列中有 4 个任务正在等待完成这些线程的执行。如果当前正在执行的所有任务都被阻止,会发生什么?是否有办法从该队列中取出任务并将阻塞的任务放入队列中?

  2. 我们如何知道任务是被阻塞还是正在执行?

I have two doubts:

  1. Regarding fixed threadpool in 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. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各自安好 2024-12-23 11:07:07

如果当前正在执行的所有任务都被阻止,会发生什么?是否有办法从该队列中取出任务并将阻塞的任务放入队列中?

不,没有这方面的设施。如果任务开始执行并被阻止,则将阻止该线程,直到其正常完成,或者因线程池关闭而中断。

我们如何知道任务是被阻塞还是正在执行?

如果您需要知道这一点,那么您需要将一些知识放入任务代码本身中,然后可以由应用程序的其他部分查询。显然,在将任务提交给执行者之前,其他东西需要保留对任务的引用才能使其工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文