Creating Thread

public class PingPong extends Thread {
    private String word; // What word to print
    private int delay; // how long to pause
    public PingPong(String whatToSay, int delayTime) {
        word = whatToSay;
        delay = delayTime;
    }

    public void run() {
        try {
            for (int i=0;i<10;i++) {
                System.out.print("\n"+word + " ");
                sleep(delay); // wait until next time
            }
        } catch (InterruptedException e) {
            return; // end this thread;
        }
    }
    public static void main(String []args) {
        new PingPong("Ping", 33).start(); // 1/30 second
        new PingPong("PONG",100).start(); // 1/10 second
    }
}


No comments: