0N/A/*
2362N/A * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @summary Basic functional test of remove method for InheritableThreadLocal
0N/A * @author Seetharama Avadhanam
0N/A */
0N/A
0N/Apublic class ITLRemoveTest {
0N/A private static final int INITIAL_VALUE = Integer.MIN_VALUE;
0N/A private static final int REMOVE_SET_VALUE = Integer.MAX_VALUE;
0N/A
0N/A static InheritableThreadLocal<Integer> n = new InheritableThreadLocal<Integer>() {
0N/A protected Integer initialValue() {
0N/A return INITIAL_VALUE;
0N/A }
0N/A
0N/A protected Integer childValue(Integer parentValue) {
0N/A return(parentValue + 1);
0N/A }
0N/A };
0N/A
0N/A static int threadCount = 100;
0N/A static int x[];
0N/A static Throwable exceptions[];
0N/A static final int[] removeNode = {10,20,45,38,88};
0N/A /* ThreadLocal values will be removed for these threads. */
0N/A static final int[] removeAndSet = {12,34,10};
0N/A /* ThreadLocal values will be removed and sets new values */
0N/A
0N/A public static void main(String args[]) throws Throwable {
0N/A x = new int[threadCount];
0N/A exceptions = new Throwable[threadCount];
0N/A
0N/A Thread progenitor = new MyThread();
0N/A progenitor.start();
0N/A
0N/A // Wait for *all* threads to complete
0N/A progenitor.join();
0N/A
0N/A for(int i = 0; i<threadCount; i++){
0N/A int checkValue = i+INITIAL_VALUE;
0N/A
0N/A /* If the remove method is called then the ThreadLocal value will
0N/A * be its initial value */
0N/A for(int removeId : removeNode)
0N/A if(removeId == i){
0N/A checkValue = INITIAL_VALUE;
0N/A break;
0N/A }
0N/A
0N/A for(int removeId : removeAndSet)
0N/A if(removeId == i){
0N/A checkValue = REMOVE_SET_VALUE;
0N/A break;
0N/A }
0N/A
0N/A if(exceptions[i] != null)
0N/A throw(exceptions[i]);
0N/A if(x[i] != checkValue)
0N/A throw(new Throwable("x[" + i + "] =" + x[i]));
0N/A }
0N/A }
0N/A private static class MyThread extends Thread {
0N/A public void run() {
0N/A
0N/A Thread child = null;
0N/A int threadId=0;
0N/A try{
0N/A threadId = n.get();
0N/A // Creating child thread...
0N/A if (threadId < (threadCount-1+INITIAL_VALUE)) {
0N/A child = new MyThread();
0N/A child.start();
0N/A }
0N/A
0N/A for (int j = 0; j<threadId; j++)
0N/A Thread.currentThread().yield();
0N/A
0N/A
0N/A // To remove the ThreadLocal value...
0N/A for(int removeId : removeNode)
0N/A if((threadId-INITIAL_VALUE) == removeId){
0N/A n.remove();
0N/A break;
0N/A }
0N/A
0N/A // To remove the ThreadLocal value and set new value ...
0N/A for(int removeId : removeAndSet)
0N/A if((threadId-INITIAL_VALUE) == removeId){
0N/A n.remove();
0N/A n.set(REMOVE_SET_VALUE);
0N/A break;
0N/A }
0N/A x[threadId-INITIAL_VALUE] = n.get();
0N/A }catch(Throwable ex){
0N/A exceptions[threadId-INITIAL_VALUE] = ex;
0N/A }
0N/A // Wait for child (if any)
0N/A if (child != null) {
0N/A try {
0N/A child.join();
0N/A } catch(InterruptedException e) {
0N/A throw(new RuntimeException("Interrupted"));
0N/A }
0N/A }
0N/A }
0N/A }
0N/A}