TestThreadId.java revision 2362
0N/A/*
962N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation.
0N/A *
553N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
553N/A */
553N/A
0N/A/*
0N/A * @test
962N/A * @bug 6434084
962N/A * @summary Exercise ThreadLocal javadoc "demo" class ThreadId
962N/A * @author Pete Soper
962N/A */
962N/A
962N/Apublic final class TestThreadId extends Thread {
962N/A
962N/A // number of times to create threads and gather their ids
962N/A private static final int ITERATIONCOUNT = 50;
962N/A
962N/A // Threads constructed per iteration. ITERATIONCOUNT=50 and
962N/A // THREADCOUNT=50 takes about one second on a sun Blade 1000 (2x750mhz)
962N/A private static final int THREADCOUNT = 50;
962N/A
962N/A // The thread local storage object for holding per-thread ids
962N/A private static ThreadId id = new ThreadId();
962N/A
962N/A // Holds the per-thread so main method thread can collect it. JMM
962N/A // guarantees this is valid after this thread joins main method thread.
962N/A private int value;
962N/A
962N/A private synchronized int getIdValue() {
962N/A return value;
962N/A }
962N/A
962N/A // Each child thread just publishes its id value for validation
962N/A public void run() {
962N/A value = id.get();
962N/A }
962N/A
962N/A public static void main(String args[]) throws Throwable {
0N/A
0N/A // holds true corresponding to a used id value
0N/A boolean check[] = new boolean[THREADCOUNT*ITERATIONCOUNT];
0N/A
962N/A // the test threads
860N/A TestThreadId u[] = new TestThreadId[THREADCOUNT];
962N/A
860N/A for (int i = 0; i < ITERATIONCOUNT; i++) {
962N/A // Create and start the threads
860N/A for (int t=0;t<THREADCOUNT;t++) {
962N/A u[t] = new TestThreadId();
1163N/A u[t].start();
1163N/A }
1163N/A // Join with each thread and get/check its id
1163N/A for (int t=0;t<THREADCOUNT;t++) {
1163N/A try {
962N/A u[t].join();
962N/A } catch (InterruptedException e) {
860N/A throw new RuntimeException(
962N/A "TestThreadId: Failed with unexpected exception" + e);
962N/A }
860N/A try {
962N/A if (check[u[t].getIdValue()]) {
860N/A throw new RuntimeException(
962N/A "TestThreadId: Failed with duplicated id: " +
962N/A u[t].getIdValue());
860N/A } else {
962N/A check[u[t].getIdValue()] = true;
962N/A }
860N/A } catch (Exception e) {
962N/A throw new RuntimeException(
962N/A "TestThreadId: Failed with unexpected id value" + e);
860N/A }
962N/A }
860N/A }
962N/A } // main
860N/A} // TestThreadId
962N/A