debug hanging multi-thread program with monitor

Hi all,

I am writting to seek help from other java developers.

I am currently using JDB to debug a hanging multi-thread program

running under jre 1.0.5_05.

I have created a class which acts as a lock for synchronization between two threads.

class myLockObject

{

public static Object aStaticVariable = new Object();

}

Two threads, namely Thread-1 and Thread-2, are basically doing similar things as followed:

while(true)

{

synchonized(myLockObject.aStaticVariable)

{

big chunk of code

};

Thread.sleep(10000);

}

But, the program sometimes hangs.

Using JDB "where" and "threadlocks" commands, I found that thread-1 is outside the

synchonized block and thread-2 is waiting to enter the synchronized block:

Thread-1[1] where

[1] java.lang.Thread.sleep (native method)

[2] myThread.run (myThread.java:845)

Thread-1[1] threadlocks

Monitor information for thread Thread-1:

No monitors owned

Not waiting for a monitor

Thread-2[1] threadlocks

Monitor information for thread Thread-2:

No monitors owned

Waiting for monitor: instance of java.lang.Object(id=2350)

However, when I use JDB "lock" command to show the lock info of myLockObject.aStaticVariable.

It shows that it is owned by Thread-1 (first confusing info).

However, the "entry count" is 0 (second confusing info):

Thread-1[1] lock myLockObject.aStaticVariable

com.sun.tools.example.debug.expr.ParseException: Unable to complete expression.

Thread not suspended for method invoke

Owned by: Thread-1, entry count: 0

Waiting thread: Thread-2

As Thread-1 is already outside the synchronized block,

it should have released the lock. Why does it show that the lock is still

being owned by Thread-1? It made me confused.

How to explain?

Could anyone help me? Thanks in advance.

MJ

[2011 byte] By [kenny2kennya] at [2007-10-2 10:59:45]
# 1
Don't synchronize on an irrelevant object.Do synchronize on the critical data object itself.The data object might have to do wait() and notify() for the accessing threads for theirwell-ordered traffic control.
hiwaa at 2007-7-13 3:28:28 > top of Java-index,Java Essentials,Java Programming...
# 2

> class myLockObject

> {

> public static Object aStaticVariable = new

> w Object();

> }

>

> Two threads, namely Thread-1 and Thread-2, are

> basically doing similar things as followed:

>

> while(true)

> {

> synchonized(myLockObject.aStaticVariable)

> {

> big chunk of code

> };

> Thread.sleep(10000);

> }

>

>

Oh yeah. That's a helpful code snippet.

You are deadlocking. Why? Well because thread 1 is waiting for a lock on something thread 2 has but won't give up until thread 1 gives up it's lock that it has but it won't give that up until thread 2 releases the lock that it has but it won't give it up till thread 1 releases it's lock but it...

etc.

hiwaa at 2007-7-13 3:28:28 > top of Java-index,Java Essentials,Java Programming...
# 3
sorry I dont catch it.There is only one lock between two threads.IHow does it cause deadlocks ?
kenny2kennya at 2007-7-13 3:28:28 > top of Java-index,Java Essentials,Java Programming...
# 4

> Don't synchronize on an irrelevant object.

> Do synchronize on the critical data object itself.

> The data object might have to do wait() and notify()

> for the accessing threads for their

> well-ordered traffic control.

Thanks for your suggestion.

But, could you see what's wrong in my code?

I still don't know why it hangs. Thanks

kenny2kennya at 2007-7-13 3:28:28 > top of Java-index,Java Essentials,Java Programming...
# 5

> what's wrong in my code?

Read the scsi-boy's reply.

Because you didn't:

post a small demo code that is generally compilable, runnable and could reproduce your

problem. See: http://homepage1.nifty.com/algafield/sscce.html

Here is a stab in the dark, more or less:

public class BadKenny{

static Object aStaticVariable;

static Data data;

public static void main(String[] args){

aStaticVariable = new Object();

data = new Data("main");

ThreadX t1 = new ThreadX("T1");

ThreadX t2 = new ThreadX("T2");

t1.start();

t2.start();

}

static class ThreadX extends Thread{

String id;

public ThreadX(String s){

id = s;

}

public void run(){

while(true){

synchronized(aStaticVariable){

try{

data.setId(id);

data.printId();

Thread.sleep(10000); // a long task

}

catch (InterruptedException e){

e.printStackTrace();

}

}

}

}

}

static class Data{

String id;

public Data(String s){

id = s;

}

public void setId(String s){

id = s;

}

public void printId(){

System.out.println("I am a " + id);

}

}

}

public class GoodKenny{

static Data data;

public static void main(String[] args){

data = new Data("main");

ThreadX t1 = new ThreadX("T1");

ThreadX t2 = new ThreadX("T2");

t1.start();

t2.start();

}

static class ThreadX extends Thread{

String id;

public ThreadX(String s){

id = s;

}

public void run(){

while(true){

try{

data.setId(id);

data.printId();

Thread.sleep(10000); // long task

}

catch (InterruptedException e){

e.printStackTrace();

}

}

}

}

static class Data{

String id;

public Data(String s){

id = s;

}

public synchronized void setId(String s){

id = s;

}

public synchronized void printId(){

System.out.println("I am a " + id);

}

// more better

public synchronized setAndPrint(String s){

id = s;

System.out.println("I am a " + id);

}

}

}

hiwaa at 2007-7-13 3:28:28 > top of Java-index,Java Essentials,Java Programming...
# 6

> > what's wrong in my code?

> Read the scsi-boy's reply.

>

> Because you didn't:

> post a small demo code that is generally compilable,

> runnable and could reproduce your

> problem. See:

> http://homepage1.nifty.com/algafield/sscce.html

>

Thanks hiwa for your code suggestion. I will consider it seriously.

I know it is difficult for you to understand the problem fully.

But, it is difficult for me to isolate the relevant code.

So, lets ignore my deadlock problem.

If I just want to explain the following two conflicting JDB info,

any hints? Thanks in advance.

Thread-1[1] threadlocks

Monitor information for thread Thread-1:

No monitors owned

Not waiting for a monitor

Thread-1[1] lock myLockObject.aStaticVariable

com.sun.tools.example.debug.expr.ParseException: Unable to complete expression.

Thread not suspended for method invoke

Owned by: Thread-1, entry count: 0

Waiting thread: Thread-2

kenny2kennya at 2007-7-13 3:28:28 > top of Java-index,Java Essentials,Java Programming...
# 7

And, ,what does it meant by:

"Thread not suspended for method invoke"

in the following JDB message.

I am sure I have sent "suspend" command to suspend all threads already. Thanks.

Thread-1[1] lock myLockObject.aStaticVariable

com.sun.tools.example.debug.expr.ParseException: Unable to complete expression.

Thread not suspended for method invoke

Owned by: Thread-1, entry count: 0

Waiting thread: Thread-2

kenny2kennya at 2007-7-13 3:28:28 > top of Java-index,Java Essentials,Java Programming...
# 8

It's only based on my 'common sense':

> No monitors owned

Not executing synchronized block/method

> Not waiting for a monitor

Nor waiting for executing them

> Thread not suspended for method invoke

Method invoke() call is not suspended

> Owned by: Thread-1, entry count: 0

> Waiting thread: Thread-2

Thread-2 is waiting for acquiring monitor

hiwaa at 2007-7-13 3:28:28 > top of Java-index,Java Essentials,Java Programming...