Credits

Friday, August 13, 2010

Java's Syncronized Block

In  the previous post, the sample program made use of synchronized methods, but, synchronized block can be used to perform the same task.

Find below the revised program.

[+/-] show/hide


/** Based on example from Sun Thread tutorial */

class Reentrant {

public void a() {

synchronized(this){ // use of syncronization block
b();
System.out.println("here I am, in a()");
}}

public synchronized void b() { // use of syncronization method
System.out.println("here I am, in b()");
}
}

class TestReentrant {
public static void main(String[] args) {
Reentrant r = new Reentrant();
r.a();
}
}

AddThis