• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on synchronization.

 
Raj Shekhar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am confused at the way my code is behaving. My undersanding of synchronization on a object is that only one thread which has the lock of that object will be able to enter the synchronized block. However, I am seeing different results.
Please check out the following code (modified from the DVD example of Max's book):
I have an infinite loop in the synchornized block to deliberately force a thread to continue to hold the lock. Once I call the 'reserve' method, that thread should have the lock for reservedDVDs object which is shared by all DVDDatabase objects. However, when I create one more DVDDatabase object and call the reserve method on a different DVDdatabase object, the thread can still execute the first print statement in the synchronized block.
I am bit a confused how it is possible. the second thread should not be able to reach this statement as the first thread still have the lock on reserved DVDs object. Can one of you please help me understand, how this is poissible.?
I appreciate any comments.
Raj

 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
H Raj,
Your example wouldn't compile, so I tweaked it a it. Try the following.

HTH,
M
[ February 08, 2004: Message edited by: Max Habibi ]
 
Raj Shekhar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Max,
Thanks for checking my post. I modified my code posting so that it is simple and did not inlcude the logic I had under the while loop to make it infinite. I have executed your code also and I saw the same behaviour.
This what I have noticed. Please comment on what you think is happenning.
1. Stared the DVDDatabase program using 'java DVDDatabase'
the program started and I see the message indicating that the thread is in synchronized block.
2. At this point the thread above has lock over the static 'reservedDVds' member of the DVDDatabase class. I let run so that this thread continues to hold the lock for the vector.
3. I start another DVDDatabase program simultaneously from the same machine using 'java DVDDatabse'.
I see that this thread also entered the synchronized block.
How is this possible since the thread in step still has the lock for reservedDVDs.
I appreciate your feedback
thanks,
Raj
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raj
In step 3 you are starting a brand new Java Virtual Machine (JVM). Mutual exclusion locks (mutexes) are per JVM, so the instance of DVDDatabase created in step 3 has a completely different mutex than the instance of DVDDatabase running in the other JVM started in step 1.
Regards, Andrew
 
Raj Shekhar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Andrew.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic