blob: 79221f8277d9208694da17fa128087035595646d [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
2 * Copyright 1999 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @summary Basic functional test of InheritableThreadLocal
27 * @author Josh Bloch
28 */
29
30public class Basic {
31 static InheritableThreadLocal n = new InheritableThreadLocal() {
32 protected Object initialValue() {
33 return new Integer(0);
34 }
35
36 protected Object childValue(Object parentValue) {
37 return new Integer(((Integer)parentValue).intValue() + 1);
38 }
39 };
40
41 static int threadCount = 100;
42 static int x[];
43
44 public static void main(String args[]) throws Exception {
45 x = new int[threadCount];
46 Thread progenitor = new MyThread();
47 progenitor.start();
48
49 // Wait for *all* threads to complete
50 progenitor.join();
51
52 // Check results
53 for(int i=0; i<threadCount; i++)
54 if (x[i] != i)
55 throw(new Exception("x[" + i + "] =" + x[i]));
56 }
57
58 private static class MyThread extends Thread {
59 public void run() {
60 Thread child = null;
61 if (((Integer)(n.get())).intValue() < threadCount-1) {
62 child = new MyThread();
63 child.start();
64 }
65 Thread.currentThread().yield();
66
67 int threadId = ((Integer)(n.get())).intValue();
68 for (int j=0; j<threadId; j++) {
69 x[threadId]++;
70 Thread.currentThread().yield();
71 }
72
73 // Wait for child (if any)
74 if (child != null) {
75 try {
76 child.join();
77 } catch(InterruptedException e) {
78 throw(new RuntimeException("Interrupted"));
79 }
80 }
81 }
82 }
83}