Note that there are some explanatory texts on larger screens.

plurals
  1. POincrementAndGet method of AtomicLong is blocking call?
    primarykey
    data
    text
    <p>I am working on <code>Multithreaded code</code> from which I am trying to measure how much time a particular method is taking as I am trying to benchmark most of our teammate codes as I am doing <code>Load and Performance</code> testing of our <code>Client code</code> and then our <code>Service code</code>.</p> <p>So for this performance measurement, I am using-</p> <p><code>System.nanoTime();</code></p> <p>And I am having Multithreaded code from which I am spawning multiple threads and trying to measure how much time that code is taking.</p> <p>Below is the sample example by which I am trying to measure the performance of any code- In the below code I am trying to measure-</p> <p><code>beClient.getAttributes method</code></p> <p>Below is the code-</p> <pre><code>public class BenchMarkTest { public static void main(String[] args) { ExecutorService executor = Executors.newFixedThreadPool(5); try { for (int i = 0; i &lt; 3 * 5; i++) { executor.submit(new ThreadTask(i)); } executor.shutdown(); executor.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); } catch (InterruptedException e) { } } } </code></pre> <p>Below is the class that implements Runnable interface</p> <pre><code>class ThreadTask implements Runnable { private int id; public static ConcurrentHashMap&lt;Long, AtomicLong&gt; selectHistogram = new ConcurrentHashMap&lt;Long, AtomicLong&gt;(); public ThreadTask(int id) { this.id = id; } @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) { before.incrementAndGet(); } } } </code></pre> <p>Whatever code I want to measure, I usually put the below line just above that method</p> <p><code>long start = System.nanoTime();</code></p> <p>And these two lines after the same method but with different <code>ConcurrentHashMap</code></p> <pre><code>long end = System.nanoTime() - start; final AtomicLong before = selectHistogram.putIfAbsent(end / 1000000L, new AtomicLong(1L)); if (before != null) { before.incrementAndGet(); } </code></pre> <p>Today I had the meeting with one of my senior folks and he said <code>incrementAndGet</code> method of <code>ConcurrentHashMap</code> is a blocking call. So your thread will be waiting for some time there. </p> <p>And he asked me to make that <code>Asynchronous call</code>.</p> <p>Is there any possibility of making that Asynchronous call?</p> <p>Because in all our client code and service code to measure the performance of each method, I am using the same above three lines that I usually put before and after each method to measure the performance of those method. And after the program finishes, I am printing it out the result from those maps.</p> <p>So now I am thinking of making that <code>Asynchronous call</code>? Can anyone help me to do that?</p> <p><strong>Basically, I am trying to measure the performance of a particular method in an asynchronous manner so that each thread won't wait and gets blocked.</strong></p> <p>I think, I can do this using <code>Futures</code>. Can anyone provide an example related to that?</p> <p>Thanks for the help.</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.
 

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