File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Monitor and lock? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Monitor and lock?" Watch "Monitor and lock?" New topic
Author

Monitor and lock?

lucy hu
Ranch Hand

Joined: Aug 21, 2000
Posts: 60
I am confused by this two concepts related with synchronization? could anybody explain in detail?
thank you
Udayan Naik
Ranch Hand

Joined: Oct 18, 2000
Posts: 135
Hi Lucy.Well,these two concepts are inter-related.A monitor is an object which enforces a certain policy about the execution of the code contained in it.This policy is that only one Thread should be able to execute it's code at a time.That is,only one thread of execution should be active in that monitor.In Java,this is done by using the synchronized keyword befor the methods.So,in Java terms,a Monitor is any object that has some synchronized code.
Now when a Thread wants to execute a synchronized method of a monitor object,t has to obtain the lock on that object.Once it has obtained the lock,no other Thread can enter that monitor.The monitor object now understands that some thread is executing it's code since the lock has been obtained.
The most common example is that of a mailbox(Monitor).We have postman and the house owner.There is only one key for the mailbox and it is locked.
1)The postman takes the key first(obtains the lock) and starts depositing the mail.
2)The house owner wants to read that mail.The mailbox informs him he cannot because the postman has the key.So the owner waits(wait() method).
3)The postman finishes,tells the owner (notify() method) and keeps the key back(releases the lock).
4)Now the owner can pick up the key and read his mail.Now even if the postman wants to deposit another letter,he cannot.
I hope I have not confused u by being over-enthusistic in my reply.
2)The postman finishes his task and gives the key
------------------
Come on in !! Drinks are on the house in the Big Moose Saloon !!


Udayan Naik<BR>Sun Certified Programmer for the Java 2 Platform
Jerry Pulley
Ranch Hand

Joined: Sep 19, 2000
Posts: 221
Lucy,
Different threading systems use a variety of mechanisms to aid the programmer in building threads that cooperate and don't trounce one another's memory. Monitors are a fairly high-level construct, compared with low-level things like the mutexes and semaphores you find in other threading approaches. Monitors make multithreaded programs in Java comparatively easy, at the cost of some minor loss of flexibility.
A monitor is an entity with a single lock and a wait set. In Java, any object may be used as a monitor. When you write <code>synchronized (anObject) {...}</code>, you're saying that <code>anObject</code> is the monitor for the enclosed block of code. A thread must obtain the lock belonging to <code>anObject</code> before executing any code in the block. When multiple threads are trying to enter a synchronized block, we say they are "contending for the monitor's lock". It's up to the threading system (the JVM, in Java) to determine which thread in contention actually gets the lock and enters the sync'ed block. (There is another way to denote a sync'ed block - you can mark an entire method as <code>synchronized</code>. That's just a shorthand for enclosing the body of the method in a <code>synchronized (this) {...}</code> statement.)
Once a thread enters the block, code inside the block may move the thread into the monitor's wait set; this happens when the block contains a call to the monitor's <code>wait</code> method. When a thread enters the wait set, it gives up the monitor's lock and just sits idle - it doesn't execute code and it doesn't contend for the lock. When another thread calls the monitor's <code>notify</code> or <code>notifyAll</code> method, one or all of the threads in the wait set will begin again to actively contend for the monitor's lock. When they receive the lock, they will resume execution where they left off.
Things to remember:
  • <code>wait</code>, <code>notify</code> and <code>notifyAll</code> are monitor methods - that's why they belong to <code>Object</code>. Any Java object may be used as a monitor.
  • To use an object as a monitor means to synchronize on it, by saying <code>synchronized (monitorObj) {...}</code>.
  • Udayan, here's a clarification to your post - the monitor does not have to be (and very often isn't) the object that owns the method. If you want to synchronize on the object that owns the method, then its OK to synchronize the entire method, but very often this is not what you want. In these cases, you use a <code>synchronized (someOtherObject) {...}</code> block inside the method.

  • jply

    [This message has been edited by Jerry Pulley (edited November 14, 2000).]
    Udayan Naik
    Ranch Hand

    Joined: Oct 18, 2000
    Posts: 135
    Yes Jerry,that is correct.But i wanted to just explain the basic concept first.
    Thanks for the tip.
    ------------------
    Come on in !! Drinks are on the house in the Big Moose Saloon !!
    Mohan Guru
    Greenhorn

    Joined: Nov 08, 2000
    Posts: 8
    Hai!
    please refer to the interesting producer-consumer example in P.Naughton under Synchronization.
    faiza haris
    Ranch Hand

    Joined: Oct 17, 2000
    Posts: 173
    Jerry your explanation is very helpful, but i would like you to explain your last point :
    monitor does not have to be the object that owns the method ...in the light of the following example.!
    Your input is highly appreciated!thanx
    e.g class Spreadsheet{
    int cell1,cell2,cell3;
    synchronized int sumRow(){
    return cell1 + cell2 + cell3;
    }
    synchronized void setRow(int a1,int a2,int a3){
    cell1 = a1;
    cell2 = a2;
    cell3 = a3;
    }
    ...
    }
    Jerry Pulley
    Ranch Hand

    Joined: Sep 19, 2000
    Posts: 221
    Faiza,
    Let's extend your example to hold multiple rows. To set a row or get the sum of its values, we wouldn't want to lock the entire <code>Spreadsheet</code>, just the row in question.
    <pre>
    class Spreadsheet{
    class Row {
    int cell1, cell2, cell3;
    }
    Row[] rows = new Row[10];

    int sumRow( int row ){
    synchronized (rows[row]) {
    return rows[row].cell1 + rows[row].cell2 + rows[row].cell3;
    }
    }

    void setRow( int row, int a1, int a2, int a3 ){
    synchronized (rows[row]) {
    rows[row].cell1 = a1;
    rows[row].cell2 = a2;
    rows[row].cell3 = a3;
    }
    }
    ...
    }
    </pre>
    jply
     
    jQuery in Action, 2nd edition
     
    subject: Monitor and lock?