wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt In Thread Topic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt In Thread Topic" Watch "Doubt In Thread Topic" New topic
Author

Doubt In Thread Topic

anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
what is the meaning of this statement?

A thread acquiring the lock of a class to execute a static synchronized method,has no bearing on any thread acquiring the lock on any object of the class to execute a synchronized instance method.

please tell me
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

A thread acquiring the lock of a class to execute a static synchronized method,has no bearing on any thread acquiring the lock on any object of the class to execute a synchronized instance method.


Hi Anil,

class DemoThread extends Thread{

public void run() {

}

public synchronized static void doStuff1() { //Lock of the class

}

public void doStuff2() {
synchronized(this) { //Lock of the instance of the class
....
}
}

public static void main(String... args) {

}

}

Inside the static method, there is no "this". So a thread acquiring lock on the static method has no bearing of the lock of the syncrhonized instance method. So while a thread executing a static synchronized method, it has no bearing on any thread acquiring the lock on any object of the class to execute a synchronized instance method.

Got it?

Thanks and Regards,
cmbhatt
[ March 31, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
that means
if a thread got a lock on static Synchronized method.
other threads can execute the non static synchronized methods
is it right?

[ March 31, 2007: Message edited by: anil kumar ]
[ March 31, 2007: Message edited by: anil kumar ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi,


Meanwhile try this code,



Do you come to any conclusion after executing this code?
"While a thread is executing static block of code, other threads still can access the synchronized instance methods, and why only synchronized all other methods too except static methods because there is one and only lock of the static method per class"


Thanks and Regards,
cmbhatt
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Anil,

After editing the post, you are correct. You corrected yourself.



Thanks and Regards,
cmbhatt
[ April 01, 2007: Message edited by: Chandra Bhatt ]
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Thanks
i got it.
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Anil,

You got it what Chandra said.

Interesting point.

1)// Class Lock is Unique. If one thread obtains it. No other thread in that JVM can obtain that lock until THE thread releases it.

synchronized(Sample.class){
}

2)//Object lock may not be unique. There is a possibility at runtime that SampleObjectRef point to some other Object.

synchronized(SampleObjectRef){
}

Concentrate on threads.
All the Best.


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

Srinivasan

yes i got it.
 
jQuery in Action, 2nd edition
 
subject: Doubt In Thread Topic