< prev index next >
src/java.base/share/classes/sun/nio/fs/NativeBuffers.java
Print this page
@@ -23,10 +23,11 @@
* questions.
*/
package sun.nio.fs;
+import jdk.internal.misc.JdkThreadLocal;
import jdk.internal.misc.Unsafe;
/**
* Factory for native buffers.
*/
@@ -35,12 +36,26 @@
private NativeBuffers() { }
private static final Unsafe unsafe = Unsafe.getUnsafe();
private static final int TEMP_BUF_POOL_SIZE = 3;
- private static ThreadLocal<NativeBuffer[]> threadLocal =
- new ThreadLocal<NativeBuffer[]>();
+ private static ThreadLocal<NativeBuffer[]> threadLocal = new JdkThreadLocal<>() {
+ @Override
+ protected void threadTerminated() {
+ NativeBuffer[] buffers = get();
+ // threadLocal may be initialized but with initialValue of null
+ if (buffers != null) {
+ for (int i = 0; i < TEMP_BUF_POOL_SIZE; i++) {
+ NativeBuffer buffer = buffers[i];
+ if (buffer != null) {
+ buffer.free();
+ buffers[i] = null;
+ }
+ }
+ }
+ }
+ };
/**
* Allocates a native buffer, of at least the given size, from the heap.
*/
static NativeBuffer allocNativeBuffer(int size) {
< prev index next >