File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Waiting for input from PipedInputStream....thread yield? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Waiting for input from PipedInputStream....thread yield?" Watch "Waiting for input from PipedInputStream....thread yield?" New topic
Author

Waiting for input from PipedInputStream....thread yield?

Grant Baxter
Greenhorn

Joined: May 13, 2005
Posts: 4
Hello All,

I've written a test class to start playing around with PipedInputStreams and PipedOutputStreams. I need to work the kinks out of the functionality before I integrate it into my real app.

Essentially what I've done is created 2 threads -- one has a PipedOutputStream to which it writes messages, the other thread has a PipedInputStream from which it reads messages.

In my real application, I will need the information that comes in on the PipedInputStream as soon as it becomes available. However, I noticed that in my current setup, the tight "while(true)" loop of the thread that's reading the PipedInputStream is consuming huge amounts of processor time.

I thought that I understood that when I called the PipedInputStream.read() function, the thread would block and therefore yield until data became available on the PipedInputStream. Maybe it is blocking but the thread scheduler is passing control back to it so fast that it's still burning loads of cycles.

How is this general IO programming problem solved? You want your data in a timely fashion, but you don't want be behave badly and suck all of the CPU time away from other processes & threads! Should I cause my read thread to sleep for a specified time when it checks the input stream and finds no data? Any suggestions?

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18824
    
  40



What does the write (twp1) thread do?

Henry
[ May 26, 2005: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Grant Baxter
Greenhorn

Joined: May 13, 2005
Posts: 4
Hi Henry,

Sorry about the poor commenting of the code. The write thread is event driven -- when the object instance is configured to send mode (setSendMode()) it gets a JTextField where a user can enter text, and a JButton that the user clicks to cause the object to send the text out the PipedOutputStream.

There isn't anything that the write thread has to do during each pass through the run() while loop.

Grant
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18824
    
  40

There isn't anything that the write thread has to do during each pass through the run() while loop.


Your code is fine... sort of self documenting actually...

Please tell me exactly what the write thread does... (hint: I am trying to get you to find the problem yourself)

Henry
Grant Baxter
Greenhorn

Joined: May 13, 2005
Posts: 4
Hi Henry,

Found the bug w/ the help of some feedback on another forum -- I was two-timing by posting on the Java Forums as well ;-)

I was looking for the error in the wrong place. For some reason, I was thinking that once the thread finished executing its run() method, it would die. That's not the case. If I reverse the order of the while and if statements in the run() function, my problem is solved! ;-)



So the send thread executes run(), returns from the method and just sits in event driven mode. The receive thread has to continually poll the PipedInputStream by using PipedInputStream.read(). When it tries to read and no data is available it blocks and yields, just like I wanted it to.

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Waiting for input from PipedInputStream....thread yield?