2015-02-06 20:50:59 +00:00
|
|
|
public class Threads implements Runnable {
|
2014-07-12 22:03:11 +00:00
|
|
|
private static boolean success = false;
|
|
|
|
|
|
|
|
private static void expect(boolean v) {
|
|
|
|
if (! v) throw new RuntimeException();
|
|
|
|
}
|
|
|
|
|
2014-02-25 19:33:12 +00:00
|
|
|
public static void main(String[] args) throws Exception {
|
2017-09-06 09:48:26 +00:00
|
|
|
Thread.currentThread().getThreadGroup()
|
2015-02-06 20:50:59 +00:00
|
|
|
.uncaughtException(Thread.currentThread(), new Exception());
|
|
|
|
|
2011-07-12 20:15:43 +00:00
|
|
|
{ Threads test = new Threads();
|
|
|
|
Thread thread = new Thread(test);
|
2007-07-02 04:04:03 +00:00
|
|
|
|
2014-02-25 19:33:12 +00:00
|
|
|
synchronized (test) {
|
|
|
|
thread.start();
|
|
|
|
test.wait();
|
2007-07-02 04:04:03 +00:00
|
|
|
}
|
2011-07-12 20:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ 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!");
|
2007-07-02 04:04:03 +00:00
|
|
|
}
|
|
|
|
|
2014-02-25 19:33:12 +00:00
|
|
|
{ Thread thread = new Thread() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
};
|
2015-02-06 20:50:59 +00:00
|
|
|
|
2014-02-25 19:33:12 +00:00
|
|
|
thread.start();
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
|
2014-07-12 22:03:11 +00:00
|
|
|
System.out.println("finished; success? " + success);
|
|
|
|
|
|
|
|
if (! success) {
|
|
|
|
System.exit(-1);
|
|
|
|
}
|
2007-07-02 04:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void run() {
|
2014-07-12 22:03:11 +00:00
|
|
|
int i = 0;
|
|
|
|
try {
|
|
|
|
expect(! Thread.holdsLock(this));
|
|
|
|
synchronized (this) {
|
|
|
|
expect(Thread.holdsLock(this));
|
|
|
|
|
2007-07-11 01:38:06 +00:00
|
|
|
System.out.println("I'm running in a separate thread!");
|
2007-07-02 04:04:03 +00:00
|
|
|
|
2014-07-12 22:03:11 +00:00
|
|
|
Thread.yield(); // just to prove Thread.yield exists and is callable
|
|
|
|
|
2007-07-11 04:19:26 +00:00
|
|
|
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];
|
|
|
|
}
|
2007-07-02 04:04:03 +00:00
|
|
|
|
2014-07-12 22:03:11 +00:00
|
|
|
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) {
|
2007-07-02 04:04:03 +00:00
|
|
|
notifyAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|