如果当前线程休眠,ThreadPoolExecutor 是否会生成新线程
这个问题是这个问题的后续问题。
本质上,我所做的就是声明一个只有一个线程的 ThreadPoolExecutor 。我正在重写 beforeExecute()
方法来进行睡眠,以便我的每个任务在执行时都会有一些延迟。这基本上是为了将 CPU 交给其他线程,因为我的线程有点不稳定。
因此预期的行为是:
对于 ThreadPoolExecutor 中的每个新任务,它会在执行任务之前调用 beforeexecute 函数,因此在执行任务之前它会休眠 20 秒。
然而,这就是我所看到的:
对于提交的每个新任务:
- 它执行任务
- 调用 beforeExecute 方法
- 休眠 20 秒
- 重新执行任务!
1. & 的顺序2. 并不是每时每刻都一样。
这是我的问题:
- 似乎有一个新线程在睡眠后/睡眠期间进入,并在实际线程睡眠时立即继续执行我的任务。
那么,一旦现有线程休眠,ThreadPoolExecutor
是否会生成一个新线程[认为该线程已终止]? 我试图把 keepAliveTime > sleeptime ..因此,如果上述断言为 true ..它至少会等待超过睡眠时间来生成新线程...[希望同时睡眠线程会被唤醒并且 ThreadPoolExecutor code> 会转储产生一个新线程的想法 - 即使它确实产生一个新线程并立即执行我的任务,为什么在睡眠线程唤醒后会重新执行该任务!在此之前任务不应该从任务队列中取出吗?
- 我在这里错过了什么吗?还有其他方法来调试这种情况吗?
=>我正在考虑执行所需任务(而不是解决问题)的另一种方法是用另一个可运行程序包装可运行程序,并在调用内部可运行程序之前休眠外部可运行程序。
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:
- It executes the task
- Calls the beforeExecute method
- sleeps for say 20s
- RE-EXECUTE the task!
The order of 1. & 2. is not the same all the time.
Here are my questions:
- 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 theThreadPoolExecutor
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 theThreadPoolExecutor
would dump the idea of spawning a new thread - 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 ??
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您正在寻找的是 ScheduledExecutorService
据我对你的问题的理解,scheduleAtFixedRate(...) 应该完成这笔交易:
I think what you're looking for is a ScheduledExecutorService
From what I understand of your question, scheduleAtFixedRate(...) should do the deal:
ThreadPoolExecutor
知道它有一个工作线程,即使该工作线程处于RUNNABLE
、WAITING
、BLOCKED
或任何状态其他状态。beforeExecute
方法之前,任务就会从BlockingQueue
中删除。ThreadPoolExecutor
knows it has a worker thread, even if that worker isRUNNABLE
,WAITING
,BLOCKED
, or any other state.BlockingQueue
long before thebeforeExecute
method is invoked.