Path: blob/master/test/jdk/java/lang/Thread/ITLConstructor.java
41149 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*/2728public class ITLConstructor {29static InheritableThreadLocal<Integer> n = new InheritableThreadLocal<>() {30protected Integer initialValue() {31return 0;32}3334protected Integer childValue(Integer parentValue) {35return parentValue + 1;36}37};3839static final int CHILD_THREAD_COUNT = 10;4041public static void main(String args[]) throws Exception {42test(true);43test(false);44}4546static void test(boolean inherit) throws Exception {47// concurrent access to separate indexes is ok48int[] x = new int[CHILD_THREAD_COUNT];49Thread child = new Thread(Thread.currentThread().getThreadGroup(),50new AnotherRunnable(0, x, inherit),51"ITLConstructor-thread-"+(0),520,53inherit);54child.start();55child.join(); // waits for *all* threads to complete5657// Check results58for(int i=0; i<CHILD_THREAD_COUNT; i++) {59int expectedValue = 1;60if (inherit)61expectedValue = i+1;6263if (x[i] != expectedValue)64throw (new Exception("Got x[" + i + "] = " + x[i]65+ ", expected: " + expectedValue));66}67}6869static class AnotherRunnable implements Runnable {70final int threadId;71final int[] x;72final boolean inherit;73AnotherRunnable(int threadId, int[] x, boolean inherit) {74this.threadId = threadId;75this.x = x;76this.inherit = inherit;77}7879public void run() {80int itlValue = n.get();8182if (threadId < CHILD_THREAD_COUNT-1) {83Thread child = new Thread(Thread.currentThread().getThreadGroup(),84new AnotherRunnable(threadId+1, x, inherit),85"ITLConstructor-thread-" + (threadId+1),860,87inherit);88child.start();89try {90child.join();91} catch(InterruptedException e) {92throw(new RuntimeException("Interrupted", e));93}94}9596x[threadId] = itlValue+1;97}98}99}100101102