private DeflateCompressor() { deflater = new FastThreadLocal<Deflater>() { @Override protected Deflater initialValue() { return new Deflater(); } }; inflater = new FastThreadLocal<Inflater>() { @Override protected Inflater initialValue() { return new Inflater(); } }; }
/** * Ensures that {@link FastThreadLocal#remove() FastThreadLocal.remove()} is called when the {@link Runnable#run()} * method of the given {@link Runnable} instance completes to ensure cleanup of {@link FastThreadLocal} instances. * This is especially important for direct byte buffers allocated locally for a thread. */ public static Runnable threadLocalDeallocator(Runnable r) { return () -> { try { r.run(); } finally { FastThreadLocal.removeAll(); } }; }
public static void destroy(Context ctx) { try { instance.destroyInternal(ctx); } finally { ctx.stop(); // Clean up Netty thread locals, which will also clean up any dangling threadDeathWatcher // daemons. See https://github.com/netty/netty/issues/7310 for more context. FastThreadLocal.removeAll(); } }
@Override public void run() { try { r.run(); } finally { FastThreadLocal.removeAll(); } }
@Override public void stop() throws Exception { future.channel().closeFuture(); // clean up internal Netty threads FastThreadLocal.removeAll(); FastThreadLocal.destroy(); }
public static <S> FastThreadLocal<S> withInitial(Supplier<? extends S> supplier) { checkNotNull(supplier, "supplier"); return new FastThreadLocal<S>() { @Override protected S initialValue() throws Exception { return supplier.get(); } }; }
private static <T> FastThreadLocal<SoftReference<T>> create(Supplier<? extends T> supplier) { requireNonNull(supplier); return FastThreadLocals.withInitial(() -> { final T value = supplier.get(); return value == null ? null : new SoftReference<>(value); }); }
@Benchmark public int fastThreadLocal() { int result = 0; for (FastThreadLocal<Integer> i: fastThreadLocals) { result += i.get(); } return result; }
public AWSSignatureCalculatorFactory(AWSCredentialsProvider credentialsProvider) { signatureCalculator = new FastThreadLocal<AWSSignatureCalculator>() { @Override protected AWSSignatureCalculator initialValue() { return new AWSSignatureCalculator(credentialsProvider); } }; }
private FastSoftThreadLocal(FastThreadLocal<SoftReference<T>> threadLocal) { this.threadLocal = threadLocal; }
/** * Creates a thread local variable. * * @see #withInitial(Supplier) */ public FastSoftThreadLocal() { this(new FastThreadLocal<>()); }