19.12.2010 20:37, DulcetTone пишет:
I don't think any of these solutions help, as I did not write the
writer thread and cannot alter its code.
Ah.
Unless I am missing something, use of notify/wait would only move the
sleep() to another thread, achieving nothing.
It would achieve quite a bit, but it requires making changes to the
writing side, which, as you said, is out of your control.
Is there no intrinsic, system-based means of having a read block until
the stream is closed (rather than having just temporarily run dry) or
has more data to offer? This is a key part of many I/O systems, yes?
Non-blocking IO is, as a whole, an afterthought in Java (they had to
reinvent it for NIO, and managed to forget SSL in the process).
You could try using InputStream.available(). Now sure how well it would
work with a BufferedStreamReader, so I'd recommend trying to make the
basic processing loop work first, and only then add the reader or your
own line breaking code.
-- Kostya
tone
On Dec 19, 9:22 am, Kostya Vasilyev<[email protected]> wrote:
Use Java thread synchronization:
http://www.javamex.com/tutorials/synchronization_wait_notify.shtml
Have the reader thread "wait" when there is no data and the writer
thread "notify" the reader when more data is available (and when it
should exit its processing loop).
-- Kostya
19.12.2010 16:38, DulcetTone пишет:
I have code in a worker thread that needs to efficiently read a
constantly-growing input stream from a process.
The issue is that the present design uses a sleep() for a short period
if there is no input presently available, and I'd like the attempt to
read the input stream simply *block* so it magically awakes when more
input is available. I fear this will mean a small hit on phone
responsiveness.
How does one alter code from this form to do such a thing?
BufferedReader bufferedReader =
new BufferedReader(new
InputStreamReader(process.getInputStream()));
do {
String line;
while ((line = bufferedReader.readLine()) != null) {
// new input would be processed here
}
Thread.sleep(500); // TODO: make this go away
} while (true);
--
Kostya Vasilyev -- WiFi Manager + pretty widget --http://kmansoft.wordpress.com
--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en