Skip to content

Instantly share code, notes, and snippets.

@tomgullo
Created August 16, 2013 17:52
Show Gist options
  • Select an option

  • Save tomgullo/6251995 to your computer and use it in GitHub Desktop.

Select an option

Save tomgullo/6251995 to your computer and use it in GitHub Desktop.
java synchronization threads test
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