Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If the queue wraps around, it is possible that byte arrays get reused and overwritten before having been read by consumers. In short, you'd need to have a <code>commitGet</code> method to make sure that producers wait for consumers before overwriting an array with new data.</p> <p>However, my suggestion is that you rely on <a href="http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/BlockingQueue.html" rel="nofollow">java.util.concurrent.BlockingQueue</a> having a second queue to return them from consumers back to the producer, and on <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/ByteBuffer.html" rel="nofollow">java.nio.ByteByffer</a> to keep track of lengths. The producer would do as follows:</p> <pre><code>ByteBuffer buffer = bufferQueue.poll(); // instead of your put() buffer.put(source); // fill buffer from source MappedByteBuffer buffer.flip(); // set length to the amount written dataQueue.offer(buffer); // instead of commitPut() </code></pre> <p>The consumer would:</p> <pre><code>ByteBuffer buffer = dataQueue.poll(); // instead of your get() buffer.get(...); // use data buffer.clear(); // reset length bufferQueue.offer(buffer); // this is the missing commitGet() </code></pre> <p>You should initially insert <code>capacity</code> elements in <code>freeQueue</code>. Note however that this will still copy data once from the <code>source</code> buffer into temporary buffers in the queues, as your original code already did.</p> <p>If you really don't want to copy data (and are sure that the source does not change until all consumers have read it!), your better option is to use a single blocking queue and insert buffers obtained with <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/ByteBuffer.html#slice%28%29" rel="nofollow">ByteBuffer.slice()</a> from your source buffer for each chunk of data to be handed down to consumers. These will then be garbage collected, but should take much less memory than the byte arrays themselves.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload