import java.util.ArrayList; /** * This program creates three Grunt objects, starts them running in separate threads, * and then does the digital equivalent of twiddling its thumbs while it waits for them * to complete. (Note that the Grunt threads in this case never complete, so the * program runs forever.) * * @author richards */ public class RunGrunts { /** * The standard Java main method. Takes an array of command-line arguments, but * they're not used for anything in this example. * @param args Ignored by main. */ public static void main(String[] args) { ArrayList output = new ArrayList(); // Make some grunt objects. Pass constructor different ID and interval values. Grunt a = new Grunt(1, 10, output); Grunt b = new Grunt(2, 37, output); Grunt c = new Grunt(3, 137, output); // Each Grunt has the potential to execute "simultaneously" with main // and its Grunt siblings, but they don't actually do so until we // wrap a Thread object around them and start the Thread running. // In some cases you really want to keep a reference to each Thread // object, but after starting the threads I don't need to access them // again here so I just invoke start() on anonymous Thread objects. (new Thread(a)).start(); (new Thread(b)).start(); (new Thread(c)).start(); // Stay alive, doing nothing, while the threads do their thing. This is // a terrible way to wait, as it devours lots of CPU time, but is a simple // way to make the point... int size = output.size(); System.out.println("Initial list size is "+size); while(true) { if (output.size() != size) { // Work our way through any new additions and print them for(; size