- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个名为 tasks
的 LinkedBlockingQueue
,但是当我调用 tasks.take()
并等待任务变得可用时, CPU 使用率为 100%。我有多个使用 tasks.take()
方法的线程(每个线程都有自己的 tasks
变量)。有谁知道为什么会发生这种情况吗?
tasks
变量的定义
private LinkedBlockingQueue<ComputerTask> tasks;
// snip
this.tasks = new LinkedBlockingQueue<ComputerTask>(100);
接受任务的代码
ComputerTask task = tasks.take();
提供新任务的代码
this.tasks.offer(task);
附注我不知道这是否是我的 Java 版本的问题,因为我还没有在任何其他计算机上测试过它。
java version "1.6.0_31"
Java(TM) SE Runtime Environment (build 1.6.0_31-b04-413-11M3623)
Java HotSpot(TM) 64-Bit Server VM (build 20.6-b01-413, mixed mode)
最佳答案
I have a
LinkedBlockingQueue
called tasks, but when I calltasks.take()
and wait for a task to become available, the CPU usage is 100%. I've got multiple threads using thetasks.take()
method (every thread has its own tasks variable).
tasks.take();
不应旋转,除非它使大量项目出队。如果队列为空,它将阻塞,因此我怀疑这不是 100% 负载的原因。我会确保您使用探查器或在 take()
周围添加调试语句,以查看它是否被多次调用。
要记住的一件事是,如果队列已满,tasks.offer(task)
会立即返回 false
。我想知道您应该使用 tasks.put(task)
,它会阻塞而不是旋转。如果您使用 offer()
来确定在哪个任务处理程序上运行任务,那么当所有任务处理程序的队列已满时,您可能会不断循环?
如果您正在进行自己的任务管理,您可以考虑使用内置的 ExecutorService 类之一。然后,您可以提交所有任务或使用阻塞队列,ExecutorService
将处理向池中的每个线程提供其任务:
// start 10 threads handling the tasks
ExecutorService threadPool = Executors.newFixedThreadPool(10);
// now submit tasks to the pools that will be run with the 10 threads
threadPool.submit(task);
...
在上面的示例中,ComputerTask
必须实现 Runnable
。
关于java - 对 CPU 影响巨大 LinkedBlockingQueue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10484693/
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
我使用 LinkedBlockingQueue 为我的应用程序进行隐式同步,但如果我使用 queue.take() 或 queue.poll() ,前几个元素总是以某种方式从队列中获取后丢失。我已经检
我正在使用 LinkedBlockingQueue 在线程之间共享一些对象。问题是这个队列中可能有一些重复项。我尝试过这个解决方案: SortedSet set = new TreeSet(new M
如果我运行以下程序,JVM 在执行后不会终止。但是,如果我取消代码中 (//newFixedThreadPool.execute(new Producer3());) 行的注释,程序将在执行后终止。我
我有一个 LinkedBlockingQueue,我想检查队列中的元素是否按一定顺序排列,而不删除元素。我希望有一种方法可以用来查看队列中的特定位置。例如,queue.peekSpot(0) 将返回头
我正在制作一个WordCounter,它有几个线程计算不同文件中的单词。除了一个小问题,我已经把一切都搞定了。 我无法找出等待线程完成的正确方法。如果我设置 Thread.sleep 等待一小段时间,
我想知道为什么不 LinkedBlockingQueue如果我们将底层数据结构更改为像 java.util.LinkedList 这样的非线程安全列表,是否可以工作?当我尝试时,我得到一个 NoSuc
我创建了一个类来计算同一目录中给定文件中的单词数。由于文件非常大,我决定使用多线程来实现多个文件的计数。 当按照下面指定的方式运行 DriverClass 时,它会卡在线程一。我究竟做错了什么?当我迭
我有一个非常基本的线程池代码。它调用位于 linkedblockingqueue 中的工作对象池。该代码只是通过回收工作对象来打印出输入数据。 我发现与以下内容一致的死锁/卡住: public cla
我的情况是单个生产者和单个消费者处理对象队列。队列可能为空有两种情况: 消费者处理对象的速度比生产者生成新对象的速度快(生产者在生成对象之前使用 I/O)。 生产者已完成对象的生成。 如果队列为空,我
我有 3 个线程:2 个消费者,ConsumerA 和 ConsumerB,以及一个 Producer。 我还有一个LinkedBlockingQueue队列 在 t=1 时:ConsumerA 调用
我有一个类似生产者-消费者的场景。 A 类生成 E 类型的对象。我必须将它保存在 A 类的静态数据结构中,因为消费者逻辑应该在 B 类中处理,它没有引用 A 的对象。LinkedBlockingQue
我看着 JDK LinkedBlockingQueue 类,迷路了。 public void put(E e) throws InterruptedException { if (e == n
我的问题涉及使用同步来订购我用于电梯程序的 LinkedBlockingQueue。 在该程序中,电梯应该尽可能高效地运行:从一楼到十楼的电梯将在上升过程中响应额外的停靠,即使这些停靠是在最初的 10
我需要一个 LinkedBlockingQueue 但我正在将原语传递给它。我添加到队列的数据速率约为每秒 4 毫秒或 256 个数据点。我遇到的问题是数据在开始时立即开始延迟,但随着时间的推移,JI
我在两个不同的线程之间使用 LinkedBlockingQueue。一个线程通过add添加数据,而另一个线程通过take接收数据。 我的问题是,我是否需要同步访问 add 和 take。 Linked
ArrayBlockingQueue public class ArrayBlockingQueue extends AbstractQueue implements BlockingQueu
如果尝试向阻塞队列添加超过阻塞队列剩余大小的集合,会发生什么?从我目前阅读的文档中并不清楚这一点。 LinkedBlockingQueue foo = new LinkedBlockingQ
在下面的伪代码中,我有一个 poll() 函数,该函数在主线程中永远被调用。当我在 poll() 中没有使用 sleep() 语句时,每分钟只有 2-3 个项目被另一个线程添加到队列中。这是否意味着轮
当我第一次关注LinkedBlockingQueue中的unlink方法时,我认为trail的第二个参数意味着它是第一个参数-p的下一个节点,但是我错了,因为我在remove方法中发现(Object
我是一名优秀的程序员,十分优秀!