• Post Reply Bookmark Topic Watch Topic
  • New Topic

Diffrence between Lock & Monitor

 
Nuwan Arambage
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read several articles but still I have confused about these two terms.....
I understood these terms are related to coordinating multiple threads...

Can somebody explain the difference between "Monitor" & "Lock"...
I will be very pleased If i can have good reference link for beginners........


 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://en.wikipedia.org/wiki/Monitor_%28synchronization%29

You'll find a description of implementing a monitor with a lock within the monitor description.
 
Pablo Marmol
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris!

Normally, a monitor is on a high abstraction level than a lock. You could use a lock to control the access of a thread to a single resource. You open the lock, your thread enters, your thread exits, and you close the lock. A monitor usually is a more sophisticated thing that allows you things like readers-writers problem, producer-consumer problem or any other synchronization problem (Yes, a lock could work with this problems but in a low abstraction level. Yes, you could use machine-code too (a lower abstraction level)).

Maybe it's like difference between an array (for example) and a hashtable: You can use an array to implement a hashtable, but it's very difficult to implement an array using a hashtable. In fact, almost every monitor uses one or more locks.

I hope this helps you!
 
Kunal kunals
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

simple thing which i knw abt Monitors & locks.

Monitor : Its for certain set of code should be executed by only one thread at a time.
Which contains an shared object(resource). Monitors are set only on objects.

Locks : Locks is for each object(resource). whenever one thread gets resource it occupies locks on that object.
& at exit time tht thread just release lock of that object.

 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi kunal

Welcome to JavaRanch
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we cannot determine both lock and moniter as different because they are associated with each other.

lets see the most appropriate explaination from JLS 17.1

Each object in Java is associated with a monitor, which a thread can lock or unlock. Only one thread at a time may hold a
lock on a monitor. Any other threads attempting to lock that monitor are blocked until they can obtain a lock on that monitor.
 
karthik Suryanarayanan
Ranch Hand
Posts: 121
Java Opera Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris, that was a nice material.Could anyone please help me out with a more precise reference material, where i could learn the entire concept of monitors & Locks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!