Skip to content

Commit

Permalink
feat[thread local]: FastThreadLocalAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Sep 20, 2023
1 parent 54209d9 commit 62c36b1
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.zfoo.protocol.util;

import io.netty.util.concurrent.FastThreadLocal;

import java.util.function.Supplier;

/**
* @author godotg
*/
public class FastThreadLocalAdapter<T> {

private FastThreadLocal<T> fastThreadLocal;

private Supplier<T> supplier;

public FastThreadLocalAdapter(Supplier<T> supplier) {
this.supplier = supplier;
this.fastThreadLocal = new FastThreadLocal<>() {
@Override
protected T initialValue() {
return supplier.get();
}
};
}

public T get() {
return Thread.currentThread().isVirtual() ? supplier.get() : fastThreadLocal.get();
}
}

0 comments on commit 62c36b1

Please sign in to comment.