1

我正在使用 BlockingQueue(LinkedBlockingQueue) 在多个线程之间同步数据。请看下面的图片。

主线程是一个生产者,它生产对象,然后将它们放入每个消费者的队列中(线程 2-10)。需要强调的是,每个消费者都有自己的队列,并且每个产生的对象都会进入所有消费者的队列。

生产者运行速度比消费者快得多,因此我们可以假设在消费者运行期间队列不应为空。put()当任何消费者的队列达到其容量(由生产者使用)时,生产者将被阻塞。消费者使用 . 从队列中获取对象take()

使用此设置,我假设消费者很少(如果可能的话)在队列为空时等待。但是,从我附在下面的图片中,我可以看到,有时,所有的消费者都必须在队列中等待被对象填充;在等待期间,我可以看到生产者正在运行。

这不是我对BlockingQueue的理解,我假设只要生产者生产一些东西并放入队列中,消费者就应该开始工作。为什么消费者线程有这么长的等待时间超出了我的理解。

有人可以解释一下吗?有没有简单的方法来分析这种应用程序?

线程等待

4

1 回答 1

1

It is not what my understanding of BlockingQueue, I had assumed that as long as the producer produce something and put in the queue, the consumer should start work. Why there such big waiting time on the consumer thread is beyond my understanding.

This most likely is not a problem with the BlockingQueue.

  1. You may be incorrect and the producer is falling behind the consumers. Maybe the producer is bursting and once and a while is reading from a loaded disk and falls behind.

  2. Maybe the consumers are blocking on some other object and not the BlockingQueue. What are the consumers doing with the data? Are they writing it to disk or sending it over the network? Any other operations that they do that can block?

  3. Another possibility is that only a couple of threads are keeping up with the producer and the so a number of the threads are waiting in a LIFO thread order. In other words, if a thread had just finished, maybe it takes the latest element added by the producer. So a couple of threads stay busy and other threads stay in blocked more. I'm not sure if that's how your queue is implemented.

Is there any easy way to profile this kinds of application?

One thing to do is to look at your stack traces to see where each of the threads is blocked. This will give you a better idea if your threads are actually waiting on the blocking queue. You can use jconsole to look at the threads live or send a QUIT signal every so often.

于 2013-05-22T14:58:40.597 回答