[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Channels versus Methods



Richard Beton wrote:
> 
> Thomas Umland wrote:
> 
> >   public synchronized Object read (CallChannelFunctionInterface
> >                                    function) {
> >     if (data.getState () == ChannelDataStore.EMPTY) {
> 
> Is this the pathological wait/notify Pooh-trap? I think you might need
>     while (data.getState () == ChannelDataStore.EMPTY) {
> 
> instead. (Someone will correct me if I'm wrong, no doubt)
> 
> Rick


Rick,

to make things easier for me I just copied the original JCSP code ;-)

And you are right, the code looks a bit strange. But I think the problem
comes from the InterruptedException and a "while" won't really help:
what are the semantics of a caught exception? Which state is still
correct after an exception occured and which is not? 

I would prefer not catching the exception at all and instead make the
whole read statement fail:

public synchronized Object read (CallChannelFunctionInterface  
                                   function) throws InterruptedException
{
                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    if (data.getState () == ChannelDataStore.EMPTY) wait ();
    Object value = data.get ();
    data.putReturn (function.accept(value));  // this is new
    notify ();
    return value;
}

This looks nicer, doesn't it?

Thomas