如果当前线程休眠,ThreadPoolExecutor 是否会生成新线程

发布于 2024-09-26 05:21:37 字数 1098 浏览 7 评论 0原文

这个问题是这个问题的后续问题。

本质上,我所做的就是声明一个只有一个线程的 ThreadPoolExecutor 。我正在重写 beforeExecute() 方法来进行睡眠,以便我的每个任务在执行时都会有一些延迟。这基本上是为了将 CPU 交给其他线程,因为我的线程有点不稳定。

因此预期的行为是:

对于 ThreadPoolExecutor 中的每个新任务,它会在执行任务之前调用 beforeexecute 函数,因此在执行任务之前它会休眠 20 秒。

然而,这就是我所看到的:

对于提交的每个新任务:

  1. 它执行任务
  2. 调用 beforeExecute 方法
  3. 休眠 20 秒
  4. 重新执行任务!

1. & 的顺序2. 并不是每时每刻都一样。

这是我的问题:

  1. 似乎有一个新线程在睡眠后/睡眠期间进入,并在实际线程睡眠时立即继续执行我的任务。
    那么,一旦现有线程休眠,ThreadPoolExecutor 是否会生成一个新线程[认为该线程已终止]? 我试图把 keepAliveTime > sleeptime ..因此,如果上述断言为 true ..它至少会等待超过睡眠时间来生成新线程...[希望同时睡眠线程会被唤醒并且 ThreadPoolExecutor code> 会转储产生一个新线程的想法
  2. 即使它确实产生一个新线程并立即执行我的任务,为什么在睡眠线程唤醒后会重新执行该任务!在此之前任务不应该从任务队列中取出吗?
  3. 我在这里错过了什么吗?还有其他方法来调试这种情况吗?

=>我正在考虑执行所需任务(而不是解决问题)的另一种方法是用另一个可运行程序包装可运行程序,并在调用内部可运行程序之前休眠外部可运行程序。

This question is a followup on this one.

Essentially what I am doing is declaring a ThreadPoolExecutor with just one thread. I am overriding the beforeExecute() method to put a sleep so that each of my tasks are executed with some delay among themselves. This is basically to give away the CPU to other threads since my thread is kind of thrashing.

So the expected behavior is:

For each new task in the ThreadPoolExecutor, it calls the before execute function before executing the task and hence it sleeps for say 20s before it executes the task.

However this is what I see:

For each new task submitted:

  1. It executes the task
  2. Calls the beforeExecute method
  3. sleeps for say 20s
  4. RE-EXECUTE the task!

The order of 1. & 2. is not the same all the time.

Here are my questions:

  1. It is appearing that a new thread comes in after/during sleeping and goes ahead and executes my task right away while the actual thread is sleeping.
    So does the ThreadPoolExecutor spawn a new thread as soon as an existing thread sleeps [thinking that the thread is terminated]??
    I tried to put the keepAliveTime > sleeptime ..so that in case the above assertion is true .. it atleast waits for more than sleep time to spawn a new thread ...[hoping in the mean time the sleeping thread would be awake and the ThreadPoolExecutor would dump the idea of spawning a new thread
  2. Even if it does spawn a new thread and execute my task right away, why would the task be re-executed after the sleeping thread wakes up !! Shouldn't the task be taken out of task Queue before that ??
  3. Am I missing something here ? Any other way to debug this scenario ?

=> An alternative method I was thinking to do the desired task [and not solve the peoblem] was to wrap the runnable with one more runnable and sleep the outer runnable before calling the inner one.

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

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

发布评论

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

评论(2

[浮城] 2024-10-03 05:21:37

我认为您正在寻找的是 ScheduledExecutorService

据我对你的问题的理解,scheduleAtFixedRate(...) 应该完成这笔交易:

scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)

创建并执行定期操作
在之后首先启用
给定初始延迟,随后
在给定的期限内;那是
处决将于之后开始
初始延迟然后初始延迟+周期,
然后是initialDelay + 2 * period,依此类推
上。

I think what you're looking for is a ScheduledExecutorService

From what I understand of your question, scheduleAtFixedRate(...) should do the deal:

scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)

Creates and executes a periodic action
that becomes enabled first after the
given initial delay, and subsequently
with the given period; that is
executions will commence after
initialDelay then initialDelay+period,
then initialDelay + 2 * period, and so
on.

三月梨花 2024-10-03 05:21:37
  1. 不,事情不是这样的。 ThreadPoolExecutor 知道它有一个工作线程,即使该工作线程处于 RUNNABLEWAITINGBLOCKED 或任何状态其他状态。
  2. 早在调用 beforeExecute 方法之前,任务就会从 BlockingQueue 中删除。
  3. 您可以自己查看 API 的代码并确定它在做什么。每个 Java JDK 安装都包含一个“src.zip”文件,其中包含整个 Java 库。如果您还没有,您可以在 Eclipse 中附加此源代码,然后在 Eclipse 中调试时深入到库方法中,将显示源代码而不仅仅是类文件。
  1. No, that is not how it works. The ThreadPoolExecutor knows it has a worker thread, even if that worker is RUNNABLE, WAITING, BLOCKED, or any other state.
  2. The task is removed from the BlockingQueue long before the beforeExecute method is invoked.
  3. You can look at the code for the API yourself and determine what it is doing. Every Java JDK installation includes a "src.zip" file which contains the entire Java Library. If yu haven't already, you can attach this source in eclipse and then while debugging in eclipse diving into a library method will show you source instead of just the class file.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文