aspose file tools
The moose likes Threads and Synchronization and the fly likes Diffrence between Lock & Monitor Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Diffrence between Lock & Monitor" Watch "Diffrence between Lock & Monitor" New topic
Author

Diffrence between Lock & Monitor

Nuwan Arambage
Ranch Hand

Joined: May 05, 2010
Posts: 76
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........



Thinker
Nuwan Arambage
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 370

http://en.wikipedia.org/wiki/Monitor_%28synchronization%29

You'll find a description of implementing a monitor with a lock within the monitor description.


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Pablo Marmol
Greenhorn

Joined: Aug 23, 2010
Posts: 13
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

Joined: Aug 27, 2010
Posts: 1
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

Joined: Mar 17, 2010
Posts: 1046

Hi kunal

Welcome to JavaRanch


SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1046

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

Joined: Oct 07, 2009
Posts: 94

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.


"Learning is a Culture where your Eagerness & Curiosity plays a major Role".
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Diffrence between Lock & Monitor
 
Similar Threads
Java Thread object monitor - where does it fit into the picture
Obtaining locks Static syncronized methods
List of tricks to watch for in SCJP questions - please contribute
static synchronized vs non-static synchronized
synchronized and ReentrantLock