// RunnableTest.java class Echo implements Runnable { int i = 0; public void run() { while (i++ < 3) { System.out.println("hello " + i); try { Thread.sleep(1000); } catch (InterruptedException e) { } } } } public class RunnableTest { public static void main(String argv[]){ Echo e1 = new Echo(); Thread th1 = new Thread(e1); Echo e2 = new Echo(); Thread th2 = new Thread(e2); System.out.println("Echo test"); th1.start(); th2.start(); System.out.println("end start"); } }
% javac RunnableTest.java % java RunnableTest Echo test hello 1 end start hello 1 hello 2 hello 2 hello 3 hello 3