Created
August 16, 2013 17:52
-
-
Save tomgullo/6251995 to your computer and use it in GitHub Desktop.
java synchronization threads test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Counter { | |
| int val | |
| public Counter() { val = 0; } | |
| public /* synchronized */ void increment() { | |
| val++ | |
| } | |
| } | |
| def c = new Counter() | |
| class MyRunnable implements Runnable { | |
| def cntr | |
| public MyRunnable(cntr) { | |
| this.cntr = cntr | |
| } | |
| public void run() { | |
| cntr.increment() | |
| } | |
| } | |
| def threads = [] | |
| for (e in 0..1000) { | |
| threads << new Thread(new MyRunnable(c)) | |
| } | |
| threads.each { it.start() } | |
| threads.each { it.join() } | |
| println 'done ' + c.val |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment