ThreadPoolExecutor-ArrayBlockingQueue ...在从队列中删除元素之前要等待 [英] ThreadPoolExecutor - ArrayBlockingQueue ... to wait before it removes an element form the Queue

查看:310
本文介绍了ThreadPoolExecutor-ArrayBlockingQueue ...在从队列中删除元素之前要等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调整执行以下操作的线程:

I am trying to Tune a thread which does the following:

只有1个线程[CorePoolSize =0, maxPoolSize = 1]

使用的队列是 Quesize = 20

背景:
线程尝试读取请求并对其执行操作.

BackGround:
The thread tries to read a request and perform an operation on it.

但是,最终,请求的增加如此之多,以至于线程一直很忙,并消耗了1个CPU,这使它成为了资源消耗.

HOWEVER, eventually the requests have increased so much that the thread is always busy and consume 1 CPU which makes it a resource hog.

我想做的是,不定期采样请求并进行处理.其他请求可以安全地忽略.

What I want to do it , instead sample the requests at intervals and process them . Other requests can be safely ignored.

我要做的是在操作"功能中进入休眠状态,以便对于每个任务,线程都休眠一段时间并释放CPU.

What I would have to do is put a sleep in "operation" function so that for each task the thread sleeps for sometime and releases the CPU.

问题:
但是,我想知道是否有一种使用队列的方法,该队列本身在读取下一个元素之前会休眠一段时间.这将是理想的,因为在执行过程中休眠任务并保持执行不完全对我来说并不是最好的选择.

Quesiton:
However , I was wondering if there is a way to use a queue which basically itself sleeps for sometime before it reads the next element. This would be ideal since sleeping a task in the middle of execution and keeping the execution incomplete just doesn't sound the best to me.

如果您对任务还有其他建议,请告诉我

Please let me know if you have any other suggestions as well for the tasks

谢谢.

我在此处 将maxpool的大小更正为1 [匆忙编写] ..感谢蒂姆指出来.

I have added a follow-up question here corrected the maxpool size to be 1 [written in a haste] .. thanks tim for pointing it out.

推荐答案

该线程消耗100%CPU的原因是因为它被赋予了更多的工作量而无法处理.在任务之间添加延迟不会解决此问题.只会使事情变得更糟.

The reason the thread is consuming 100% CPU is because it is given more work than it can process. Adding a delay between tasks is not going to fix this problem. It is just make things worse.

相反,您应该查看为什么您的任务消耗了如此多的CPU,例如使用探查器并进行更改,以减少CPU的消耗,直到发现线程可以继续运行,并且不再消耗100%的CPU.

Instead you should look at WHY your tasks are consuming so much CPU e.g. with a profiler and change them so that consume less CPU until you find that your thread can keep up and it no longer consumes 100% cpu.

这篇关于ThreadPoolExecutor-ArrayBlockingQueue ...在从队列中删除元素之前要等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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