/** Try out a few instances of the Counter2 class.
Driver class that creates three threaded objects (Counter2) that count from 0 to 4. In this case,
the driver does not start the threads, as each thread is automatically started in Counter2's
constructor. Uses the following class:
Counter2Test.java
Counter2.java
**************************
public class Counter2Test {
public static void main(String[] args) {
Counter2 c1 = new Counter2(5);
Counter2 c2 = new Counter2(5);
Counter2 c3 = new Counter2(5);
}
}
* Counter2.java A Runnable that counts to a specified value and sleeps a random number of seconds
in between counts. Here, the thread is started automatically when the object is instantiated.
/** A Runnable that counts up to a specified
* limit with random pauses in between each count.
public class Counter2 implements Runnable {
private static int totalNum = 0;
private int currentNum, loopLimit;
public Counter2(int loopLimit) {
this.loopLimit = loopLimit;
currentNum = totalNum++;
Thread t = new Thread(this);
t.start();
}
private void pause(double seconds) {
try { Thread.sleep(Math.round(1000.0*seconds)); }
catch(InterruptedException ie) {}
}
public void run() {
for(int i=0; i