Exemple d'interface exécutable

public class CreateThreadRunnableExample implements Runnable {

    public void run() {

        for (int i = 0; i < 5; i++) {
            System.out.println("Child Thread : " + i);

            try {
                Thread.sleep(50);
            } catch (InterruptedException ie) {
                System.out.println("Child thread interrupted! " + ie);
            }
        }

        System.out.println("Child thread finished!");
    }

    public static void main(String[] args) {

        Thread t = new Thread(new CreateThreadRunnableExample(), "My Thread");

        t.start();

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

            System.out.println("Main thread : " + i);

            try {
                Thread.sleep(100);
            } catch (InterruptedException ie) {
                System.out.println("Child thread interrupted! " + ie);
            }
        }
        System.out.println("Main thread finished!");
    }
}

dans ce programme, deux de sommeil méthodes sont utilisées de deifferent temps..,,, donc si thread principal de l'exécution enfants thread avez à courir 2 fois.mais il s'exécute qu'une seule fois....elle nous prenons le concept de l'exécutable ou de l'état d'exécution....ensuite, lorsque le thread principal de la fin,alors 2 threads enfants seront dans l'état prêt, alors pourquoi un seul enfant thread s'exécute.

source d'informationauteur bimal chawla