jdk/test/java/lang/ThreadLocal/TLRemoveTest.java
author rupashka
Mon, 17 May 2010 17:23:18 +0400
changeset 5581 9c2282c6f080
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6938481: 4906607 is not fixed for NIMBUS L&F Reviewed-by: alexp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2004 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Basic functional test of remove method for ThreadLocal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @author Seetharama Avadhanam
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
public class TLRemoveTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
    private static final int INITIAL_VALUE = 101;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
    private static final int REMOVE_SET_VALUE = 102;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    static ThreadLocal<Integer> n = new ThreadLocal<Integer>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
        protected synchronized Integer initialValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
            return INITIAL_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public static void main(String args[]) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        int threadCount = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        final int[] removeNode = {10,20,45,38};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        // ThreadLocal values will be removed for these threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        final int[] removeAndSet = {12,34,10};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        // ThreadLocal values will be removed and sets new values..
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        Thread th[] = new Thread[threadCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        final int x[] = new int[threadCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        final Throwable exceptions[] = new Throwable[threadCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        for(int i = 0; i<threadCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            final int threadId = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            th[i] = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                    try{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                        n.set(threadId); // Sets threadId as threadlocal value...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                        for (int j = 0; j<threadId; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                            Thread.currentThread().yield();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                        // To remove the ThreadLocal ....
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                        for(int removeId  : removeNode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                            if(threadId == removeId){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                               n.remove(); // Removes ThreadLocal values..
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                               break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                        // To remove the ThreadLocal value and set new value ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                        for(int removeId  : removeAndSet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                            if(threadId == removeId){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                               n.remove(); // Removes the ThreadLocal Value...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                               n.set(REMOVE_SET_VALUE); /* Setting new Values to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                                                          ThreadLocal */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                               break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                            /* Storing the threadlocal values in 'x'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                               ...so that it can be used for checking results... */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                        x[threadId] = n.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    catch(Throwable ex){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                        exceptions[threadId] = ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            th[i].start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // Wait for the threads to finish
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        for(int i = 0; i<threadCount; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            th[i].join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // Check results
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        for(int i = 0; i<threadCount; i++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            int checkValue = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            /* If the remove method is called then the ThreadLocal value will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
             * be its initial value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            for(int removeId : removeNode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                if(removeId == i){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    checkValue = INITIAL_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            for(int removeId : removeAndSet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                if(removeId == i){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    checkValue = REMOVE_SET_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if(exceptions[i] != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                throw(exceptions[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if(x[i] != checkValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                throw(new Throwable("x[" + i + "] =" + x[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
}