Path: blob/master/test/jdk/java/lang/ThreadLocal/InitialValue.java
41149 views
/*1* Copyright (c) 2005, 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* @bug 502523026* @summary Tests to see that a set nested in initialValue works OK27* @author Pete Soper28*/29public class InitialValue implements Runnable {3031static ThreadLocal<String> other;32static boolean passed;3334public class MyLocal extends ThreadLocal<String> {35String val;36protected String initialValue() {37other = new ThreadLocal<String>();38// This should reuse the map that the containing get() created39// or visa versa (i.e. instead of a second map being created).40other.set("Other");41return "Initial";42}43}4445public void run() {46MyLocal l = new MyLocal();47// This should pick up the initial value48String s1 = l.get();49// And this should pick up the other local in this thread's locals map50String s2 = other.get();51if ((s2 != null) && s2.equals("Other")) {52// JMM guarantees this will be visible to53// another thread joining with this thread's54// termination: no need for this to be volatile.55passed = true;56}57}5859public static void main(String[] args) {60// Starting with Mustang the main thread already has an initialized61// ThreadLocal map at this point, so test with a second thread.62Thread t = new Thread(new InitialValue());63t.start();64try {65t.join();66} catch (InterruptedException e) {67throw new RuntimeException("Test Interrupted: failed");68}69if (!passed) {70throw new RuntimeException("Test Failed");71}72}73}747576