2
|
1 |
/*
|
|
2 |
* Copyright 2007 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 |
* @bug 6434084
|
|
27 |
* @summary Exercise ThreadLocal javadoc "demo" class ThreadId
|
|
28 |
* @author Pete Soper
|
|
29 |
*/
|
|
30 |
|
|
31 |
public final class TestThreadId extends Thread {
|
|
32 |
|
|
33 |
// number of times to create threads and gather their ids
|
|
34 |
private static final int ITERATIONCOUNT = 50;
|
|
35 |
|
|
36 |
// Threads constructed per iteration. ITERATIONCOUNT=50 and
|
|
37 |
// THREADCOUNT=50 takes about one second on a sun Blade 1000 (2x750mhz)
|
|
38 |
private static final int THREADCOUNT = 50;
|
|
39 |
|
|
40 |
// The thread local storage object for holding per-thread ids
|
|
41 |
private static ThreadId id = new ThreadId();
|
|
42 |
|
|
43 |
// Holds the per-thread so main method thread can collect it. JMM
|
|
44 |
// guarantees this is valid after this thread joins main method thread.
|
|
45 |
private int value;
|
|
46 |
|
|
47 |
private synchronized int getIdValue() {
|
|
48 |
return value;
|
|
49 |
}
|
|
50 |
|
|
51 |
// Each child thread just publishes its id value for validation
|
|
52 |
public void run() {
|
|
53 |
value = id.get();
|
|
54 |
}
|
|
55 |
|
|
56 |
public static void main(String args[]) throws Throwable {
|
|
57 |
|
|
58 |
// holds true corresponding to a used id value
|
|
59 |
boolean check[] = new boolean[THREADCOUNT*ITERATIONCOUNT];
|
|
60 |
|
|
61 |
// the test threads
|
|
62 |
TestThreadId u[] = new TestThreadId[THREADCOUNT];
|
|
63 |
|
|
64 |
for (int i = 0; i < ITERATIONCOUNT; i++) {
|
|
65 |
// Create and start the threads
|
|
66 |
for (int t=0;t<THREADCOUNT;t++) {
|
|
67 |
u[t] = new TestThreadId();
|
|
68 |
u[t].start();
|
|
69 |
}
|
|
70 |
// Join with each thread and get/check its id
|
|
71 |
for (int t=0;t<THREADCOUNT;t++) {
|
|
72 |
try {
|
|
73 |
u[t].join();
|
|
74 |
} catch (InterruptedException e) {
|
|
75 |
throw new RuntimeException(
|
|
76 |
"TestThreadId: Failed with unexpected exception" + e);
|
|
77 |
}
|
|
78 |
try {
|
|
79 |
if (check[u[t].getIdValue()]) {
|
|
80 |
throw new RuntimeException(
|
|
81 |
"TestThreadId: Failed with duplicated id: " +
|
|
82 |
u[t].getIdValue());
|
|
83 |
} else {
|
|
84 |
check[u[t].getIdValue()] = true;
|
|
85 |
}
|
|
86 |
} catch (Exception e) {
|
|
87 |
throw new RuntimeException(
|
|
88 |
"TestThreadId: Failed with unexpected id value" + e);
|
|
89 |
}
|
|
90 |
}
|
|
91 |
}
|
|
92 |
} // main
|
|
93 |
} // TestThreadId
|