Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try turning off <code>-XX:-UseBiasedLocking</code> It probably ping-pongs until the code is optimised (try <code>-XX:+PrintCompilation</code> and then it effectively determines the program will run faster if only one thread holds the lock. e.g. the synchronized is effectively moved outside the loop.</p> <p>You will see this behaviour often when you using multiple threads to do something which is faster to do single threaded.</p> <hr> <p>You can either have each thread alternatively increment the value or you can have one loop run until it stops (at Integer.MAX_VALUE) and then the second one.</p> <pre><code>public class CountingWithThreads { static long count1 = 0; static long count2 = 0; public static void main(String... args) throws InterruptedException { long start1 = System.nanoTime(); Thread t1a = new Thread(new CountsSingleThreaded(Integer.MAX_VALUE)); Thread t1b = new Thread(new CountsSingleThreaded(Integer.MAX_VALUE)); t1a.start(); t1b.start(); t1a.join(); t1b.join(); long time1 = System.nanoTime() - start1; System.out.printf("Single threaded, took %.1f second to count to %,d%n", time1 / 1e9, count1); long start2 = System.nanoTime(); Thread t2a = new Thread(new CountsAlternatively(true, 1000000)); Thread t2b = new Thread(new CountsAlternatively(false, 1000000)); t2a.start(); t2b.start(); t2a.join(); t2b.join(); long time2 = System.nanoTime() - start2; System.out.printf("Alternating multi-threaded took %.1f second to count to %,d%n", time2 / 1e9, count2); } static class CountsSingleThreaded implements Runnable { private final long maxValue; CountsSingleThreaded(long maxValue) { this.maxValue = maxValue; } public void run() { synchronized (CountingWithThreads.class) { for (long i = 0; i &lt; maxValue; i++) { count1++; } } } } static class CountsAlternatively implements Runnable { private final boolean even; private final long maximum; CountsAlternatively(boolean even, long maximum) { this.even = even; this.maximum = maximum; } public void run() { try { synchronized (CountingWithThreads.class) { while (count2 &lt; maximum) if (((count2 &amp; 1) == 0) == even) { count2++; CountingWithThreads.class.notify(); } else { CountingWithThreads.class.wait(); } } } catch (InterruptedException e) { e.printStackTrace(); } } } } </code></pre> <p>prints</p> <pre><code>Single threaded, took 2.3 second to count to 4,294,967,294 Alternating multi-threaded took 3.4 second to count to 1,000,000 </code></pre> <p>Running one thread to completion one at a time is the most efficient. If you force strict alternating of the counters (the most extreme example of mult-threading) it is over 1000x slower.</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.
    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