JDK 10 RFR of 8185092: Data race in FilterOutputStream.close
Claes Redestad
claes.redestad at oracle.com
Thu Jul 27 13:50:44 UTC 2017
On 07/27/2017 01:05 PM, Alan Bateman wrote:
>
> This is probably okay but would be good to check the impact on startup
> (as FileOutputStream is loaded early as part of initializing
> System.out and err).
>
> -Alan
Right, it appears FilterOutputStream is initialized in System.initPhase1,
which we're actively trying to optimize for as quick startup as possible
since it executes before some subsystems in the VM.
Adding a dependency on AtomicBoolean loads 21 classes in this sensitive
phase of the initialization (it uses VarHandles internally) and executes
around 58,000 bytecode.
I suggest we instead apply the trick recently applied to FileInputStream
to avoid a similar startup regression and use a volatile boolean:
diff -r ec72be80f81b
src/java.base/share/classes/java/io/FilterOutputStream.java
--- a/src/java.base/share/classes/java/io/FilterOutputStream.java Thu
Jul 27 15:07:33 2017 +0200
+++ b/src/java.base/share/classes/java/io/FilterOutputStream.java Thu
Jul 27 15:47:52 2017 +0200
@@ -50,7 +50,9 @@
/**
* Whether the stream is closed; implicitly initialized to false.
*/
- private boolean closed;
+ private volatile boolean closed;
+
+ private final Object closeLock = new Object();
/**
* Creates an output stream filter built on top of the specified
@@ -165,7 +167,12 @@
if (closed) {
return;
}
- closed = true;
+ synchronized (closeLock) {
+ if (closed) {
+ return;
+ }
+ closed = true;
+ }
Throwable flushException = null;
try {
Thanks!
/Claes
More information about the nio-dev
mailing list