corda/sgx-jvm/avian/test/Threads.java
Chris Rankin c0c75c3e19 Squashed commit of the following: (#32)
commit a25c09bb738e7e82d2dfd909381ba052f7b69687
Merge: aa6664214 92af5d169
Author: Joel Dice <joel.dice@gmail.com>
Date:   Tue Sep 5 11:38:08 2017 -0600

    Merge pull request #545 from corda/chrisr3-uncaught-exceptions

    Support Thread.uncaughtExceptionHandler with OpenJDK

commit aa666421499d0e9ed46b6c745a93abd9998a58b0
Merge: dc8c99bd2 1cb11e964
Author: Joel Dice <joel.dice@gmail.com>
Date:   Tue Sep 5 11:23:41 2017 -0600

    Merge pull request #546 from corda/chrisr3-werror

    Fix "fallthrough" warnings with recent GCC.

commit dc8c99bd2f045c787f2c322cd65f866626fa9461
Author: Joel Dice <joel.dice@gmail.com>
Date:   Tue Sep 5 11:17:54 2017 -0600

    fix bootimage-test build regression

commit 1cb11e964fab925e9d98373ba58e181b1a1f11fd
Author: Chris Rankin <chris.rankin@r3.com>
Date:   Tue Sep 5 10:25:48 2017 +0100

    Fix "fallthrough" warnings with recent GCC.

commit 92af5d169dbfb7b9b9f5b667b0dc71ca7b98b416
Author: Chris Rankin <chris.rankin@r3.com>
Date:   Mon Sep 4 17:26:31 2017 +0100

    Ensure that the thread resources are only cleaned up once.

commit 05d260f8bed68e3f999619aee338a781ba0f4c63
Author: Chris Rankin <chris.rankin@r3.com>
Date:   Mon Sep 4 17:10:04 2017 +0100

    Test exception thrown from uncaught-exception handler.

commit b1c5dca36163a876fba86221493883ddbd5fe805
Author: Chris Rankin <chris.rankin@r3.com>
Date:   Tue Aug 29 17:17:24 2017 +0100

    Support Thread.uncaughtExceptionHandler.
2017-09-06 10:48:26 +01:00

95 lines
2.3 KiB
Java

public class Threads implements Runnable {
private static boolean success = false;
private static void expect(boolean v) {
if (! v) throw new RuntimeException();
}
public static void main(String[] args) throws Exception {
Thread.currentThread().getThreadGroup()
.uncaughtException(Thread.currentThread(), new Exception());
{ Threads test = new Threads();
Thread thread = new Thread(test);
synchronized (test) {
thread.start();
test.wait();
}
}
{ Thread thread = new Thread() {
public void run() {
while (true) {
System.out.print(".");
try {
sleep(1000);
} catch (Exception e) {
System.out.println("thread interrupted? " + interrupted());
break;
}
}
}
};
thread.start();
System.out.println("\nAbout to interrupt...");
thread.interrupt();
System.out.println("\nInterrupted!");
}
{ Thread thread = new Thread() {
@Override
public void run() {
// do nothing
}
};
thread.start();
thread.join();
}
System.out.println("finished; success? " + success);
if (! success) {
System.exit(-1);
}
}
public void run() {
int i = 0;
try {
expect(! Thread.holdsLock(this));
synchronized (this) {
expect(Thread.holdsLock(this));
System.out.println("I'm running in a separate thread!");
Thread.yield(); // just to prove Thread.yield exists and is callable
final int arrayCount = 16;
final int arraySize = 4;
System.out.println("Allocating and discarding " + arrayCount +
" arrays of " + arraySize + "MB each");
for (; i < arrayCount; ++i) {
byte[] array = new byte[arraySize * 1024 * 1024];
}
long nap = 500;
System.out.println("sleeping for " + nap + " milliseconds");
Thread.sleep(nap);
notifyAll();
}
success = true;
} catch (Throwable e) {
System.err.println("caught something in second thread after " + i +
" iterations");
e.printStackTrace();
} finally {
synchronized (this) {
notifyAll();
}
}
}
}