public class CountDown {
// A Java code that creates 5 parallel tasks
// performing countdown from 5 to 0
public static void main(String[] args) {
CountDown application = new CountDown();
application.start();
}
// Method for spawning 5 parallel threads of execution
public void start() {
for(int i=1; i<=5; i++)
new Thread(new CountDownHandler(5, i)).start();
}
// Utility class for handling count down per thread
// that implements the Runnable interface
private class CountDownHandler implements Runnable {
private int id;
private int counter;
public CountDownHandler(int counter, int id) {
this.counter = counter;
this.id = id;
}
public String status() {
return "id#" + id + " " +
((counter >= 0) ? counter : "LiftOff!");
}
public void run() {
while(counter >= -1) {
System.out.println(status());
counter--;
try {
Thread.sleep(1000);
} catch(Exception e) {
}
}
}
}
}