Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reg : Synchronization access

 
Rajesh Nagaraju
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sujeet Kumar Jaiswal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ... ...
 
Rajesh Nagaraju
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ... ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic