aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static and Non-static Synchronized Methods never block each other Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static and Non-static Synchronized Methods never block each other" Watch "Static and Non-static Synchronized Methods never block each other" New topic
Author

Static and Non-static Synchronized Methods never block each other

priti manas duddilla
Greenhorn

Joined: Jul 17, 2007
Posts: 25
Can some one please explian the below sentence with a clear example?

A Static synchronized method (Locks on class instance) and a non-static suynchronised method (Locks on this instance) never block each other.

Thanks in advance.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Priti Manas,


A Static synchronized method (Locks on class instance) and a non-static suynchronised method (Locks on this instance) never block each other


Before we understand this, we know that threads invoking a synchronized non-static method on two different instances, shall not block each other. This is because, each object has its own copy of vulnerable data and the chance of having that data corrupted is negative, if the same data is not being accessed by two concurrent threads. This is because, threads acquire locks on two different objects (This is observable, on two different objects)

Applying the same principle to our problem now. When we invoke a synchronized static method, that thread acquires the lock at class level, actually on a Class instance ( For any class we define in java, there would a corresponding singleton object representing that class.) Threads acquire lock on that object. Meanwhile if any other thread invokes a synchronized Non-static method, this time it holds the lock at object level and actually on the instance of the class.This would be a different object. So two threads on two different objects would not block each other.

Hope I haven't confused you more
priti manas duddilla
Greenhorn

Joined: Jul 17, 2007
Posts: 25
So that means,
Locking on a class has no effect on locking on the object belonging to same class.Both are two different locks.
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
Originally posted by priti manas duddilla:
So that means,
Locking on a class has no effect on locking on the object belonging to same class.Both are two different locks.


Yes both are different locks, one "Class" instance the other "this" instance.
[ September 23, 2007: Message edited by: Ahmed Yehia ]
priti manas duddilla
Greenhorn

Joined: Jul 17, 2007
Posts: 25
That clears my doubt.Thanks every one!
 
wood burning stoves
 
subject: Static and Non-static Synchronized Methods never block each other
 
Similar Threads
need examples on these Thread related topics
New question on K&B Chapter 9 Question 2
Doubt in one question
Doubt in threads
Thrread