简体   繁体   中英

InputStream - closing, interrupting blocking read()

Some of the input - output objects like InputStream cannot be interrupted and the source cannot be closed during reading, writing.

Example:

class InputStreamOperation1 implements Runnable
{
    static InputStream stream;

    InputStreamOperation1(InputStream stream) { this.stream = stream; }

    public void run()
    {
        System.out.println("InputStreamOperation - the beginning");
        try
        {
            stream.read(); //blocking operation
        } catch (Exception e)
        {
            System.out.println("InputStreamOperation interrupted");
        }
        System.out.println("InputStreamOperation - the end");
    }
}

Trying to interrupt:

ExecutorService executor = Executors.newCachedThreadPool();
Future<?> f = executor.submit(new InputStreamOperation1(System.in));
TimeUnit.MILLISECONDS.sleep(100);
System.out.println("Interrupting InputStream");
f.cancel(true); // Interrupts if running
System.out.println("Interrupt sent to InputStream");

Trying to close the source

Future<?> f = executor.submit(new InputStreamOperation1(System.in));
TimeUnit.MILLISECONDS.sleep(100);
System.out.println("Closing source in InpuStream");
try
{
      System.in.close();
} catch (IOException e)
{
      System.out.println("Error during closing InputStreamOperation");
}
System.out.println("Closed source in InputStream");

Both solutions are not working. It interrupts or closes after reading at least one letter.

My question: it there any way to interrupt blocking read() operation or close the source during this blocking operation?

I found something similar here - Is it possible to read from a InputStream with a timeout? . I want to know whether is any other solution (especially with interrupts or closing source) to stop the thread or the only one is connected with that tricky manner.

Both solutions are not working. It interrupts or closes after reading at least one letter.

It works for me to close System.in .

My output shows:

InputStreamOperation - the beginning
Closing source in InpuStream
Closed source in InputStream
read: -1
InputStreamOperation - the end

I added a printing of the result of the read and it is showing -1 meaning EOF when I close System.in in the other thread.

I wonder if your problem is that you are not closing your ExecutorService ? If you don't then your application won't finish.

ExecutorService executor = Executors.newCachedThreadPool();
Future<?> f = executor.submit(new InputStreamOperation1(System.in));
executor.shutdown();

If it still is not working for you then this must be OS dependent. It works for me under OSX and Linux.

My question: it there any way to interrupt blocking read() operation or close the source during this blocking operation?

Aside from closing the stream, which has always worked for me, the only other way that I know is to use a NIO InterruptibleChannel class and interrupt it.

Edit:

From the comments, you are working under Netbeans and it may be that the web wrappers protect System.in somehow for security purposes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM