Non-blocking read problem

I wonder if you could take a look at the following code. As some of you will know, in some cases, the only way to break out of a blocking read is to cause an exception to be thrown by attempting to close the stream. This code attempts to use that principle but doesn't work. Working code correction gets you the points!

/*
Having had some problems reading from stdout
and stderr got from the Process class, I decided
to implement a stream reader with a 'blocking
time out', that is to say a stream reader that
will allow blocking for blockingTimeout milliseconds.
The code below represents a crude test bed for proof
of concept.

The problem seems to be that once the thread reading
System.in has started blocking, an attempt to close
close System.in (yes I know that is a dubious concept, but
it will have the desired effect, i.e. to throw an exception
and stop the read) only succeeds if it is called immediately,
thus rendering my blockingTimeout completely useless!
*/
import java.util.*;
import java.io.*;

public class StreamReader {
  InputStream in;
  String type;
  OutputStream out;
  long blockingTimeout = 500;
  private Thread internalThread;

  public StreamReader(InputStream in,String type){
    this.in = in;
    this.type = type;
  }

  public static void main(String[] args){
    StreamReader sr = new StreamReader(System.in,"");
    sr.read();
  }

  public void read(){
    Runnable r = new Runnable() {
    int c = -1;
      public void run(){
        try {
          // deliberately cause a blocking read of System.in
          while((c = in.read()) > -1){
          }
        }catch(Exception e){
          System.err.println("stopped (reported from runnable)");
        }
      }
    };
    internalThread = new Thread(r);
    internalThread.start();

    try {
      // If the following is uncommented, in.close() is never called on Win 2000 [unless you press a key!]
      //Thread.sleep(blockingTimeout);
      in.close();
    }catch(Exception e){
      e.printStackTrace();
    }
  }
}
LVL 86
CEHJAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
objectsConnect With a Mentor Commented:
It looks to me like the problem you're seeing is not related to blocking reads, but rather an issue with stdin.
Your code (should) work with.

If your simply want to do non blocking IO the have a look at http://www.cs.berkeley.edu/~mdw/proj/java-nbio/
0
 
rjackmanCommented:
hi
this code is working fine on my machine
it comes out of thread on exception rather than blocking
Cheers
RJ
0
 
CEHJAuthor Commented:
Well the Thread.sleep() call at the end should be uncommented. Did you uncomment it, and if so did it work *then*?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
nebekerCommented:
With the "sleep()" call commented out, the program works fine on Solaris and NT 4.0.

With the "sleep()" call un-commented, the program works fine on Solaris, but hangs on NT 4.0 until the enter key is pressed.

My guess is that it has something to do with the way Windows handles input buffering for console apps.
0
 
objectsCommented:
Plus I'm not sure if closing stdin is a valid thing to be doing.
Changing the code to use a FileInputStream instead, works fine with the sleep uncommented.

0
 
nebekerCommented:
There's no problem with closing stdin - it's just another input stream like everything else.
0
 
SendohCommented:
hi !!
i'd try this on winnt4.0
dono is this what u want...:)

import java.util.*;
import java.io.*;

public class test implements Runnable{
 InputStream in;
 String type;
 OutputStream out;
 long blockingTimeout = 5000;
 private Thread internalThread;

 public test(InputStream in,String type){
   this.in = in;
   this.type = type;
 }

 public static void main(String[] args){
   test sr = new test(System.in,"");
   sr.read();
 }

 public void read(){
   internalThread = new Thread(this);
   internalThread.start();

   try {
     Thread newThread = Thread.currentThread();
     System.out.println("start sleep here");
     // If the following is uncommented, in.close() is never called on Win 2000 [unless you press akey!]
     newThread.sleep(blockingTimeout);
     //internalThread.sleep(blockingTimeout);
     System.out.println("end sleep");
     in.close();
     //throw(new Exception("test"));
   }catch(Exception e){
     e.printStackTrace();
   }
 }

     public void run()
     {
       int c = -1;

       try
       {
         // deliberately cause a blocking read of System.in
         while(true)
         {
             if((c = in.read())>-1)
             {
                 System.out.println("accept:"+c);
             }
         }
       }
       catch(Exception e)
       {
         System.err.println("stopped (reported from runnable)");
       }
     }
}

hope it helps.:)
0
 
CEHJAuthor Commented:
objects: I brought up the matter of closing stdin in my opening comment. As for FileInputStream, I'm looking for an 'all purpose' non-blocking read

sendoh: Your program doesn't exit on Win2k Advanced Server I'm afraid...
0
 
CEHJAuthor Commented:
Yes thanks, I've seen this library objects. I might take a look into it...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.