package test; public class Test { private void declareTest(String s) { try { Thread.sleep(1000); System.out.println("\nTesting " + s); Thread.sleep(5000); } catch(InterruptedException e) {} } public static void main(String args[]) throws Exception { System.out.println("Starting tests"); final Test t = new Test(); t.runStackGrowthTest(); t.runTypeCheckTest(); t.runSwitchTest(); t.runThrowTest(); t.runMultiArrayTest(); t.runThreadTest(); t.runThreadTest2(); t.runThreadTest3(); System.out.println("Tests complete"); } /* * Stack growth */ public void runStackGrowthTest() { declareTest("java stack growth"); useUpStack(0); useUpStack(0); } private int limit = 200; private void useUpStack(int i) { System.out.println("frame is number " + i); if(i < limit) { useUpStack(i + 1); } } static class Bla3 extends Bla { public Bla3() {} public void x(){} } /* * type checking */ void runTypeCheckTest() throws Exception { declareTest("type checking"); System.out.println("1: " + (new Bla() instanceof Runnable)); System.out.println("2: " + !(this instanceof Runnable)); System.out.println("3: " + (this instanceof Test)); System.out.println("4: " + (this instanceof Object)); Object blax = new Bla3(); Bla blay = (Bla) blax.getClass().newInstance(); System.out.println("5: " + (blax instanceof Bla)); System.out.println("6: " + !(blax instanceof Bla2)); System.out.println("7: " + (blay instanceof Object)); System.out.println("8: " + (blay instanceof Bla)); System.out.println("9: " + (blay instanceof Runnable)); System.out.println("10: " + !(blay instanceof I)); Object c = new Bla[3]; System.out.println("11: " + (c instanceof Object)); System.out.println("12: " + (c instanceof Runnable[])); System.out.println("13: " + (c instanceof Bla[])); System.out.println("14: " + !(c instanceof Bla)); } static interface I {} /* * Switch instructions */ void runSwitchTest() { declareTest("java switch instructions tableswitch and lookupswitch"); int i; int switches[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; for(i=0; i= 2) { System.out.println("thread " + Thread.currentThread() + " is notifying all"); notifyAll(); break; } else { wait(1); } } } } catch(InterruptedException e) {} System.out.println("thread " + Thread.currentThread() + " jumping out"); } } } /* * thread synchronization */ boolean thread1Running = true; boolean thread2Running = true; void runThreadTest2() { declareTest("thread synchronization and notification"); final Object synchronizer = new Object(); final Object synchronizer2 = new Object(); /* Thread t1 and thread t2 will alternate the monitor entry * for a while until thread t1 is done and thread t2 can finish * without any more delays */ Thread t1 = new Thread() { int counter = 0; public void run() { while(counter++ < 3) { synchronized(synchronizer) { for(int k = 0; k<4; k++) { System.out.println("in thread 1"); } } Thread.yield(); } synchronized(synchronizer2) { thread1Running = false; synchronizer2.notify(); } } }; Thread t2 = new Thread() { int counter = 0; public void run() { while(counter++ < 5) { synchronized(synchronizer) { for(int k = 0; k<3; k++) { System.out.println("in thread 2"); } } Thread.yield(); } synchronized(synchronizer2) { thread2Running = false; synchronizer2.notify(); } } }; t1.start(); t2.start(); /* the main thread will not continue until the two created threads have expired */ while(thread1Running || thread2Running) { synchronized(synchronizer2) { if(thread1Running || thread2Running) { try { synchronizer2.wait(); } catch(InterruptedException e) {} } else { break; } } } System.out.println("done"); } /* * thread timed wait, synchronization and notification */ void runThreadTest3() { declareTest("thread timed wait, synchronization and notification"); Thread t1, t2; t1 = new Thread(new Bla2(1)); t2 = new Thread(new Bla2(2)); System.out.println("revving up the threads"); t1.start(); t2.start(); new Bla2(3).run(); while(deathCounter < 3) { doNothing(); } System.out.println("done"); } public void doNothing() {} static int deathCounter = 0; static Object jointSynchronizer = new String(); static class Bla2 implements Runnable { int num; Bla2(int num) { this.num = num; } /* * The three threads will alternate monitor entry. The timed wait will not * actually timeout until only a single thread is left running. */ public void run() { int counter = 0; while(counter++ < 11) { synchronized(jointSynchronizer) { for(int i=0; i<3; i++) System.out.println("Running " + num + " a "); jointSynchronizer.notify(); for(int i=0; i<3; i++) System.out.println("Running " + num + " b "); try { System.out.println(num + " about to wait a bit"); jointSynchronizer.wait(7000); } catch(InterruptedException e) {} for(int i=0; i<3; i++) System.out.println("Running " + num + " c "); } } deathCounter++; System.out.println(num + " is done"); } } }