1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package java.lang; 19 20 21/** 22 * A thread-local variable whose value is passed from parent to child thread. By 23 * default, the value of an inheritable thread-local variable of a child thread 24 * is initialized with the value of the parent thread's variable at thread 25 * creation time. However, subclasses may override {code #childValue(Object)} 26 * to provide an arbitrary function for passing the value of a parent's 27 * thread-local variable to the child's thread-local variable. 28 * 29 * @see java.lang.Thread 30 * @see java.lang.ThreadLocal 31 */ 32public class InheritableThreadLocal<T> extends ThreadLocal<T> { 33 34 /** 35 * Creates a new inheritable thread-local variable. 36 */ 37 public InheritableThreadLocal() { 38 } 39 40 /** 41 * Computes the initial value of this thread-local variable for the child 42 * thread given the parent thread's value. Called from the parent thread when 43 * creating a child thread. The default implementation returns the parent 44 * thread's value. 45 * 46 * @param parentValue the value of the variable in the parent thread. 47 * @return the initial value of the variable for the child thread. 48 */ 49 protected T childValue(T parentValue) { 50 return parentValue; 51 } 52 53 @Override 54 Values values(Thread current) { 55 return current.inheritableValues; 56 } 57 58 @Override 59 Values initializeValues(Thread current) { 60 return current.inheritableValues = new Values(); 61 } 62} 63