Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange concurrency behaviour on Java 6 Mac OS X Lion
    primarykey
    data
    text
    <p>I've been experimenting with difference between intrinsic locks and <code>java.util.concurrent.ReentrantLock</code> for some time now. I've found very strange thing. Consider following code:</p> <pre><code>public class WriteOnceRunAnywhere { private static long counter = 0; public static void main(String[] args) throws InterruptedException { final int numThreads = 2; final int numIterations = Integer.MAX_VALUE; Runnable inc = new Runnable() { public void run() { for (int i = 0; i &lt; numIterations; i++) { increment(); if (i % 10000000 == 0) System.out.println(Thread.currentThread().getName()); } } }; for (int i = 0; i &lt; numThreads; i++) new Thread(inc).start(); } public static synchronized void increment() { counter++; } } </code></pre> <p>Simple thing, no fancy stuff. Right? Happens to break! Most likely when you run it, it won't end. After some ping-pong between threads you'll see only one thread is actually running. The other <em>hangs</em> forever:</p> <blockquote> <p>Thread-1 Thread-2 Thread-1 Thread-2 Thread-1 ... Thread-2 Thread-2 Thread-2 Thread-2 Thread-2 Thread-2 Thread-2 Thread-2 Thread-2 ...</p> </blockquote> <p>After that, java process can't accept <code>jvisualvm</code> connection. CPU load drops and continuously stays at about 1.0%. </p> <blockquote> <p>Mac OS X Lion 10.7.2 (11C74), 2.53 GHz Intel Core i5</p> <p>java version "1.6.0_29" Java(TM) SE Runtime Environment (build 1.6.0_29-b11-402-11M3527) Java HotSpot(TM) 64-Bit Server VM (build 20.4-b02-402, mixed mode)</p> </blockquote> <p>Can someone tell me what the heck is happening here?</p> <p><strong>UPD</strong> Looks like the bug will be fixed in 1.6.30 <a href="https://stackoverflow.com/a/9088122/381105">see</a></p>
    singulars
    1. This table or related slice is empty.
    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.
 

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