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()

[4144 byte] By [sree_at_worka] at [2007-11-26 18:14:00]
# 1
This is a cr oss post. http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=27&t=002373
cooper6a at 2007-7-9 5:47:16 > top of Java-index,Core,Core APIs...
# 2

The question and description makes no sense.

All objects have an associated monitor, including Class objects. A synchronized block acquires the monitor of the referenced object at the start and releases it at the end.

The threads could run in any order so the output could be one of a number of things.

davidholmesa at 2007-7-9 5:47:16 > top of Java-index,Core,Core APIs...
# 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

sree_at_worka at 2007-7-9 5:47:16 > top of Java-index,Core,Core APIs...
# 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?

davidholmesa at 2007-7-9 5:47:16 > top of Java-index,Core,Core APIs...