mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-22 20:27:11 +00:00
feat[thread local]: FastThreadLocalAdapter
This commit is contained in:
@@ -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