[concurrency-interest] Fwd: ReadWriteLocks and Conditions

Peter Veentjer alarmnummer at gmail.com
Tue Feb 6 09:15:11 EST 2007


---------- Forwarded message ----------
From: Peter Veentjer <alarmnummer at gmail.com>
Date: Feb 6, 2007 3:14 PM
Subject: Re: [concurrency-interest] ReadWriteLocks and Conditions
To: Tim Peierls <tim at peierls.net>


Hi Tim,

Example usage:

while(true){
        Runnable task = lendableRef.take();
        try{
                task.execute();
        }finally{
                lendableRef.takeBack(task);
        }
}


By placing a Runnable in the lendableRef, the task is being executed.
By removing the Runnable from the lendableRef (so setting a null
value) the take blocks.

And this is the interface:

package org.jph.concurrent.lendable;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * The LendableReference is a synchronization stone that makes it easy to deal
 * with a value that can be lend by one of more threads.
 * <p/>
 * Todo remark about comparison between LendableReference and
AwaitableReference.
 * <p/>
 * Todo: remark about save hand of
 * <p/>
 * Idiom:
 * <code>
 * Integer someref = lendableRef.tryTake();
 * try{
 *      ..do something
 * }finally{
 *      someRef.takeBack(someref);
 * }
 * </code>
 * <p/>
 *
 * The reference check for takeback is done based on equals method.
 * So you can send back a different object, as long as the equals
 * says they are equal.
 *
 * A LendableReference should allow that a lend reference is
 * takenback by a different thread than took the value.
 *
 * @author Peter Veentjer.
 */
public interface LendableReference<E> {

    /**
     * Takes the current reference. If no reference is available, this
     * call blocks until:
     * <ol>
     * <li>a non <tt>null</tt> comes available</li>
     * <li>the calling thread is interrupted</li>
     * </ol>
     *
     * @return the found value. This value will never be <tt>null</tt>.
     * @throws InterruptedException if interrupted while waiting.
     */
    E take() throws InterruptedException;

    /**
     * Takes the current reference. If no reference is available, this
     * call blocks until:
     * <ol>
     * <li>a non <tt>null</tt> comes available</li>
     * <li>the calling thread is interrupted</li>
     * <li>a timeout occurs</li>
     * </ol>
     * <p/>
     * If the timeout is smaller than zero, a TimeoutException is
     * thrown.
     * todo: nul timeouts.
     *
     * @param timeout how long to wait before giving up in units of
<tt>unit</tt>.
     * @param unit    a <tt>TimeUnit</tt> determining how to interpret
the <tt>timeout</tt> parameter.
     * @return the found value. If a timeout occurs, <tt>null</tt> is returned.
     * @throws NullPointerException if unit is <tt>null</tt>.
     * @throws InterruptedException if interrupted while waiting.
     * @throws TimeoutException     if a timeout occurrs.
     */
    E tryTake(long timeout, TimeUnit unit) throws
InterruptedException, TimeoutException;


    /**
     * Returns a lend reference back to this LendableReference. If a incorrect
     * reference is returned, a IncorrectReferenceTakenBackException is thrown.
     * The check on correctness is done based on the equal method. So you are
     * allowed to send back a different object, as long as it is equal.
     *
     * <p/>
     * A different thread is allowed to tryTake the reference back.
     * <p/>
     *
     * todo:
     * what happens if a bogus takeback is done? So a value is taken back
     * although nothing is lend.
     *
     * @param ref the reference taken back.
     * @throws NullPointerException if ref is <tt>null</tt>. Because a
<tt>null</tt> will
     *                              never be lend, it can't be returned.
     * @throws InvalidTakeBackException
     *                              if a incorrect reference is returned. It
     *                              depends on the implementation if this
     *                              exception is thrown. todo
     */
    void takeBack(E ref);

    /**
     * Puts a new reference into this LendableReference. This call blocks until:
     * <ol>
     * <li>the lendable reference allows the new value to be placed.
todo: explain situations</li>
     * <li>it is interrupted</li>
     * </ol>
     * <p/>
     * todo:
     * it depends on te implementation if blocking needs to tryTake place.
     * <p/>
     * todo:
     * explain why the tryExecute method now has an interrupt while
the AwaitableValue doesn't need it.
     *
     * @param newRef the new reference. The newRef is allowed to be
<tt>null</tt>.
     * @return the old reference, could be <tt>null</tt>.
     * @throws InterruptedException if interrupted while waiting.
     */
    E put(E newRef) throws InterruptedException;

    /**
     * Puts a new reference into this LendableReference. This call blocks until:
     * <ol>
     * <li></li>
     * <li>a timeout occurs</li>
     * <li>the calling thread is interrupted</li>
     * </ol>
     * <p/>
     * If the timeout is smaller than zero, a TimeoutException is
     * thrown.
     * todo: nul timeouts.
     *
     * @param newRef  the new reference. The newRef is allowed to be
<tt>null</tt>.
     * @param timeout how long to wait before giving up in units of
<tt>unit</tt>.
     * @param unit    a <tt>TimeUnit</tt> determining how to interpret
the <tt>timeout</tt> parameter.
     * @return the old reference, could be <tt>null</tt>.
     * @throws InterruptedException if interrupted while waiting.
     * @throws TimeoutException     if a timeout occurs.
     * @throws NullPointerException if unit is <tt>null</tt>.
     */
    E tryPut(E newRef, long timeout, TimeUnit unit) throws
InterruptedException, TimeoutException;

    /**
     * Returns the current reference. If no reference is available,
<tt>null</tt>
     * is returned.
     *
     * @return the current reference, if no reference is available,
<tt>null</tt> is
     *         returned.
     */
    E peek();
}



On 2/6/07, Tim Peierls <tim at peierls.net> wrote:
> On 2/6/07, Peter Veentjer <alarmnummer at gmail.com> wrote:
> > public class NewStrictLendeableReference<E> implements
> LendableReference<E> {
> >
>
> Peter,
>
> It's hard for me to reason backwards from an implementation. Maybe you could
> give us the LendableReference interface with a brief example or sketch of
> how it might be used?
>
> --tim
>


More information about the Concurrency-interest mailing list