wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads 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 "Threads" Watch "Threads" New topic
Author

Threads

swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

I have recently gone through a mock test question saying: "Synchronized statements with identical expressions can be nested".
Which I thought to be false because in my opinion it is useless to acquire lock over something you have already locked. But it didn't actually( the answer is true): why so..?

Then I tried this-
But this is giving compile time error as follows on line#9:Help me please..where am I going wrong ?

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

I think its small "s" and not "S" on line 9.


Mohamed Sanaulla | My Blog
Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
Well as for your compiler error: Synchronized != synchronized.
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

thanks mohamed and Kavin..
I apologize for that silly mistake and there is no compiler error now.

But what do you say about its significance. Can we use this scenario in some worth-full way?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3598
    
  14

Swaraj, did you edit your post after the solutions were given? Please refrain from doing so, it makes the topic very confusing for other readers.
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

Stephan van Hulst wrote:Swaraj, did you edit your post after the solutions were given? Please refrain from doing so, it makes the topic very confusing for other readers.

Hello Stephnan, I have changed it to original post now. sorry
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3598
    
  14

Thanks
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

Stephan van Hulst wrote:Thanks

Hey, what about synchronizing over the same expression in nested synchronized blocks.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3598
    
  14

Well, yes. You are correct. It is useless.

But how does the compiler know it will be useless? To know that, the compiler would have to understand the meaning of the expression you give in the declaration of the synchronized block. "this" is a simple example, but what if you use a non-final reference to an object? The compiler won't know if it's the same lock the thread already holds.
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

Stephan van Hulst wrote:Well, yes. You are correct. It is useless.

But how does the compiler know it will be useless? To know that, the compiler would have to understand the meaning of the expression you give in the declaration of the synchronized block. "this" is a simple example, but what if you use a non-final reference to an object? The compiler won't know if it's the same lock the thread already holds.


yeh, you are right in case of non-final reference variable the object on which the thread will attempt to acquire lock might change..
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18529
    
  40

swaraj gupta wrote:"Synchronized statements with identical expressions can be nested".
Which I thought to be false because in my opinion it is useless to acquire lock over something you have already locked. But it didn't actually( the answer is true): why so..?


While I agree that the example is kinda pointless, the ability to nest synchronized locks (ie. acquire a lock more than once) is not. For example, you have two public methods. And both public methods are synchronized. So, if you call either method, you will have to acquire the "this" object.

If one of these synchronized methods, calls the other synchronized method, then you are acquiring the lock on the "this" object twice. If nested synchronized locks were not allowed, then it would not be possible for either of these methods to call the other.


And.... if you are going to allow nested lock grabs, then what would be the reason to disallow nested synchronized statements?

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

This wikipedia note on re-entrant / recursive locks should be a good read -> http://en.wikipedia.org/wiki/Reentrant_mutex


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
 
 
subject: Threads
 
Similar Threads
Beginner: Threads vs. Runnables
Doubt about deadlock using join and synchronized
synchronization and locks
Question On Object Locks.
Thread Synchronization Q