Initial load
diff --git a/test/java/lang/Thread/GenerifyStackTraces.java b/test/java/lang/Thread/GenerifyStackTraces.java
new file mode 100644
index 0000000..e10080c0
--- /dev/null
+++ b/test/java/lang/Thread/GenerifyStackTraces.java
@@ -0,0 +1,181 @@
+/*
+ * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4919105
+ * @summary Generified basic unit test of Thread.getAllStackTraces()
+ * @author Mandy Chung
+ *
+ * @compile -source 1.5 GenerifyStackTraces.java
+ * @run main GenerifyStackTraces
+ */
+
+import java.util.*;
+
+public class GenerifyStackTraces {
+
+ private static Object go = new Object();
+ private static Object dumpObj = new Object();
+ private static String[] methodNames = {"run", "A", "B", "C", "Done"};
+ private static int DONE_DEPTH = 5;
+ private static boolean testFailed = false;
+
+ private static Thread one;
+ private static boolean trace = false;
+ public static void main(String[] args) throws Exception {
+ if (args.length > 0 && args[0].equals("trace")) {
+ trace = true;
+ }
+
+ one = new ThreadOne();
+ one.start();
+
+ Thread dt = new DumpThread();
+ dt.setDaemon(true);
+ dt.start();
+
+ if (testFailed) {
+ throw new RuntimeException("Test Failed.");
+ }
+ }
+
+ static class DumpThread extends Thread {
+ public void run() {
+ int depth = 2;
+ while (true) {
+ // At each iterator, wait until ThreadOne blocks
+ // to wait for thread dump.
+ // Then dump stack trace and notify ThreadOne to continue.
+ try {
+ sleep(2000);
+ dumpStacks(depth);
+ depth++;
+ finishDump();
+ } catch (Exception e) {
+ e.printStackTrace();
+ testFailed = true;
+ }
+ }
+ }
+ }
+
+ static class ThreadOne extends Thread {
+ public void run() {
+ A();
+ }
+ private void A() {
+ waitForDump();
+ B();
+ }
+ private void B() {
+ waitForDump();
+ C();
+ }
+ private void C() {
+ waitForDump();
+ Done();
+ }
+ private void Done() {
+ waitForDump();
+
+ // Get stack trace of current thread
+ StackTraceElement[] stack = getStackTrace();
+ try {
+ checkStack(this, stack, DONE_DEPTH);
+ } catch (Exception e) {
+ e.printStackTrace();
+ testFailed = true;
+ }
+ }
+
+ }
+
+
+ static private void waitForDump() {
+ synchronized(go) {
+ try {
+ go.wait();
+ } catch (Exception e) {
+ throw new RuntimeException("Unexpected exception" + e);
+ }
+ }
+ }
+
+ static private void finishDump() {
+ synchronized(go) {
+ try {
+ go.notifyAll();
+ } catch (Exception e) {
+ throw new RuntimeException("Unexpected exception" + e);
+ }
+ }
+ }
+
+ public static void dumpStacks(int depth) throws Exception {
+ // Get stack trace of another thread
+ StackTraceElement[] stack = one.getStackTrace();
+ checkStack(one, stack, depth);
+
+
+ // Get stack traces of all Threads
+ for (Map.Entry<Thread, StackTraceElement[]> entry :
+ Thread.getAllStackTraces().entrySet()) {
+ Thread t = entry.getKey();
+ stack = entry.getValue();
+ if (t == null || stack == null) {
+ throw new RuntimeException("Null thread or stacktrace returned");
+ }
+ if (t == one) {
+ checkStack(t, stack, depth);
+ }
+ }
+ }
+
+ private static void checkStack(Thread t, StackTraceElement[] stack,
+ int depth) throws Exception {
+ if (trace) {
+ printStack(t, stack);
+ }
+ int frame = stack.length - 1;
+ for (int i = 0; i < depth; i++) {
+ if (! stack[frame].getMethodName().equals(methodNames[i])) {
+ throw new RuntimeException("Expected " + methodNames[i] +
+ " in frame " + frame + " but got " +
+ stack[frame].getMethodName());
+ }
+ frame--;
+ }
+ }
+
+ private static void printStack(Thread t, StackTraceElement[] stack) {
+ System.out.println(t +
+ " stack: (length = " + stack.length + ")");
+ if (t != null) {
+ for (int j = 0; j < stack.length; j++) {
+ System.out.println(stack[j]);
+ }
+ System.out.println();
+ }
+ }
+}
diff --git a/test/java/lang/Thread/HoldsLock.java b/test/java/lang/Thread/HoldsLock.java
new file mode 100644
index 0000000..7276a99
--- /dev/null
+++ b/test/java/lang/Thread/HoldsLock.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2000 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4363076
+ * @summary Basic functional test of Thread.holdsLock(Object)
+ * @author Josh Bloch and Steffen Grarup
+ */
+
+public class HoldsLock {
+ private static Object target = null;
+
+ private static void checkLock(boolean value) {
+ if (Thread.holdsLock(target) != value)
+ throw new RuntimeException("Should be " + value);
+ }
+
+ static class LockThread extends Thread {
+ public void run() {
+ checkLock(false);
+ synchronized(target) {
+ checkLock(true);
+ }
+ checkLock(false);
+ }
+ }
+
+ public static void main(String args[]) throws Exception {
+ // Test null obj case
+ try {
+ checkLock(false);
+ throw new RuntimeException("NullPointerException not thrown");
+ } catch (NullPointerException e) {
+ };
+
+ // Test uncontested case
+ target = new Object();
+ checkLock(false);
+ synchronized(target) {
+ checkLock(true);
+ }
+ checkLock(false);
+
+ // Test contested case
+ synchronized(target) {
+ checkLock(true);
+ new LockThread().start();
+ checkLock(true);
+ Thread.sleep(100);
+ checkLock(true);
+ }
+ checkLock(false);
+ }
+}
diff --git a/test/java/lang/Thread/MainThreadTest.java b/test/java/lang/Thread/MainThreadTest.java
new file mode 100644
index 0000000..e0a77eb
--- /dev/null
+++ b/test/java/lang/Thread/MainThreadTest.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4533087
+ * @summary Test to see if the main thread is in its thread group
+ */
+
+public class MainThreadTest {
+ public static void main(String args[]) {
+ ThreadGroup tg = Thread.currentThread().getThreadGroup();
+ int n = tg.activeCount();
+ Thread[] ts = new Thread[n];
+ int m = tg.enumerate(ts);
+ for (int i = 0; i < ts.length; i++) {
+ if (Thread.currentThread() == ts[i]) {
+ return;
+ }
+ }
+ throw new RuntimeException(
+ "Current thread is not in its own thread group!");
+ }
+}
diff --git a/test/java/lang/Thread/NullStackTrace.java b/test/java/lang/Thread/NullStackTrace.java
new file mode 100644
index 0000000..43ef51e
--- /dev/null
+++ b/test/java/lang/Thread/NullStackTrace.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6571589
+ * @summary java.lang.Thread#getStackTrace() returns null.
+ */
+
+public class NullStackTrace
+{
+ static final int TIMES = 1000;
+
+ public static void main(String[] args)
+ {
+ for (int i=0; i<TIMES; i++) {
+ Thread t = new Thread();
+ t.start();
+
+ StackTraceElement[] ste = t.getStackTrace();
+ if (ste == null)
+ throw new RuntimeException("Failed: Thread.getStackTrace should not return null");
+ }
+ }
+}
diff --git a/test/java/lang/Thread/StackTraces.java b/test/java/lang/Thread/StackTraces.java
new file mode 100644
index 0000000..504af23
--- /dev/null
+++ b/test/java/lang/Thread/StackTraces.java
@@ -0,0 +1,182 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4593133
+ * @summary Basic unit test of Thread.getStackTraces()
+ * @author Mandy Chung
+ */
+
+import java.util.*;
+
+public class StackTraces {
+
+ private static Object go = new Object();
+ private static Object dumpObj = new Object();
+ private static String[] methodNames = {"run", "A", "B", "C", "Done"};
+ private static int DONE_DEPTH = 5;
+ private static boolean testFailed = false;
+
+ private static Thread one;
+ private static boolean trace = false;
+ public static void main(String[] args) throws Exception {
+ if (args.length > 0 && args[0].equals("trace")) {
+ trace = true;
+ }
+
+ one = new ThreadOne();
+ one.start();
+
+ Thread dt = new DumpThread();
+ dt.setDaemon(true);
+ dt.start();
+
+ if (testFailed) {
+ throw new RuntimeException("Test Failed.");
+ }
+ }
+
+ static class DumpThread extends Thread {
+ public void run() {
+ int depth = 2;
+ while (true) {
+ // At each iterator, wait until ThreadOne blocks
+ // to wait for thread dump.
+ // Then dump stack trace and notify ThreadOne to continue.
+ try {
+ sleep(2000);
+ dumpStacks(depth);
+ depth++;
+ finishDump();
+ } catch (Exception e) {
+ e.printStackTrace();
+ testFailed = true;
+ }
+ }
+ }
+ }
+
+ static class ThreadOne extends Thread {
+ public void run() {
+ A();
+ }
+ private void A() {
+ waitForDump();
+ B();
+ }
+ private void B() {
+ waitForDump();
+ C();
+ }
+ private void C() {
+ waitForDump();
+ Done();
+ }
+ private void Done() {
+ waitForDump();
+
+ // Get stack trace of current thread
+ StackTraceElement[] stack = getStackTrace();
+ try {
+ checkStack(this, stack, DONE_DEPTH);
+ } catch (Exception e) {
+ e.printStackTrace();
+ testFailed = true;
+ }
+ }
+
+ }
+
+
+ static private void waitForDump() {
+ synchronized(go) {
+ try {
+ go.wait();
+ } catch (Exception e) {
+ throw new RuntimeException("Unexpected exception" + e);
+ }
+ }
+ }
+
+ static private void finishDump() {
+ synchronized(go) {
+ try {
+ go.notifyAll();
+ } catch (Exception e) {
+ throw new RuntimeException("Unexpected exception" + e);
+ }
+ }
+ }
+
+ public static void dumpStacks(int depth) throws Exception {
+ // Get stack trace of another thread
+ StackTraceElement[] stack = one.getStackTrace();
+ checkStack(one, stack, depth);
+
+ // Get stack traces of all Threads
+ Map m = Thread.getAllStackTraces();
+ Set s = m.entrySet();
+ Iterator iter = s.iterator();
+
+ Map.Entry entry;
+ while (iter.hasNext()) {
+ entry = (Map.Entry) iter.next();
+ Thread t = (Thread) entry.getKey();
+ stack = (StackTraceElement[]) entry.getValue();
+ if (t == null || stack == null) {
+ throw new RuntimeException("Null thread or stacktrace returned");
+ }
+ if (t == one) {
+ checkStack(t, stack, depth);
+ }
+ }
+ }
+
+ private static void checkStack(Thread t, StackTraceElement[] stack,
+ int depth) throws Exception {
+ if (trace) {
+ printStack(t, stack);
+ }
+ int frame = stack.length - 1;
+ for (int i = 0; i < depth; i++) {
+ if (! stack[frame].getMethodName().equals(methodNames[i])) {
+ throw new RuntimeException("Expected " + methodNames[i] +
+ " in frame " + frame + " but got " +
+ stack[frame].getMethodName());
+ }
+ frame--;
+ }
+ }
+
+ private static void printStack(Thread t, StackTraceElement[] stack) {
+ System.out.println(t +
+ " stack: (length = " + stack.length + ")");
+ if (t != null) {
+ for (int j = 0; j < stack.length; j++) {
+ System.out.println(stack[j]);
+ }
+ System.out.println();
+ }
+ }
+}
diff --git a/test/java/lang/Thread/StartOOMTest.java b/test/java/lang/Thread/StartOOMTest.java
new file mode 100644
index 0000000..dd8b519
--- /dev/null
+++ b/test/java/lang/Thread/StartOOMTest.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6379235
+ * @run main/othervm -server -Xmx32m -Xms32m -Xss256m StartOOMTest
+ * @summary ThreadGroup accounting mistake possible with failure of Thread.start()
+ */
+
+import java.util.*;
+
+public class StartOOMTest
+{
+ public static void main(String[] args) throws Throwable {
+ Runnable r = new SleepRunnable();
+ ThreadGroup tg = new ThreadGroup("buggy");
+ List<Thread> threads = new ArrayList<Thread>();
+ Thread failedThread;
+ int i = 0;
+ for (i = 0; ; i++) {
+ Thread t = new Thread(tg, r);
+ try {
+ t.start();
+ threads.add(t);
+ } catch (Throwable x) {
+ failedThread = t;
+ System.out.println(x);
+ System.out.println(i);
+ break;
+ }
+ }
+
+ int j = 0;
+ for (Thread t : threads)
+ t.interrupt();
+
+ while (tg.activeCount() > i/2)
+ Thread.yield();
+ failedThread.start();
+ failedThread.interrupt();
+
+ for (Thread t : threads)
+ t.join();
+ failedThread.join();
+
+ try {
+ Thread.sleep(1000);
+ } catch (Throwable ignore) {
+ }
+
+ int activeCount = tg.activeCount();
+ System.out.println("activeCount = " + activeCount);
+
+ if (activeCount > 0) {
+ throw new RuntimeException("Failed: there should be no active Threads in the group");
+ }
+ }
+
+ static class SleepRunnable implements Runnable
+ {
+ public void run() {
+ try {
+ Thread.sleep(60*1000);
+ } catch (Throwable t) {
+ }
+ }
+ }
+}
diff --git a/test/java/lang/Thread/StopBeforeStart.java b/test/java/lang/Thread/StopBeforeStart.java
new file mode 100644
index 0000000..a0ae20a
--- /dev/null
+++ b/test/java/lang/Thread/StopBeforeStart.java
@@ -0,0 +1,132 @@
+/*
+ * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4519200
+ * @summary Confirm a Thread.stop before start complies with the spec
+ * @author Pete Soper
+ *
+ * Confirm that a thread that had its stop method invoked before start
+ * does properly terminate with expected exception behavior. NOTE that
+ * arbitrary application threads could return from their run methods faster
+ * than the VM can throw an async exception.
+ */
+public class StopBeforeStart {
+
+ private static final int JOIN_TIMEOUT=10000;
+
+ private class MyThrowable extends Throwable {
+ }
+
+ private class Catcher implements Thread.UncaughtExceptionHandler {
+ private boolean nullaryStop;
+ private Throwable theThrowable;
+ private Throwable expectedThrowable;
+ private boolean exceptionThrown;
+
+ Catcher(boolean nullaryStop) {
+ this.nullaryStop = nullaryStop;
+ if (!nullaryStop) {
+ expectedThrowable = new MyThrowable();
+ }
+ }
+
+ public void uncaughtException(Thread t, Throwable th) {
+ exceptionThrown = true;
+ theThrowable = th;
+ }
+
+ void check(String label) throws Throwable {
+ if (!exceptionThrown) {
+ throw new RuntimeException(label +
+ " test:" + " missing uncaught exception");
+ }
+
+ if (nullaryStop) {
+ if (! (theThrowable instanceof ThreadDeath)) {
+ throw new RuntimeException(label +
+ " test:" + " expected ThreadDeath in uncaught handler");
+ }
+ } else if (theThrowable != expectedThrowable) {
+ throw new RuntimeException(label +
+ " test:" + " wrong Throwable in uncaught handler");
+ }
+ }
+ }
+
+ private class MyRunnable implements Runnable {
+ public void run() {
+ while(true)
+ ;
+ }
+ }
+
+ private class MyThread extends Thread {
+ public void run() {
+ while(true)
+ ;
+ }
+ }
+
+
+ public static void main(String args[]) throws Throwable {
+ (new StopBeforeStart()).doit();
+ System.out.println("Test passed");
+ }
+
+ private void doit() throws Throwable {
+
+ runit(false, new Thread(new MyRunnable()),"Thread");
+ runit(true, new Thread(new MyRunnable()),"Thread");
+ runit(false, new MyThread(),"Runnable");
+ runit(true, new MyThread(),"Runnable");
+ }
+
+ private void runit(boolean nullaryStop, Thread thread,
+ String type) throws Throwable {
+
+ Catcher c = new Catcher(nullaryStop);
+ thread.setUncaughtExceptionHandler(c);
+
+ if (nullaryStop) {
+ thread.stop();
+ } else {
+ thread.stop(c.expectedThrowable);
+ }
+
+ thread.start();
+ thread.join(JOIN_TIMEOUT);
+
+ if (thread.getState() != Thread.State.TERMINATED) {
+
+ thread.stop();
+
+ // Under high load this could be a false positive
+ throw new RuntimeException(type +
+ " test:" + " app thread did not terminate");
+ }
+
+ c.check(type);
+ }
+}
diff --git a/test/java/lang/Thread/ThreadStateTest.java b/test/java/lang/Thread/ThreadStateTest.java
new file mode 100644
index 0000000..c658e0c
--- /dev/null
+++ b/test/java/lang/Thread/ThreadStateTest.java
@@ -0,0 +1,386 @@
+/*
+ * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 5014783
+ * @summary Basic unit test of thread states returned by
+ * Thread.getState().
+ *
+ * @author Mandy Chung
+ *
+ * @build ThreadStateTest
+ * @run main ThreadStateTest
+ */
+
+import java.util.concurrent.locks.LockSupport;
+import java.util.concurrent.Semaphore;
+
+public class ThreadStateTest {
+ private static boolean testFailed = false;
+
+ static class Lock {
+ private String name;
+ Lock(String name) {
+ this.name = name;
+ }
+ public String toString() {
+ return name;
+ }
+ }
+ private static Lock globalLock = new Lock("my lock");
+
+ public static void main(String[] argv) {
+ // Call Thread.getState to force all initialization done
+ // before test verification begins.
+ Thread.currentThread().getState();
+ MyThread myThread = new MyThread("MyThread");
+
+ // before myThread starts
+ checkThreadState(myThread, Thread.State.NEW);
+
+ myThread.start();
+ myThread.waitUntilStarted();
+ checkThreadState(myThread, Thread.State.RUNNABLE);
+
+ synchronized (globalLock) {
+ myThread.goBlocked();
+ checkThreadState(myThread, Thread.State.BLOCKED);
+ }
+
+ myThread.goWaiting();
+ checkThreadState(myThread, Thread.State.WAITING);
+
+ myThread.goTimedWaiting();
+ checkThreadState(myThread, Thread.State.TIMED_WAITING);
+
+
+ /*
+ *********** park and parkUntil seems not working
+ * ignore this case for now.
+ * Bug ID 5062095
+ ***********************************************
+
+ myThread.goParked();
+ checkThreadState(myThread, Thread.State.WAITING);
+
+ myThread.goTimedParked();
+ checkThreadState(myThread, Thread.State.TIMED_WAITING);
+ */
+
+
+ myThread.goSleeping();
+ checkThreadState(myThread, Thread.State.TIMED_WAITING);
+
+ myThread.terminate();
+ checkThreadState(myThread, Thread.State.TERMINATED);
+
+ try {
+ myThread.join();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ System.out.println("Unexpected exception.");
+ testFailed = true;
+ }
+ if (testFailed)
+ throw new RuntimeException("TEST FAILED.");
+ System.out.println("Test passed.");
+ }
+
+ private static void checkThreadState(Thread t, Thread.State expected) {
+ Thread.State state = t.getState();
+ System.out.println("Checking thread state " + state);
+ if (state == null) {
+ throw new RuntimeException(t.getName() + " expected to have " +
+ expected + " but got null.");
+ }
+
+ if (state != expected) {
+ throw new RuntimeException(t.getName() + " expected to have " +
+ expected + " but got " + state);
+ }
+ }
+
+ private static String getLockName(Object lock) {
+ if (lock == null) return null;
+
+ return lock.getClass().getName() + '@' +
+ Integer.toHexString(System.identityHashCode(lock));
+ }
+
+ private static void goSleep(long ms) {
+ try {
+ Thread.sleep(ms);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ System.out.println("Unexpected exception.");
+ testFailed = true;
+ }
+ }
+
+ static class MyThread extends Thread {
+ private ThreadExecutionSynchronizer thrsync = new ThreadExecutionSynchronizer();
+
+ MyThread(String name) {
+ super(name);
+ }
+
+ private final int RUNNABLE = 0;
+ private final int BLOCKED = 1;
+ private final int WAITING = 2;
+ private final int TIMED_WAITING = 3;
+ private final int PARKED = 4;
+ private final int TIMED_PARKED = 5;
+ private final int SLEEPING = 6;
+ private final int TERMINATE = 7;
+ private int state = RUNNABLE;
+
+ private boolean done = false;
+ public void run() {
+ // Signal main thread to continue.
+ thrsync.signal();
+ while (!done) {
+ switch (state) {
+ case RUNNABLE: {
+ double sum = 0;
+ for (int i = 0; i < 1000; i++) {
+ double r = Math.random();
+ double x = Math.pow(3, r);
+ sum += x - r;
+ }
+ break;
+ }
+ case BLOCKED: {
+ // signal main thread.
+ thrsync.signal();
+ System.out.println(" myThread is going to block.");
+ synchronized (globalLock) {
+ // finish blocking
+ state = RUNNABLE;
+ }
+ break;
+ }
+ case WAITING: {
+ synchronized (globalLock) {
+ // signal main thread.
+ thrsync.signal();
+ System.out.println(" myThread is going to wait.");
+ try {
+ globalLock.wait();
+ } catch (InterruptedException e) {
+ // ignore
+ }
+ }
+ break;
+ }
+ case TIMED_WAITING: {
+ synchronized (globalLock) {
+ // signal main thread.
+ thrsync.signal();
+ System.out.println(" myThread is going to timed wait.");
+ try {
+ globalLock.wait(10000);
+ } catch (InterruptedException e) {
+ // ignore
+ }
+ }
+ break;
+ }
+ case PARKED: {
+ // signal main thread.
+ thrsync.signal();
+ System.out.println(" myThread is going to park.");
+ LockSupport.park();
+ // give a chance for the main thread to block
+ goSleep(10);
+ break;
+ }
+ case TIMED_PARKED: {
+ // signal main thread.
+ thrsync.signal();
+ System.out.println(" myThread is going to timed park.");
+ long deadline = System.currentTimeMillis() + 10000*1000;
+ LockSupport.parkUntil(deadline);
+
+ // give a chance for the main thread to block
+ goSleep(10);
+ break;
+ }
+ case SLEEPING: {
+ // signal main thread.
+ thrsync.signal();
+ System.out.println(" myThread is going to sleep.");
+ try {
+ Thread.sleep(1000000);
+ } catch (InterruptedException e) {
+ // finish sleeping
+ interrupted();
+ }
+ break;
+ }
+ case TERMINATE: {
+ done = true;
+ // signal main thread.
+ thrsync.signal();
+ break;
+ }
+ default:
+ break;
+ }
+ }
+ }
+ public void waitUntilStarted() {
+ // wait for MyThread.
+ thrsync.waitForSignal();
+ goSleep(10);
+ }
+
+ public void goBlocked() {
+ System.out.println("Waiting myThread to go blocked.");
+ setState(BLOCKED);
+ // wait for MyThread to get blocked
+ thrsync.waitForSignal();
+ goSleep(20);
+ }
+
+ public void goWaiting() {
+ System.out.println("Waiting myThread to go waiting.");
+ setState(WAITING);
+ // wait for MyThread to wait on object.
+ thrsync.waitForSignal();
+ goSleep(20);
+ }
+ public void goTimedWaiting() {
+ System.out.println("Waiting myThread to go timed waiting.");
+ setState(TIMED_WAITING);
+ // wait for MyThread timed wait call.
+ thrsync.waitForSignal();
+ goSleep(20);
+ }
+ public void goParked() {
+ System.out.println("Waiting myThread to go parked.");
+ setState(PARKED);
+ // wait for MyThread state change to PARKED.
+ thrsync.waitForSignal();
+ goSleep(20);
+ }
+ public void goTimedParked() {
+ System.out.println("Waiting myThread to go timed parked.");
+ setState(TIMED_PARKED);
+ // wait for MyThread.
+ thrsync.waitForSignal();
+ goSleep(20);
+ }
+
+ public void goSleeping() {
+ System.out.println("Waiting myThread to go sleeping.");
+ setState(SLEEPING);
+ // wait for MyThread.
+ thrsync.waitForSignal();
+ goSleep(20);
+ }
+ public void terminate() {
+ System.out.println("Waiting myThread to terminate.");
+ setState(TERMINATE);
+ // wait for MyThread.
+ thrsync.waitForSignal();
+ goSleep(20);
+ }
+
+ private void setState(int newState) {
+ switch (state) {
+ case BLOCKED:
+ while (state == BLOCKED) {
+ goSleep(20);
+ }
+ state = newState;
+ break;
+ case WAITING:
+ case TIMED_WAITING:
+ state = newState;
+ synchronized (globalLock) {
+ globalLock.notify();
+ }
+ break;
+ case PARKED:
+ case TIMED_PARKED:
+ state = newState;
+ LockSupport.unpark(this);
+ break;
+ case SLEEPING:
+ state = newState;
+ this.interrupt();
+ break;
+ default:
+ state = newState;
+ break;
+ }
+ }
+ }
+
+
+
+ static class ThreadExecutionSynchronizer {
+
+ private boolean waiting;
+ private Semaphore semaphore;
+
+ public ThreadExecutionSynchronizer() {
+ semaphore = new Semaphore(1);
+ waiting = false;
+ }
+
+ // Synchronizes two threads execution points.
+ // Basically any thread could get scheduled to run and
+ // it is not possible to know which thread reaches expected
+ // execution point. So whichever thread reaches a execution
+ // point first wait for the second thread. When the second thread
+ // reaches the expected execution point will wake up
+ // the thread which is waiting here.
+ void stopOrGo() {
+ semaphore.acquireUninterruptibly(); // Thread can get blocked.
+ if (!waiting) {
+ waiting = true;
+ // Wait for second thread to enter this method.
+ while(!semaphore.hasQueuedThreads()) {
+ try {
+ Thread.sleep(20);
+ } catch (InterruptedException xx) {}
+ }
+ semaphore.release();
+ } else {
+ waiting = false;
+ semaphore.release();
+ }
+ }
+
+ // Wrapper function just for code readability.
+ void waitForSignal() {
+ stopOrGo();
+ }
+
+ void signal() {
+ stopOrGo();
+ }
+ }
+}
diff --git a/test/java/lang/Thread/UncaughtExceptions.sh b/test/java/lang/Thread/UncaughtExceptions.sh
new file mode 100644
index 0000000..43b56c8
--- /dev/null
+++ b/test/java/lang/Thread/UncaughtExceptions.sh
@@ -0,0 +1,205 @@
+#!/bin/sh
+
+#
+# Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
+# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+#
+# This code is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License version 2 only, as
+# published by the Free Software Foundation.
+#
+# This code is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+# version 2 for more details (a copy is included in the LICENSE file that
+# accompanied this code).
+#
+# You should have received a copy of the GNU General Public License version
+# 2 along with this work; if not, write to the Free Software Foundation,
+# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+# CA 95054 USA or visit www.sun.com if you need additional information or
+# have any questions.
+#
+
+#
+# @test
+# @bug 4833089 4992454
+# @summary Check for proper handling of uncaught exceptions
+# @author Martin Buchholz
+#
+# @run shell UncaughtExceptions.sh
+
+# To run this test manually, simply do ./UncaughtExceptions.sh
+
+ java="${TESTJAVA+${TESTJAVA}/bin/}java"
+javac="${TESTJAVA+${TESTJAVA}/bin/}javac"
+
+failed=""
+Fail() { echo "FAIL: $1"; failed="${failed}."; }
+
+Die() { printf "%s\n" "$*"; exit 1; }
+
+Sys() {
+ "$@"; rc="$?";
+ test "$rc" -eq 0 || Die "Command \"$*\" failed with exitValue $rc";
+}
+
+HorizontalRule() {
+ echo "-----------------------------------------------------------------"
+}
+
+Bottom() {
+ test "$#" = 1 -a "$1" = "Line" || Die "Usage: Bottom Line"
+
+ HorizontalRule
+ if test -n "$failed"; then
+ count=`printf "%s" "$failed" | wc -c | tr -d ' '`
+ echo "FAIL: $count tests failed"
+ exit 1
+ else
+ echo "PASS: all tests gave expected results"
+ exit 0
+ fi
+}
+
+Cleanup() { Sys rm -f Seppuku* OK.class; }
+
+set -u
+
+checkOutput() {
+ name="$1" expected="$2" got="$3"
+ printf "$name:\n"; cat "$got"
+ if test -z "$expected"; then
+ test "`cat $got`" != "" && \
+ Fail "Unexpected $name: `cat $got`"
+ else
+ grep "$expected" "$got" >/dev/null || \
+ Fail "Expected \"$expected\", got `cat $got`"
+ fi
+}
+
+CheckCommandResults() {
+ expectedRC="$1" expectedOut="$2" expectedErr="$3"; shift 3
+ saveFailed="${failed}"
+ "$@" >TmpTest.Out 2>TmpTest.Err; rc="$?";
+ printf "==> %s (rc=%d)\n" "$*" "$rc"
+ checkOutput "stdout" "$expectedOut" "TmpTest.Out"
+ checkOutput "stderr" "$expectedErr" "TmpTest.Err"
+ test "${saveFailed}" = "${failed}" && \
+ echo "PASS: command completed as expected"
+ Sys rm -f TmpTest.Out TmpTest.Err
+}
+
+Run() {
+ expectedRC="$1" expectedOut="$2" expectedErr="$3" mainBody="$4"
+ cat > Seppuku.java <<EOJAVA
+import static java.lang.Thread.*;
+import static java.lang.System.*;
+
+class OK implements UncaughtExceptionHandler {
+ public void uncaughtException(Thread t, Throwable e) {
+ out.println("OK");
+ }
+}
+
+class NeverInvoked implements UncaughtExceptionHandler {
+ public void uncaughtException(Thread t, Throwable e) {
+ err.println("Test failure: This handler should never be invoked!");
+ }
+}
+
+public class Seppuku extends Thread implements Runnable {
+ public static void seppuku() { throw new RuntimeException("Seppuku!"); }
+
+ public void run() { seppuku(); }
+
+ public static void main(String[] args) throws Exception {
+ $mainBody
+ }
+}
+EOJAVA
+
+ Sys "$javac" "-source" "1.5" "Seppuku.java"
+ CheckCommandResults "$expectedRC" "$expectedOut" "$expectedErr" \
+ "$java" "Seppuku"
+ Cleanup
+}
+
+#----------------------------------------------------------------
+# A thread is never alive after you've join()ed it.
+#----------------------------------------------------------------
+Run 0 "OK" "Exception in thread \"Thread-0\".*Seppuku" "
+ Thread t = new Seppuku();
+ t.start(); t.join();
+ if (! t.isAlive())
+ out.println(\"OK\");"
+
+#----------------------------------------------------------------
+# Even the main thread is mortal - here it terminates "abruptly"
+#----------------------------------------------------------------
+Run 1 "OK" "Exception in thread \"main\".*Seppuku" "
+ final Thread mainThread = currentThread();
+ new Thread() { public void run() {
+ try { mainThread.join(); }
+ catch (InterruptedException e) {}
+ if (! mainThread.isAlive())
+ out.println(\"OK\");
+ }}.start();
+ seppuku();"
+
+#----------------------------------------------------------------
+# Even the main thread is mortal - here it terminates normally.
+#----------------------------------------------------------------
+Run 0 "OK" "" "
+ final Thread mainThread = currentThread();
+ new Thread() { public void run() {
+ try { mainThread.join(); }
+ catch (InterruptedException e) {}
+ if (! mainThread.isAlive())
+ out.println(\"OK\");
+ }}.start();"
+
+#----------------------------------------------------------------
+# Check uncaught exception handler mechanism on the main thread.
+# Check that thread-level handler overrides global default handler.
+#----------------------------------------------------------------
+Run 1 "OK" "" "
+ currentThread().setUncaughtExceptionHandler(new OK());
+ setDefaultUncaughtExceptionHandler(new NeverInvoked());
+ seppuku();"
+
+Run 1 "OK" "" "
+ setDefaultUncaughtExceptionHandler(new OK());
+ seppuku();"
+
+#----------------------------------------------------------------
+# Check uncaught exception handler mechanism on non-main threads.
+#----------------------------------------------------------------
+Run 0 "OK" "" "
+ Thread t = new Seppuku();
+ t.setUncaughtExceptionHandler(new OK());
+ t.start();"
+
+Run 0 "OK" "" "
+ setDefaultUncaughtExceptionHandler(new OK());
+ new Seppuku().start();"
+
+#----------------------------------------------------------------
+# Test ThreadGroup based uncaught exception handler mechanism.
+# Since the handler for the main thread group cannot be changed,
+# there are no tests for the main thread here.
+#----------------------------------------------------------------
+Run 0 "OK" "" "
+ setDefaultUncaughtExceptionHandler(new NeverInvoked());
+ new Thread(
+ new ThreadGroup(\"OK\") {
+ public void uncaughtException(Thread t, Throwable e) {
+ out.println(\"OK\");}},
+ new Seppuku()
+ ).start();"
+
+Cleanup
+
+Bottom Line