Reproduce java. Io. ioexception: stream closed exception

Source: Internet
Author: User
In the morning, QA in the United States reported an exception:

Caused by: Java. Io. ioexception: stream closed
At java. Io. bufferedinputstream. getinifopen (bufferedinputstream. Java: 134)
At java. Io. bufferedinputstream. Fill (bufferedinputstream. Java: 218)
At java. Io. bufferedinputstream. Read (bufferedinputstream. Java: 235)
At ........

This exception occurs when information is read from an input stream. In our current system, this exception is abnormal, especially it is not easy to reproduce. I and the local QA were not reproduced.

Although the bug cannot be reproduced, this exception occurs in the following scenario: multiple threads index the same input stream. After a thread is executed, the inputstream is disabled, and the thread reading information from the input stream will throw Java. io. ioexception: stream closed exception.

I wrote a short test program to simulate this scenario, as shown below:

Public class iotest ...{
Public static void main (string [] ARGs )...{
File F = new file ("C:/test/StoreTest-1.xml ");
Java. Io. bufferedreader BR = NULL;
Try ...{
BR = new java. Io. bufferedreader (New java. Io. filereader (f ));
} Catch (filenotfoundexception e )...{}

Final java. Io. bufferedreader br1 = BR;
Final java. Io. bufferedreader Br2 = BR;

Thread t = new thread ()...{
Public void run ()...{
Try ...{
For (string line = NULL; (line = br1.readline ())! = NULL ;)...{
System. Out. println ("process stdout :");
Try ...{
Thread. Sleep (1000 );
} Catch (interruptedexception e )...{}
}
} Catch (ioexception IOE )...{
IOE. printstacktrace ();
}
}
};

Thread t2 = new thread ()...{
Public void run ()...{
Try ...{
For (string line = NULL; (line = br2.readline ())! = NULL ;)...{}
System. Out. println ("process stdout2 :");
Br2.close ();
} Catch (ioexception IOE )...{
IOE. printstacktrace ();
}
}
};

T. Start ();
T2.start ();
}
}

After executing this program, the following output is displayed:
Process stdout:
Process stdout2:
Java. Io. ioexception: stream closed
At java. Io. bufferedreader. ensureopen (bufferedreader. Java: 97)
At java. Io. bufferedreader. Readline (bufferedreader. Java: 293)
At java. Io. bufferedreader. Readline (bufferedreader. Java: 362)
At test. Common. util. iotest $ 1.run( iotest. Java: 38)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.