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)