classpath
[Top][All Lists]
Advanced

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

Re: Note on PushbackInputStream


From: Stuart Ballard
Subject: Re: Note on PushbackInputStream
Date: Tue, 21 Aug 2001 13:21:54 -0400

John Keiser wrote:
> 
> On 21 Aug 2001 13:47:50 +0200, Dalibor Topic wrote:

> > public class WaitingPushbackInputStream extends PushbackInputStream {
> > [...]
> >     public int read() throws IOException {
> >       synchronized(this) {
> >           synchronized(in) {
> >               while(0 == available()) {
> >                   try {
> >                       wait();
> >                   }
> >                   catch (InterruptedException e) {
> >                       // ignore
> >                   }
> >               }
> >
> >               // something is available for reading,
> >               // and we've got both locks. this read
> >               // should not block at all.
> >               return super.read();
> >           }
> >       }
> >     }
> >
> >     public void unread(int oneByte) throws IOException {
> >       synchronized(this) {
> >           notifyAll();
> >           super.unread(oneByte);
> >       }
> >     }
> > }

> That's some nice code.  But how could super.unread() ever get called if
> read() is in its loop?  They're both synchronized on this.

AIUI, wait() causes all monitors to be temporarily released. So a call
to unread() will go through as soon as read() hits the line that says
wait().

But ICBVW...

Stuart.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]