1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @summary Basic test for Thread(ThreadGroup,Runnable,String,long,boolean)
  27  */
  28 
  29 public class ITLConstructor {
  30     static InheritableThreadLocal<Integer> n = new InheritableThreadLocal<>() {
  31         protected Integer initialValue() {
  32             return 0;
  33         }
  34 
  35         protected Integer childValue(Integer parentValue) {
  36             return parentValue + 1;
  37         }
  38     };
  39 
  40     static final int CHILD_THREAD_COUNT = 10;
  41 
  42     public static void main(String args[]) throws Exception {
  43         test(true);
  44         test(false);
  45     }
  46 
  47     static void test(boolean inherit) throws Exception {
  48         // concurrent access to separate indexes is ok
  49         int[] x = new int[CHILD_THREAD_COUNT];
  50         Thread child = new Thread(Thread.currentThread().getThreadGroup(),
  51                                   new AnotherRunnable(0, x, inherit),
  52                                   "ITLConstructor-thread-"+(0),
  53                                    0,
  54                                    inherit);
  55         child.start();
  56         child.join(); // waits for *all* threads to complete
  57 
  58         // Check results
  59         for(int i=0; i<CHILD_THREAD_COUNT; i++) {
  60             int expectedValue = 1;
  61             if (inherit)
  62                 expectedValue = i+1;
  63 
  64             if (x[i] != expectedValue)
  65                 throw (new Exception("Got x[" + i + "] = " + x[i]
  66                                      + ", expected: " + expectedValue));
  67         }
  68     }
  69 
  70     static class AnotherRunnable implements Runnable {
  71         final int threadId;
  72         final int[] x;
  73         final boolean inherit;
  74         AnotherRunnable(int threadId, int[] x, boolean inherit) {
  75             this.threadId = threadId;
  76             this.x = x;
  77             this.inherit = inherit;
  78         }
  79 
  80         public void run() {
  81             int itlValue = n.get();
  82 
  83             if (threadId < CHILD_THREAD_COUNT-1) {
  84                 Thread child = new Thread(Thread.currentThread().getThreadGroup(),
  85                                           new AnotherRunnable(threadId+1, x, inherit),
  86                                           "ITLConstructor-thread-" + (threadId+1),
  87                                           0,
  88                                           inherit);
  89                 child.start();
  90                 try {
  91                     child.join();
  92                 } catch(InterruptedException e) {
  93                     throw(new RuntimeException("Interrupted", e));
  94                 }
  95             }
  96 
  97             x[threadId] = itlValue+1;
  98         }
  99     }
 100 }