wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Reg : Synchronization access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Reg : Synchronization access" Watch "Reg : Synchronization access" New topic
Author

Reg : Synchronization access

Rajesh Nagaraju
Ranch Hand

Joined: Nov 27, 2003
Posts: 57
Hi All,

I have been asked regularly at interviews this question,

Consider the following code,



If thread 1 has obtained the lock for method2(), Can thread2 have access to method1() which is not synchronized

My answer : Yes, as I feel unsynchronized methods should not bother about checking for a lock. Otherwise if you synchronize a method you can as well synchronize the other methods. However the interviewer said I was wrong, not sure why?

Also what happens if method2 is static, how does this affect the access to method1.


Thanks and Regards
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1753
    
    7

You are right, and its easy to verify.
Just create a simple counter POJO with a single int field and an unsynchronized getter/setter pair. Create two runnable implementations. One that, in an infinite loop, simply gets and displays the counter value along with the name of the current thread, increments the counter and then goes to sleep for any number of milliseconds (using Thread.sleep(), as it doesn't mess with any locks). And another implementation that does the same thing, but synchronizes the entire run() method on the intrinsic lock of the POJO instance. Then create and start two Thread instances. First two that use the synchronized Runnable implementation, followed by another run that mixes them up. You should notice that in the first run the thread that first aquires the lock of the POJO instance will be the only one doing the displaying/incrementing, because the other thread will be blocked and unable to obtain the lock. In the second run, while the synchonized implementation will still hold the POJO's lock forever, the other thread won't be blocked by it, because it never attempts to obtain the lock. You should see both threds happily displaying and incrementing the counter - albeit probably inconsistently.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Rajesh Nagaraju wrote:If thread 1 has obtained the lock for method2(), Can thread2 have access to method1() which is not synchronized

My answer : Yes, ... However the interviewer said I was wrong, not sure why?


If this is the scenario the interviewer presented, then he is wrong. method1() as written has no synchronized lock check, and so entry into the method is uncontrolled, and allowed independent of who has a lock on method2().


Also what happens if method2 is static, how does this affect the access to method1.


If method2() is a static synchronized method, what Object does it hold a lock on? How might you think that was different from when it is non-static in this scenario?


Steve
Sujeet Kumar Jaiswal
Greenhorn

Joined: Mar 07, 2010
Posts: 17
Rajesh Nagaraju wrote:

Also what happens if method2 is static, how does this affect the access to method1.


When the method2 is static then thread1 will get the class lock but still it will bring no impact on thread2's access to method1.
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Synchronized static methods get in line to secure the class object lock. Also,
it bothers me to hear about methods being locked or blocked, as if other
threads cannot use them. Objects are locked, not methods. Synchronized
methods can happily operate on many different objects simultaneously,
per JVM thread control, if their locks are available.

Jim ... ...


BEE MBA PMP SCJP-6
Rajesh Nagaraju
Ranch Hand

Joined: Nov 27, 2003
Posts: 57
Jim Hoglund wrote: Objects are locked, not methods. Synchronized
methods can happily operate on many different objects simultaneously,
per JVM thread control, if their locks are available.

Jim ... ...


A further thought on this,



Since the lock on method1 is the Class and the lock on method2 is the instance variable, they can run without a dependency on their locks. Am I right?

Sujeet Kumar Jaiswal
Greenhorn

Joined: Mar 07, 2010
Posts: 17
Thread running method1 has class lock and Thread running method2 has object lock. They can run concurrently. They will not impact each other's execution.
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Please stop talking about method locks! If you redirect your
thinking to focus on object locks, many situations that now
seem confusing will suddenly become clear.

Jim ... ...
 
 
subject: Reg : Synchronization access
 
Similar Threads
Thread synchronization
Synchronized blocks
locks threads
lock
need a clarification in synchronization