• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


What does the write (twp1) thread do?

Henry
[ May 26, 2005: Message edited by: Henry Wong ]
 
Grant Baxter
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic