1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package io.netty5.util.concurrent;
17  
18  import io.netty5.util.internal.UnstableApi;
19  import io.netty5.util.internal.logging.InternalLogger;
20  import io.netty5.util.internal.logging.InternalLoggerFactory;
21  
22  
23  
24  
25  public class FastThreadLocalThread extends Thread {
26  
27      private static final InternalLogger logger = InternalLoggerFactory.getInstance(FastThreadLocalThread.class);
28  
29      
30      private final boolean cleanupFastThreadLocals;
31  
32      private InternalThreadLocalMap threadLocalMap;
33  
34      public FastThreadLocalThread() {
35          cleanupFastThreadLocals = false;
36      }
37  
38      public FastThreadLocalThread(Runnable target) {
39          super(FastThreadLocalRunnable.wrap(target));
40          cleanupFastThreadLocals = true;
41      }
42  
43      public FastThreadLocalThread(ThreadGroup group, Runnable target) {
44          super(group, FastThreadLocalRunnable.wrap(target));
45          cleanupFastThreadLocals = true;
46      }
47  
48      public FastThreadLocalThread(String name) {
49          super(name);
50          cleanupFastThreadLocals = false;
51      }
52  
53      public FastThreadLocalThread(ThreadGroup group, String name) {
54          super(group, name);
55          cleanupFastThreadLocals = false;
56      }
57  
58      public FastThreadLocalThread(Runnable target, String name) {
59          super(FastThreadLocalRunnable.wrap(target), name);
60          cleanupFastThreadLocals = true;
61      }
62  
63      public FastThreadLocalThread(ThreadGroup group, Runnable target, String name) {
64          super(group, FastThreadLocalRunnable.wrap(target), name);
65          cleanupFastThreadLocals = true;
66      }
67  
68      public FastThreadLocalThread(ThreadGroup group, Runnable target, String name, long stackSize) {
69          super(group, FastThreadLocalRunnable.wrap(target), name, stackSize);
70          cleanupFastThreadLocals = true;
71      }
72  
73      
74  
75  
76  
77      final InternalThreadLocalMap threadLocalMap() {
78          if (this != Thread.currentThread() && logger.isWarnEnabled()) {
79              logger.warn(new RuntimeException("It's not thread-safe to get 'threadLocalMap' " +
80                      "which doesn't belong to the caller thread"));
81          }
82          return threadLocalMap;
83      }
84  
85      
86  
87  
88  
89      final void setThreadLocalMap(InternalThreadLocalMap threadLocalMap) {
90          if (this != Thread.currentThread() && logger.isWarnEnabled()) {
91              logger.warn(new RuntimeException("It's not thread-safe to set 'threadLocalMap' " +
92                      "which doesn't belong to the caller thread"));
93          }
94          this.threadLocalMap = threadLocalMap;
95      }
96  
97      
98  
99  
100     @UnstableApi
101     public boolean willCleanupFastThreadLocals() {
102         return cleanupFastThreadLocals;
103     }
104 
105     
106 
107 
108     @UnstableApi
109     public static boolean willCleanupFastThreadLocals(Thread thread) {
110         return thread instanceof FastThreadLocalThread &&
111                 ((FastThreadLocalThread) thread).willCleanupFastThreadLocals();
112     }
113 }