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 ThreadLocal
0N/A * @author Seetharama Avadhanam
0N/A */
0N/A
0N/Apublic class TLRemoveTest {
0N/A private static final int INITIAL_VALUE = 101;
0N/A private static final int REMOVE_SET_VALUE = 102;
0N/A
0N/A static ThreadLocal<Integer> n = new ThreadLocal<Integer>() {
0N/A protected synchronized Integer initialValue() {
0N/A return INITIAL_VALUE;
0N/A }
0N/A };
0N/A
0N/A public static void main(String args[]) throws Throwable {
0N/A int threadCount = 100;
0N/A final int[] removeNode = {10,20,45,38};
0N/A // ThreadLocal values will be removed for these threads.
0N/A final int[] removeAndSet = {12,34,10};
0N/A // ThreadLocal values will be removed and sets new values..
0N/A
0N/A Thread th[] = new Thread[threadCount];
0N/A final int x[] = new int[threadCount];
0N/A final Throwable exceptions[] = new Throwable[threadCount];
0N/A
0N/A for(int i = 0; i<threadCount; i++) {
0N/A final int threadId = i;
0N/A th[i] = new Thread() {
0N/A public void run() {
0N/A try{
0N/A n.set(threadId); // Sets threadId as threadlocal value...
0N/A for (int j = 0; j<threadId; j++)
0N/A Thread.currentThread().yield();
0N/A
0N/A // To remove the ThreadLocal ....
0N/A for(int removeId : removeNode)
0N/A if(threadId == removeId){
0N/A n.remove(); // Removes ThreadLocal values..
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 == removeId){
0N/A n.remove(); // Removes the ThreadLocal Value...
0N/A n.set(REMOVE_SET_VALUE); /* Setting new Values to
0N/A ThreadLocal */
0N/A break;
0N/A }
0N/A /* Storing the threadlocal values in 'x'
0N/A ...so that it can be used for checking results... */
0N/A x[threadId] = n.get();
0N/A }
0N/A catch(Throwable ex){
0N/A exceptions[threadId] = ex;
0N/A }
0N/A }
0N/A };
0N/A th[i].start();
0N/A }
0N/A
0N/A // Wait for the threads to finish
0N/A for(int i = 0; i<threadCount; i++)
0N/A th[i].join();
0N/A
0N/A // Check results
0N/A for(int i = 0; i<threadCount; i++){
0N/A int checkValue = i;
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}