🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #8952

Re: Redirecting System.in

#8952
From: John P. McGrath
Date: Thu, 15 Jun 2000 00:00
67 lines
3441 bytes
On Wed, 14 Jun 2000 14:43:41 +0200, Jeroen Zwartepoorte wrote:

> > You do not show the code that points System.in to your
> > input stream object or the code creates the buffered
> > reader.  My guess is that your problem is with this code.
> 
> That code is very simple:
> 
>     System.setOut(new Pl0PrintStream(textArea));
>     System.setIn(new Pl0InputStream(textArea));
> 
> BufferedReader creation is also trivial:
> 
>     String sTest = new BufferedReader(new
> InputStreamReader(System.in)).readLine();

That looks right.  This may seem obvious, but are you sure that 
readLine() is being executed after the System.setIn()?  You can be sure 
by adding the line:
    System.out.println( System.in );
just before the readLine.

> > You synchronize the code that adds a character to your buffer,
> > but not the code that removes a character from the buffer.
> > I don't believe this will help if you do not do it in both
> > places.
> 
> Why not? If i make sure that when i add something to the Queue,
> i'm the only thread currently accessing that Object, then there
> is no reason to add another synchronized in the read() method?!

But that's not how synchronization works.  When you add something to the 
queue, you grab the lock for your object, which ensures that no other 
thread can grab that lock.  But it does not prevent other threads from 
accessing the object.

The way that you ensure that other threads will not access the object is 
to have all code that accesses the object synchronize with the object's 
lock.  But normally, you do not need to prevent any access, so you may 
only need to synchronize some parts of the code.

> I haven't read the document (yet), but the jdk docs state
> that read() should block until there is something to return.
> That's why i've added the Thread.sleep method.

Yes, it should block, and sleeping will cause it to block.  But it still 
wakes up 4 times per second and runs a little bit of code.  It is not 
horrible, but it is also not necessary.

Using wait/notify, you can arrange for it to block until a character is 
actually placed into the queue.  That way, there is no overhead while you 
are waiting for input.

> You do have a valid point about dead-locking: the read() method
> is currently called from a non-AWT thread, so i'm safe here.
Yes, you are safe, but it would be a good idea to add the test.  The 
really neat thing about useful classes like the one you are writing is 
that you invariably find other uses for them, and so do other people.  
Usually this is long after you have forgotten all of the implementation 
details.  So if some time in the future, you or someone else tries to use 
it from the AWT thread, which would be better: a deadlock or an exception 
with an informative message?

-- 
John P. McGrath
<JPMcGrath@fast.net>
<jpm@nscs.fast.net>
Technical Details
Message-ID: <MPG.13b22dbea380f00e9899ce@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <8i5dnc$j27@bornews.borland.com> <MPG.13b0e12eea71f5b29899c5@forums.borland.com> <8i7tuh$msd7@bornews.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,ed9d203ce9e9692c
X-Google-Attributes: gid111d45,public
From: John P. McGrath <JPMcGrath@fast.net>
Subject: Re: Redirecting System.in
Date: 2000/06/15
Message-ID: <MPG.13b22dbea380f00e9899ce@forums.borland.com>
X-Deja-AN: 634750951
References: <8i5dnc$j27@bornews.borland.com> <MPG.13b0e12eea71f5b29899c5@forums.borland.com> <8i7tuh$msd7@bornews.borland.com>
Organization: Another Netscape Collabra Server User
Newsgroups: borland.public.jbuilder.java.swing