Results 1 to 5 of 5
  1. #1
    l_m_b is offline Member
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Default [SOLVED] Interesting question about Semaphore

    Hello, guys!

    I have been reading “Java Concurrency in Practice” and in chapter 14 I saw a code from the Semaphore’s implementation which is backed by an AbstractQueuedSynchronizer.

    protected int tryAcquireShared(int acquires) {
    while (true) {
    int available = getState();
    int remaining = available - acquires;
    if (remaining < 0
    || compareAndSetState(available, remaining))
    return remaining;
    }
    }
    This puzzles me a bit. What I cannot understand is the following. Suppose we have available 5 permits but thread A requests 10. Then it calculates remaining = -5. In the mean time another thread(s) release 20 permits. Now there are enough permits for Thread A but their acquisition will be impossible because tryAcquireShared returns negative number and the thread will block.

    What do I miss? :confused:

    Thanks!
    Last edited by l_m_b; 02-10-2009 at 05:38 PM.

  2. #2
    Join Date
    Oct 2008
    Posts
    68
    Rep Power
    0

    Default

    I havn't read this book. You may want to explain more on the problem context.

    what is author try to say in the book. May be its a good idea to put the summary on forum ...



    ____________________________________________
    Priya,
    Cooking is Fun | Eat Healthy Stay Fit | Sweets | Raita | Bread | Dal| Snacks

  3. #3
    l_m_b is offline Member
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Default

    This is a nice book. I would say - a must read. Review is available on the official book site, but I'm still not able to post links because I have less than 20 posts :eek:

    In short the author explained the implementation of synchronizers using AbstractQueuedSynchronizer. Here is a quote:

    A synchronizer supporting exclusive acquisition should implement the protected methods TRyAcquire, TRyRelease, and isHeldExclusively, and those supporting shared acquisition should implement tryAcquireShared and TRyReleaseShared. The acquire, acquireShared, release, and releaseShared methods in AQS call the TRy forms of these methods in the synchronizer subclass to determine if the operation can proceed. The synchronizer subclass can use getState, setState, and compareAndSetState to examine and update the state according to its acquire and release semantics, and informs the base class through the return status whether the attempt to acquire or release the synchronizer was successful. For example, returning a negative value from TRyAcquireShared indicates acquisition failure; returning zero indicates the synchronizer was acquired exclusively; and returning a positive value indicates the synchronizer was acquired nonexclusively. The TRyRelease and TRyReleaseShared methods should return true if the release may have unblocked threads attempting to acquire the synchronizer.
    Here is the release implementation which seems ok to me:

    protected boolean tryReleaseShared(int releases) {
    while (true) {
    int p = getState();
    if (compareAndSetState(p, p + releases))
    return true;
    }
    }
    I'll have to spend some time during the weekend in exploring these issues :D

  4. #4
    l_m_b is offline Member
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Default Answer

    I think I understood the enigma. It lies in the queuing mechanics in AbstractQueuedSynchronizer. In my example let us name the threads Thread A and Thread B. Thread A first reserves 20 permits. To reserve the permits first it is queued in the synchronizer’s queue. Then Thread B comes into play. It is queued in after thread A (it is its successor).

    Then the described racing condition happens in tryAcquireShared. Thread B mistakenly decides that not enough permits are available. That means that it should “park” which may or may not result in its suspension (see LockSupport.park(Object) ).

    Now after thread A releases the permits it will “unpark” its successor which will lead to another call to tryAcquireShared(Object). This time it will return the correct number of permits.

  5. #5
    piyu.sha is offline Member
    Join Date
    Oct 2008
    Posts
    31
    Rep Power
    0

    Default

    Would you like to mark this post as resolved ? It helps everyone.

    Thanks

    ---------------------
    Music | HTS | Remix
    Live life king size
    [Lucene]

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. Interesting Experiment
    By uncommon in forum Advanced Java
    Replies: 13
    Last Post: 12-20-2008, 11:30 PM
  3. Interesting topic in JComboBox
    By success21061985 in forum NetBeans
    Replies: 0
    Last Post: 08-21-2008, 12:43 PM
  4. Tricky but very interesting problem
    By ravjot28 in forum New To Java
    Replies: 4
    Last Post: 06-26-2008, 01:43 PM
  5. Question
    By ayoood in forum New To Java
    Replies: 16
    Last Post: 05-21-2008, 02:23 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
  •