public class Sibling extends Thread { // Constructor for Sibling Sibling(String name, int priority) { super(name); setPriority(priority); } public void run() { int count = 1000000; try { sleep(200); } catch (InterruptedException e) {}; // Countdown, with reports every 100,000 iterations while (count-- > 0) { if (count%100000 == 0) System.out.println(getName() + ": " + count); } } private static void usage() { System.err.println("Usage: java Sibling p1 p2" + "where p1 and p2 are greater than 0\n and" + "less than or equal to 10"); System.exit(1); } private static int checkArg(String arg) { int i = 0; try { i = Integer.parseInt(arg); } catch (NumberFormatException e) { usage();} if ( i < 1 || i > 10 ) usage(); return i; } public static void main(String args[]) { if (args.length != 2) usage(); int p1 = checkArg(args[0]); int p2 = checkArg(args[1]); // Args are okay, let's get grandparent ThreadGroup ThreadGroup next = Thread.currentThread().getThreadGroup(); ThreadGroup top = next; while (next != null) { top = next; next = next.getParent(); } // Now, create and start two Siblings Sibling sib1 = new Sibling("Older", p1); Sibling sib2 = new Sibling("Younger", p2); sib1.start(); sib2.start(); // List all ThreadGroups and Threads top.list(); // Wait for lower priority Sibling to complete Sibling lower = (p1>p2)?sib2:sib1; try { lower.join(); } catch (InterruptedException e) {}; System.out.println("Main Thread exiting."); } }