Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Thread/ITLConstructor.java
38812 views
/*1* Copyright (c) 2015, 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 test for Thread(ThreadGroup,Runnable,String,long,boolean)26*/2728import sun.misc.SharedSecrets;2930import java.security.AccessControlContext;31import java.security.AccessController;32import java.security.PrivilegedAction;3334public class ITLConstructor {35static InheritableThreadLocal<Integer> n = new InheritableThreadLocal<Integer>() {36protected Integer initialValue() {37return 0;38}3940protected Integer childValue(Integer parentValue) {41return parentValue + 1;42}43};4445static final int CHILD_THREAD_COUNT = 10;4647public static void main(String args[]) throws Exception {48test();49}5051static void test() throws Exception {52// concurrent access to separate indexes is ok53int[] x = new int[CHILD_THREAD_COUNT];54Thread child = createThread(new AnotherRunnable(0, x));55child.start();56child.join(); // waits for *all* threads to complete5758// Check results59for(int i=0; i<CHILD_THREAD_COUNT; i++) {60int expectedValue = 1;61if (x[i] != expectedValue)62throw (new Exception("Got x[" + i + "] = " + x[i]63+ ", expected: " + expectedValue));64}65}6667static class AnotherRunnable implements Runnable {68final int threadId;69final int[] x;70AnotherRunnable(int threadId, int[] x) {71this.threadId = threadId;72this.x = x;73}7475public void run() {76int itlValue = n.get();7778if (threadId < CHILD_THREAD_COUNT-1) {79Thread child = createThread(80new AnotherRunnable(threadId+1, x));81child.start();82try {83child.join();84} catch(InterruptedException e) {85throw(new RuntimeException("Interrupted", e));86}87}8889x[threadId] = itlValue+1;90}91}9293static Thread createThread(final Runnable r) {94final AccessControlContext acc = AccessController.getContext();95// 4290486: doPrivileged is needed to create a thread in96// an environment that restricts "modifyThreadGroup".97return AccessController.doPrivileged(98new PrivilegedAction<Thread>() {99public Thread run() {100return SharedSecrets.getJavaLangAccess()101.newThreadWithAcc(r, acc);102}103}104);105}106}107108109