Results 1 to 5 of 5
  1. #1
    johnmerlino is offline Member
    Join Date
    May 2014
    Posts
    54
    Rep Power
    0

    Default Guarded Blocks and Threads

    I'm reading the following section of the Oracle docs:

    Guarded Blocks (The Java™ Tutorials > Essential Classes > Concurrency)

    We have multiple threads. One of them sets the joy flag to true. The other waits until joy flag is set to true in order to print to the output stream. Rather than squander processer resources with a while loop, we choose to use the wait method of Object which suspends execution of thread. When the other thread throws an exception, we check the loop condition again.

    Java Code:
    public synchronized void guardedJoy() {
        // This guard only loops once for each special event, which may not
        // be the event we're waiting for.
        while(!joy) {
            try {
                wait();
            } catch (InterruptedException e) {}
        }
        System.out.println("Joy and efficiency have been achieved!");
    }
    The documentation goes on to state the following:
    When a thread invokes d.wait, it must own the intrinsic lock for d — otherwise an error is thrown. Invoking wait inside a synchronized method is a simple way to acquire the intrinsic lock. When wait is invoked, the thread releases the lock and suspends execution.
    The statement seems somewhat contradictory. When we invoke wait inside a synchronized method, is the intrinsic lock acquired or released? I thought it was the synchronized keyword itself that acquired the intrinsic lock.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Guarded Blocks and Threads

    I think your assumption is correct: the synchronized keyword itself that acquired the intrinsic lock
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    johnmerlino is offline Member
    Join Date
    May 2014
    Posts
    54
    Rep Power
    0

    Default Re: Guarded Blocks and Threads

    I'm trying to understand the flow of it:

    When we call wait() on an object in a method, that object must own the intrinsic lock, otherwise wait() will throw an error when another thread's object tries to invoke it. Therefore, to ensure the object owns the intrinsic lock, we declare the method as synchronized. Therefore, another thread must block until the intrinsic lock is released. At the moment wait is invoked, the thread who owned the lock releases the lock and suspends execution. Another thread can then acquire the same lock by invoking the guardJoy method, and invoke Object.notifyAll, informing all threads waiting on that lock that something important has happened. Some time after the second thread has released the lock, the first thread reacquires the lock and resumes by returning from the invocation of wait.

    I still doesn't see the function of notifyAll in this example.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Guarded Blocks and Threads

    I'd change the first part by replacing object with thread: that thread must own the intrinsic lock, otherwise wait() will throw an error when another thread tries to invoke it. Therefore, to ensure the thread owns the intrinsic lock ...

    Each object has a lock and a queue (for threads waiting for the lock).
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,414
    Rep Power
    5

    Default Re: Guarded Blocks and Threads

    Quote Originally Posted by johnmerlino View Post
    Another thread can then acquire the same lock by invoking the guardJoy method, and invoke Object.notifyAll

    I still doesn't see the function of notifyAll in this example.
    Your first statement is not correct. Once the wait() is invoked, it releases the lock as you said. Then another synchronized method or block, totally unrelated to the guardJoy() method, may now acquire the same lock (it must be synchronized on the same lock). Then the notify or notifyAll method may be invoked.

    I find notifyAll usually more useful than notify since it wakes up all the threads waiting on the same lock. By carefully controlling the guard, one can control which thread continues execution.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Static Blocks
    By penguinCoder in forum New To Java
    Replies: 6
    Last Post: 10-13-2012, 03:22 AM
  2. problem with a guarded block !
    By pheonix in forum New To Java
    Replies: 3
    Last Post: 01-03-2011, 12:10 AM
  3. Java Blocks??
    By abimaran in forum New To Java
    Replies: 24
    Last Post: 11-07-2009, 03:22 PM
  4. Blocks Language 0.1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 09-22-2007, 11:21 PM
  5. Task Blocks 0.5
    By johnt in forum Java Software
    Replies: 0
    Last Post: 08-08-2007, 08:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •