Class Monitor Vs. Object Monitor
The following is my effort to differentiate Thread Monitors and priorities between them. Is this sample code infering more than that?
This my effort is trying to answer an interview question.
The question:
In a class with two sync methods and one static method with sync code block, explain the monitors, priorities and dependencies?
import java.lang.*;
publicclass MyClass{
privateint i = 0;
publicsynchronizedvoid increment(){
i++;
System.out.format("increment() : i = %d\n", i);
}
publicsynchronizedvoid decrement(){
i--;
System.out.format("decrement() : i = %d\n", i);
}
publicstaticvoid methodStaticOne(){
synchronized (MyClass.class){
System.out.println("methodStaticOne()");
}
}
publicstaticvoid methodStaticTwo(){
synchronized (MyClass.class){
System.out.println("methodStaticTwo()");
}
}
publicstaticvoid main(String[] a){
final MyClass mc =new MyClass();
Thread t1 =new Thread (new Runnable (){
publicvoid run(){
mc.increment();
}
});
Thread t2 =new Thread (new Runnable (){
publicvoid run(){
mc.decrement();
}
});
Thread t3 =new Thread (new Runnable (){
publicvoid run(){
MyClass.methodStaticOne();
}
});
Thread t4 =new Thread (new Runnable (){
publicvoid run(){
MyClass.methodStaticTwo();
}
});
t1.start();// sync
t3.start();// static
t2.start();// sync
t4.start();// static
}
}
Result:
methodStaticOne()
increment() : i = 1
decrement() : i = 0
methodStaticTwo()
# 3
I introduced delay in each method and changed the calling sequence. In every different calling combination, the static method with sync code block executed first, then the object sync method, then again static method with sync code block, then the object sync method,....... like round robin.
This happens on Win XP sp2.
import java.lang.*;
public class MyClass {
private int i = 0;
public synchronized void increment() {
i++;
try {
Thread.sleep(1000);
}
catch (java.lang.InterruptedException iEx) {
iEx.printStackTrace();
}
System.out.format("increment() : i = %d\n", i);
}
public synchronized void decrement() {
i--;
try {
Thread.sleep(1000);
}
catch (java.lang.InterruptedException iEx) {
iEx.printStackTrace();
}
System.out.format("decrement() : i = %d\n", i);
}
public static void methodStaticOne() {
synchronized (MyClass.class) {
try {
Thread.sleep(1000);
}
catch (java.lang.InterruptedException iEx) {
iEx.printStackTrace();
}
System.out.println("methodStaticOne()");
}
}
public static void methodStaticTwo() {
synchronized (MyClass.class) {
try {
Thread.sleep(1000);
}
catch (java.lang.InterruptedException iEx) {
iEx.printStackTrace();
}
System.out.println("methodStaticTwo()");
}
}
public static void main(String[] a) {
final MyClass mc = new MyClass();
Thread t1 = new Thread ( new Runnable () {
public void run() {
mc.increment();
}
});
Thread t2 = new Thread ( new Runnable () {
public void run() {
mc.decrement();
}
});
Thread t3 = new Thread ( new Runnable () {
public void run() {
MyClass.methodStaticOne();
}
});
Thread t4 = new Thread ( new Runnable () {
public void run() {
MyClass.methodStaticTwo();
}
});
// change any combination of these calls. the 'Result:' is the same on Win XP Prof sp2.
t3.start(); // static
t4.start(); // static
t1.start(); // sync
t2.start(); // sync
}
}
Result:
methodStaticOne()
increment() : i = 1
methodStaticTwo()
decrement() : i = 0
# 4
> I introduced delay in each method and changed the
> calling sequence. In every different calling
> combination, the static method with sync code block
> executed first, then the object sync method, then
> again static method with sync code block, then the
> object sync method,....... like round robin.
> This happens on Win XP sp2.
I don't understand the point your are trying to make. The program has no guaranteed ordering based on the semantics of the Java platform. Different machines and different OS can, and will, behave differently.
Do you actually have a question?