Path: blob/master/test/jdk/java/lang/ThreadLocal/TLRemoveTest.java
41152 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 ThreadLocal26* @author Seetharama Avadhanam27*/2829public class TLRemoveTest {30private static final int INITIAL_VALUE = 101;31private static final int REMOVE_SET_VALUE = 102;3233static ThreadLocal<Integer> n = new ThreadLocal<Integer>() {34protected synchronized Integer initialValue() {35return INITIAL_VALUE;36}37};3839public static void main(String args[]) throws Throwable {40int threadCount = 100;41final int[] removeNode = {10,20,45,38};42// ThreadLocal values will be removed for these threads.43final int[] removeAndSet = {12,34,10};44// ThreadLocal values will be removed and sets new values..4546Thread th[] = new Thread[threadCount];47final int x[] = new int[threadCount];48final Throwable exceptions[] = new Throwable[threadCount];4950for(int i = 0; i<threadCount; i++) {51final int threadId = i;52th[i] = new Thread() {53public void run() {54try{55n.set(threadId); // Sets threadId as threadlocal value...56for (int j = 0; j<threadId; j++)57Thread.currentThread().yield();5859// To remove the ThreadLocal ....60for(int removeId : removeNode)61if(threadId == removeId){62n.remove(); // Removes ThreadLocal values..63break;64}6566// To remove the ThreadLocal value and set new value ...67for(int removeId : removeAndSet)68if(threadId == removeId){69n.remove(); // Removes the ThreadLocal Value...70n.set(REMOVE_SET_VALUE); /* Setting new Values to71ThreadLocal */72break;73}74/* Storing the threadlocal values in 'x'75...so that it can be used for checking results... */76x[threadId] = n.get();77}78catch(Throwable ex){79exceptions[threadId] = ex;80}81}82};83th[i].start();84}8586// Wait for the threads to finish87for(int i = 0; i<threadCount; i++)88th[i].join();8990// Check results91for(int i = 0; i<threadCount; i++){92int checkValue = i;9394/* If the remove method is called then the ThreadLocal value will95* be its initial value */96for(int removeId : removeNode)97if(removeId == i){98checkValue = INITIAL_VALUE;99break;100}101102for(int removeId : removeAndSet)103if(removeId == i){104checkValue = REMOVE_SET_VALUE;105break;106}107108if(exceptions[i] != null)109throw(exceptions[i]);110if(x[i] != checkValue)111throw(new Throwable("x[" + i + "] =" + x[i]));112}113}114}115116117