/* * "hello world" program, version 1: * * creates threads using anonymous inner class implementing Runnable. * * command-line argument specifies number of threads. */ package csci3366.sample.hello; import csci3366.sample.utility.Utility; public class Hello1 { public static void main(String[] args) { /* get number of threads from command line */ int numThreads = Utility.getIntegerArg(args, 0, 1, "numThreads", "arguments: numThreads"); /* create threads */ Thread[] threads = new Thread[numThreads]; for (int i = 0; i < threads.length; ++i) { threads[i] = new Thread(new Runnable() { public void run() { System.out.println("hello, world, from thread " + Thread.currentThread().getName()); } }); } /* start them up */ System.out.println("starting threads"); for (int i = 0; i < threads.length; ++i) { threads[i].start(); } /* wait for them to finish */ for (int i = 0; i < threads.length; ++i) { try { threads[i].join(); } catch (InterruptedException e) { System.err.println("should not happen"); } } System.out.println("threads all done"); } }