Threads of Control

   	
Index Previous Next

An Example: Two threads

The following program is a simple Java application that creates and starts two independent threads:

class TwoThreadsTest {

    public static void main (String[] args) {

        new SimpleThread("Jamaica").start();

        new SimpleThread("Fiji").start();

    }

}

class SimpleThread extends Thread {

    public SimpleThread(String str) {

        super(str);

    }

    public void run() {

        for (int i = 0; i < 10; i++) {

            System.out.println(i + " " + getName());

            try {

                sleep((int)(Math.random() * 1000));

            } catch (InterruptedException e) {}

        }

        System.out.println("DONE! " + getName());

    }

}

More About threads


Index Previous Next