4

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.

Community
  • 1
  • 1
Pawel
  • 1,457
  • 1
  • 11
  • 22
  • Here's [another](http://stackoverflow.com/questions/8494763/any-way-of-using-java-nio-to-interrupt-a-inputstreamread-without-closing-s) relevant question. – Andrew Logvinov Nov 19 '13 at 19:56
  • @Andrew Longvinov yes, but it is connected with `InterruptibleChannel` and the solution is connected with advice of usage of another class. – Pawel Nov 19 '13 at 20:16

1 Answers1

1

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.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • 1
    I close `ExecutorService`. You run this using terminal? I run this using NetBeans. Probably, `System.in` is somehow different. – Pawel Nov 19 '13 at 22:05
  • Yeah, they may not allow you to have access to the real `System.in`. – Gray Nov 19 '13 at 22:20