mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-19 14:16:20 +00:00
feat[thread local]: FastThreadLocalAdapter
This commit is contained in:
1 parent
54209d9cc9
commit
62c36b1749
1 file changed
+29
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user