Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The line:</p> <pre><code>if (before != null) { before.incrementAndGet(); } </code></pre> <p>Will lock the current Thread until <code>before.incrementAndGet()</code> acquires the lock (if you must know, there is in fact no lock, there is a <code>while(true)</code> and a <a href="http://en.wikipedia.org/wiki/Compare-and-swap" rel="nofollow noreferrer">compare-and-swap</a> method) and returns the long value (that you are not using).</p> <p>You can make it asynchronous by calling that specific method in a Thread of its own, thus not blocking the current Thread.</p> <p>To do this, I believe you already know how: use <code>Thread.start()</code>, an <code>ExecutorService</code> or a <code>FutureTask</code> (check "<a href="https://stackoverflow.com/questions/1842734/how-to-asynchronously-call-a-method-in-java">How to asynchronously call a method in Java</a>" on how to do it in an elegant fashion).</p> <p>In case I'm not clear, here's a solution using <code>FutureTask</code>:</p> <pre><code>public class BenchMarkTest { public static void main(String[] args) { ExecutorService executor = Executors.newFixedThreadPool(5); int threadNum = 2; ExecutorService taskExecutor = Executors.newFixedThreadPool(threadNum); List&lt;FutureTask&lt;Long&gt;&gt; taskList = new ArrayList&lt;FutureTask&lt;Long&gt;&gt;(); try { for (int i = 0; i &lt; 3 * 5; i++) { executor.submit(new ThreadTask(i, taskExecutor, taskList)); } executor.shutdown(); executor.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); } catch (InterruptedException e) { } for (FutureTask&lt;Long&gt; futureTask : taskList) { futureTask.get(); // doing a job similar to joining threads } taskExecutor.shutdown(); } } </code></pre> <p><code>ThreadTask</code> class:</p> <pre><code>class ThreadTask implements Runnable { private int id; public static ConcurrentHashMap&lt;Long, AtomicLong&gt; selectHistogram = new ConcurrentHashMap&lt;Long, AtomicLong&gt;(); private ExecutorService taskExecutor; private List&lt;FutureTask&lt;Long&gt;&gt; taskList; public ThreadTask(int id, ExecutorService taskExecutor, List&lt;FutureTask&lt;Long&gt;&gt; taskList) { this.id = id; this.taskExecutor = taskExecutor; this.taskList = taskList; } @Override public void run() { long start = System.nanoTime(); attributes = beClient.getAttributes(columnsList); long end = System.nanoTime() - start; final AtomicLong before = selectHistogram.putIfAbsent(end / 1000000L, new AtomicLong(1L)); if (before != null) { FutureTask&lt;Long&gt; futureTask = new FutureTask&lt;Long&gt;(new Callable&lt;Long&gt;() { public Long call() { return before.incrementAndGet(); } }); taskList.add(futureTask); taskExecutor.execute(futureTask); } } } </code></pre> <h2>Update:</h2> <p>I thought of a little possible improvement: Instead of telling the <code>taskExecutor</code> to execute the <code>futureTask</code> in the <code>ThreadTask</code> class, it may be better to postpone the tasks' executions to the end of the <code>main</code> method. I mean:</p> <p>Remove the line below of <code>ThreadTask.run()</code>:</p> <pre><code> taskExecutor.execute(futureTask); </code></pre> <p>And, in the <code>main()</code> method, where you have:</p> <pre><code> for (FutureTask&lt;Long&gt; futureTask : taskList) { futureTask.get(); // doing a job similar to joining threads } taskExecutor.shutdown(); </code></pre> <p>Add the the execution of the tasks, thus having:</p> <pre><code> taskExecutor.invokeAll(taskList); for (FutureTask&lt;Long&gt; futureTask : taskList) { futureTask.get(); // doing a job similar to joining threads } taskExecutor.shutdown(); </code></pre> <p>(Also, you can remove <code>ThreadTask</code>'s <code>ExecutorService</code> field, as it will no longer use it.)</p> <p><strong>This way, there is very little overhead while you are executing the benchmark (the overhead is adding an object to the <code>taskList</code> and nothing else).</strong></p> <h2><strong>Full updated code:</strong></h2> <pre><code>public class BenchMarkTest { public static void main(String[] args) { ExecutorService executor = Executors.newFixedThreadPool(5); List&lt;FutureTask&lt;Long&gt;&gt; taskList = new ArrayList&lt;FutureTask&lt;Long&gt;&gt;(); try { for (int i = 0; i &lt; 3 * 5; i++) { executor.submit(new ThreadTask(i, taskList)); } executor.shutdown(); executor.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); } catch (InterruptedException e) { } int threadNum = 2; ExecutorService taskExecutor = Executors.newFixedThreadPool(threadNum); taskExecutor.invokeAll(taskList); for (FutureTask&lt;Long&gt; futureTask : taskList) { futureTask.get(); // doing a job similar to joining threads } taskExecutor.shutdown(); } } </code></pre> <p>-</p> <pre><code>class ThreadTask implements Runnable { private int id; public static ConcurrentHashMap&lt;Long, AtomicLong&gt; selectHistogram = new ConcurrentHashMap&lt;Long, AtomicLong&gt;(); private List&lt;FutureTask&lt;Long&gt;&gt; taskList; public ThreadTask(int id, List&lt;FutureTask&lt;Long&gt;&gt; taskList) { this.id = id; this.taskList = taskList; } @Override public void run() { long start = System.nanoTime(); attributes = beClient.getAttributes(columnsList); long end = System.nanoTime() - start; final AtomicLong before = selectHistogram.putIfAbsent(end / 1000000L, new AtomicLong(1L)); if (before != null) { FutureTask&lt;Long&gt; futureTask = new FutureTask&lt;Long&gt;(new Callable&lt;Long&gt;() { public Long call() { return before.incrementAndGet(); } }); taskList.add(futureTask); } } } </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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