Path: blob/master/test/jdk/java/lang/InheritableThreadLocal/ITLRemoveTest.java
41149 views
/*1* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Basic functional test of remove method for InheritableThreadLocal26* @author Seetharama Avadhanam27*/2829public class ITLRemoveTest {30private static final int INITIAL_VALUE = Integer.MIN_VALUE;31private static final int REMOVE_SET_VALUE = Integer.MAX_VALUE;3233static InheritableThreadLocal<Integer> n = new InheritableThreadLocal<Integer>() {34protected Integer initialValue() {35return INITIAL_VALUE;36}3738protected Integer childValue(Integer parentValue) {39return(parentValue + 1);40}41};4243static int threadCount = 100;44static int x[];45static Throwable exceptions[];46static final int[] removeNode = {10,20,45,38,88};47/* ThreadLocal values will be removed for these threads. */48static final int[] removeAndSet = {12,34,10};49/* ThreadLocal values will be removed and sets new values */5051public static void main(String args[]) throws Throwable {52x = new int[threadCount];53exceptions = new Throwable[threadCount];5455Thread progenitor = new MyThread();56progenitor.start();5758// Wait for *all* threads to complete59progenitor.join();6061for(int i = 0; i<threadCount; i++){62int checkValue = i+INITIAL_VALUE;6364/* If the remove method is called then the ThreadLocal value will65* be its initial value */66for(int removeId : removeNode)67if(removeId == i){68checkValue = INITIAL_VALUE;69break;70}7172for(int removeId : removeAndSet)73if(removeId == i){74checkValue = REMOVE_SET_VALUE;75break;76}7778if(exceptions[i] != null)79throw(exceptions[i]);80if(x[i] != checkValue)81throw(new Throwable("x[" + i + "] =" + x[i]));82}83}84private static class MyThread extends Thread {85public void run() {8687Thread child = null;88int threadId=0;89try{90threadId = n.get();91// Creating child thread...92if (threadId < (threadCount-1+INITIAL_VALUE)) {93child = new MyThread();94child.start();95}9697for (int j = 0; j<threadId; j++)98Thread.currentThread().yield();99100101// To remove the ThreadLocal value...102for(int removeId : removeNode)103if((threadId-INITIAL_VALUE) == removeId){104n.remove();105break;106}107108// To remove the ThreadLocal value and set new value ...109for(int removeId : removeAndSet)110if((threadId-INITIAL_VALUE) == removeId){111n.remove();112n.set(REMOVE_SET_VALUE);113break;114}115x[threadId-INITIAL_VALUE] = n.get();116}catch(Throwable ex){117exceptions[threadId-INITIAL_VALUE] = ex;118}119// Wait for child (if any)120if (child != null) {121try {122child.join();123} catch(InterruptedException e) {124throw(new RuntimeException("Interrupted"));125}126}127}128}129}130131132