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

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to