Java Threads mailing list archive

Re: Channels versus Methods

From: Thomas.Umland@t-online.de (Thomas Umland)
Date: Fri, 02 Jul 1999 00:16:40 +0200

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

	


Last updated: Tue Nov 2 12:11:41 1999
Maintained by Peter Welch.