View Javadoc

1   /*
2   * Copyright 2014 The Netty Project
3   *
4   * The Netty Project licenses this file to you under the Apache License,
5   * version 2.0 (the "License"); you may not use this file except in compliance
6   * with the License. You may obtain a copy of the License at:
7   *
8   *   http://www.apache.org/licenses/LICENSE-2.0
9   *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */
16  package io.netty.util.concurrent;
17  
18  import io.netty.util.internal.InternalThreadLocalMap;
19  import io.netty.util.internal.UnstableApi;
20  
21  /**
22   * A special {@link Thread} that provides fast access to {@link FastThreadLocal} variables.
23   */
24  public class FastThreadLocalThread extends Thread {
25      // This will be set to true if we have a chance to wrap the Runnable.
26      private final boolean cleanupFastThreadLocals;
27  
28      private InternalThreadLocalMap threadLocalMap;
29  
30      public FastThreadLocalThread() {
31          cleanupFastThreadLocals = false;
32      }
33  
34      public FastThreadLocalThread(Runnable target) {
35          super(FastThreadLocalRunnable.wrap(target));
36          cleanupFastThreadLocals = true;
37      }
38  
39      public FastThreadLocalThread(ThreadGroup group, Runnable target) {
40          super(group, FastThreadLocalRunnable.wrap(target));
41          cleanupFastThreadLocals = true;
42      }
43  
44      public FastThreadLocalThread(String name) {
45          super(name);
46          cleanupFastThreadLocals = false;
47      }
48  
49      public FastThreadLocalThread(ThreadGroup group, String name) {
50          super(group, name);
51          cleanupFastThreadLocals = false;
52      }
53  
54      public FastThreadLocalThread(Runnable target, String name) {
55          super(FastThreadLocalRunnable.wrap(target), name);
56          cleanupFastThreadLocals = true;
57      }
58  
59      public FastThreadLocalThread(ThreadGroup group, Runnable target, String name) {
60          super(group, FastThreadLocalRunnable.wrap(target), name);
61          cleanupFastThreadLocals = true;
62      }
63  
64      public FastThreadLocalThread(ThreadGroup group, Runnable target, String name, long stackSize) {
65          super(group, FastThreadLocalRunnable.wrap(target), name, stackSize);
66          cleanupFastThreadLocals = true;
67      }
68  
69      /**
70       * Returns the internal data structure that keeps the thread-local variables bound to this thread.
71       * Note that this method is for internal use only, and thus is subject to change at any time.
72       */
73      public final InternalThreadLocalMap threadLocalMap() {
74          return threadLocalMap;
75      }
76  
77      /**
78       * Sets the internal data structure that keeps the thread-local variables bound to this thread.
79       * Note that this method is for internal use only, and thus is subject to change at any time.
80       */
81      public final void setThreadLocalMap(InternalThreadLocalMap threadLocalMap) {
82          this.threadLocalMap = threadLocalMap;
83      }
84  
85      /**
86       * Returns {@code true} if {@link FastThreadLocal#removeAll()} will be called once {@link #run()} completes.
87       */
88      @UnstableApi
89      public boolean willCleanupFastThreadLocals() {
90          return cleanupFastThreadLocals;
91      }
92  
93      /**
94       * Returns {@code true} if {@link FastThreadLocal#removeAll()} will be called once {@link Thread#run()} completes.
95       */
96      @UnstableApi
97      public static boolean willCleanupFastThreadLocals(Thread thread) {
98          return thread instanceof FastThreadLocalThread &&
99                  ((FastThreadLocalThread) thread).willCleanupFastThreadLocals();
100     }
101 }