Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>@Peter Lawrey</p> <blockquote> <blockquote> <p>Certain operations use OS calls (such as locking/cyclic barriers)</p> </blockquote> </blockquote> <p>Those are NOT OS (kernel) calls. Implemented via simple CAS (which on x86 comes w/ free memory fence as well)</p> <p>One more: dont use ArrayBlockingQueue unless you know why (you use it).</p> <p>@OP: Look at ThreadPoolExecutor, it offers excellent producer/consumer framework.</p> <p><em>Edit below</em>:</p> <p>to reduce the latency (baring the busy wait), change the queue to SynchronousQueue add the following like before starting the consumer</p> <pre><code>... consumerThread.setPriority(Thread.MAX_PRIORITY); consumerThread.start(); </code></pre> <p>This is the best you can get.</p> <hr> <p>Edit2: Here w/ sync. queue. And not printing the results.</p> <pre><code>package t1; import java.math.BigDecimal; import java.util.concurrent.CountDownLatch; import java.util.concurrent.SynchronousQueue; public class QueueTest { static final int RUNS = 250000; final SynchronousQueue&lt;Long&gt; queue = new SynchronousQueue&lt;Long&gt;(); int sleep = 1000; long[] results = new long[0]; public void start(final int runs) throws Exception { results = new long[runs]; final CountDownLatch barrier = new CountDownLatch(1); Thread consumerThread = new Thread(new Runnable() { @Override public void run() { barrier.countDown(); try { for(int i = 0; i &lt; runs; i++) { results[i] = consume(); } } catch (Exception e) { return; } } }); consumerThread.setPriority(Thread.MAX_PRIORITY); consumerThread.start(); barrier.await(); final long sleep = this.sleep; for(int i = 0; i &lt; runs; i++) { try { doProduce(sleep); } catch (Exception e) { return; } } } private void doProduce(final long sleep) throws InterruptedException { produce(); } public void produce() throws InterruptedException { queue.put(new Long(System.nanoTime()));//new Long() is faster than value of } public long consume() throws InterruptedException { long t = queue.take(); long now = System.nanoTime(); return now-t; } public static void main(String[] args) throws Throwable { QueueTest test = new QueueTest(); System.out.println("Starting + warming up..."); // Run first once, ignoring results test.sleep = 0; test.start(15000);//10k is the normal warm-up for -server hotspot // Run again, printing the results System.gc(); System.out.println("Starting again..."); test.sleep = 1000;//ignored now Thread.yield(); test.start(RUNS); long sum = 0; for (long elapsed: test.results){ sum+=elapsed; } BigDecimal elapsed = BigDecimal.valueOf(sum, 3).divide(BigDecimal.valueOf(test.results.length), BigDecimal.ROUND_HALF_UP); System.out.printf("Avg: %1.3f micros%n", elapsed); } } </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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