import java.util.ArrayList; /** * Classes that implement the Runnable interface must provide a no-argument run() method. * Think of run() as a sort of main method — it's where execution will start when * this object is "executed". Since we can't pass arguments to run(), any information * needed by an instance of this class needs to be passed as arguments to the constructor. * * @author richards */ public class Grunt implements Runnable { private int interval; private int id; private ArrayList output; /** * Each grunt gets "personalized" with these argument values. * @param id The id number of this specific Grunt object. * @param interval Used in the run() method to figure out when to print. */ public Grunt(int id, int interval, ArrayList output) { this.id = id; this.interval = interval; this.output = output; } /** * After an initial line of ouptut, the run() method here just loops forever, * printing periodically as it goes. */ public void run() { System.out.println("This is Grunt #"+id+" reporting for duty."); int val = interval; while(true) { val = val + interval; if (val == interval) { System.out.println("Grunt #"+id+" hit it again"); output.add("Grunt #"+id+" hit it again"); } } } }