/* Java class that implements the Runnable interface, making ** its instances suitable for becoming "resident runnables" ** of threads. This class generalizes the PrintGrisGrop class ** by giving the client program the ability to choose which string ** is to be printed and how many times (by its run() method). ** (This data is supplied by the client to this class's constructor.) */ public class StringPrinter implements Runnable { // instance variables // ------------------ private String str; // the string to be printed private int n; // # of times to print it /* Establish the string to be printed by the run() method, ** and how many times it is to be printed. ** pre: numTimes >= 0 */ public StringPrinter(String s, int numTimes) { this.str = s; this.n = numTimes; } /* Prints the string established at construction the ** number of times established at construction. */ public void run() { // The loop iterates 1000*n times and prints // str only every thousandth iteration. This // is for the purpose of making it more likely // that this output intersperses with that of // its caller. for (int i=0; i != 1000*n; i++) { if (i % 1000 == 0) { System.out.println(str); } } } }