114 * be invoked for the thread. Normally, this method is invoked at
115 * most once per thread, but it may be invoked again in case of
116 * subsequent invocations of {@link #remove} followed by {@link #get}.
117 *
118 * <p>This implementation simply returns {@code null}; if the
119 * programmer desires thread-local variables to have an initial
120 * value other than {@code null}, {@code ThreadLocal} must be
121 * subclassed, and this method overridden. Typically, an
122 * anonymous inner class will be used.
123 *
124 * @return the initial value for this thread-local
125 */
126 protected T initialValue() {
127 return null;
128 }
129
130 /**
131 * Creates a thread local variable. The initial value of the variable is
132 * determined by invoking the {@code get} method on the {@code Supplier}.
133 *
134 * @param supplier the supplier to be used to determine the initial value
135 * @return a new thread local variable
136 * @throws NullPointerException if the specified supplier is null
137 * @since 1.8
138 */
139 public static <T> ThreadLocal<T> withInitial(Supplier<? extends T> supplier) {
140 return new SuppliedThreadLocal<>(supplier);
141 }
142
143 /**
144 * Creates a thread local variable.
145 * @see #withInitial(java.util.function.Supplier)
146 */
147 public ThreadLocal() {
148 }
149
150 /**
151 * Returns the value in the current thread's copy of this
152 * thread-local variable. If the variable has no value for the
153 * current thread, it is first initialized to the value returned
154 * by an invocation of the {@link #initialValue} method.
155 *
156 * @return the current thread's value of this thread-local
157 */
158 public T get() {
159 Thread t = Thread.currentThread();
|
114 * be invoked for the thread. Normally, this method is invoked at
115 * most once per thread, but it may be invoked again in case of
116 * subsequent invocations of {@link #remove} followed by {@link #get}.
117 *
118 * <p>This implementation simply returns {@code null}; if the
119 * programmer desires thread-local variables to have an initial
120 * value other than {@code null}, {@code ThreadLocal} must be
121 * subclassed, and this method overridden. Typically, an
122 * anonymous inner class will be used.
123 *
124 * @return the initial value for this thread-local
125 */
126 protected T initialValue() {
127 return null;
128 }
129
130 /**
131 * Creates a thread local variable. The initial value of the variable is
132 * determined by invoking the {@code get} method on the {@code Supplier}.
133 *
134 * @param <S> the type of the thread local's value
135 * @param supplier the supplier to be used to determine the initial value
136 * @return a new thread local variable
137 * @throws NullPointerException if the specified supplier is null
138 * @since 1.8
139 */
140 public static <S> ThreadLocal<S> withInitial(Supplier<? extends S> supplier) {
141 return new SuppliedThreadLocal<>(supplier);
142 }
143
144 /**
145 * Creates a thread local variable.
146 * @see #withInitial(java.util.function.Supplier)
147 */
148 public ThreadLocal() {
149 }
150
151 /**
152 * Returns the value in the current thread's copy of this
153 * thread-local variable. If the variable has no value for the
154 * current thread, it is first initialized to the value returned
155 * by an invocation of the {@link #initialValue} method.
156 *
157 * @return the current thread's value of this thread-local
158 */
159 public T get() {
160 Thread t = Thread.currentThread();
|