this is my first post on stackoverflow... I hope someone can help me
I have a big performance regression with Java 6 LinkedBlockingQueue
.
In the first thread i generate some objects which i push in to the queue
In the second thread i pull these objects out. The performance regression occurs when the take()
method of the LinkedBlockingQueue
is called frequently.
I monitored the whole program and the take()
method claimed the most time overall.
And the throughput goes from ~58Mb/s to 0.9Mb/s...
the queue pop and take methods ar called with a static method from this class
public class C_myMessageQueue {
private static final LinkedBlockingQueue<C_myMessageObject> x_queue = new LinkedBlockingQueue<C_myMessageObject>( 50000 );
/**
* @param message
* @throws InterruptedException
* @throws NullPointerException
*/
public static void addMyMessage( C_myMessageObject message )
throws InterruptedException, NullPointerException {
x_queue.put( message );
}
/**
* @return Die erste message der MesseageQueue
* @throws InterruptedException
*/
public static C_myMessageObject getMyMessage() throws InterruptedException {
return x_queue.take();
}
}
how can I tune the take()
method to accomplish at least 25Mb/s, or is there a other class I can use which will block when the "queue" is full or empty.
kind regards
Bart
P.S.: sorry for my bad english, I'm from Germany ;)
BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control.
Here we have a blockingQueue that has a capacity equal to 10. It means that when a producer tries to add an element to an already full queue, depending on a method that was used to add it (offer(), add() or put()), it will block until space for inserting object becomes available. Otherwise, the operations will fail.
The LinkedBlockingQueue is an optionally-bounded blocking queue based on linked nodes. It means that the LinkedBlockingQueue can be bounded, if its capacity is given, else the LinkedBlockingQueue will be unbounded. The capacity can be given as a parameter to the constructor of LinkedBlockingQueue.
Your producer thread simply puts more elements than the consumer consumes, so the queue eventually hits its capacity limit, thus the producer waits.
Consolidating my original answer since now we have basically the full picture:
LinkedBlockingQueue
(every queue has one) by doing extremely fast put()
s, where even continual take()s
, with zero further processing, cannot keep up. (By the way this shows that in this structure, on your JVM and machine anyway, put()s are at least slightly more costly than the reads). SynchronousQueue
, ConcurrentLinkedQueue
, and the upcoming TransferQueue
of jsr166y).Some suggestions:
/updated after John W. rightly pointed out my original answer was misleading
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With