Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads

 
swaraj gupta
Ranch Hand
Posts: 182
C++ Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think its small "s" and not "S" on line 9.
 
Kevin Workman
Ranch Hand
Posts: 151
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well as for your compiler error: Synchronized != synchronized.
 
swaraj gupta
Ranch Hand
Posts: 182
C++ Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4826
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
C++ Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4826
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
swaraj gupta
Ranch Hand
Posts: 182
C++ Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Thanks

Hey, what about synchronizing over the same expression in nested synchronized blocks.
 
Stephan van Hulst
Bartender
Pie
Posts: 4826
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
C++ Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20832
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This wikipedia note on re-entrant / recursive locks should be a good read -> http://en.wikipedia.org/wiki/Reentrant_mutex
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic