1 public class ThreadPingPong extends Thread { 2 private String name; 3 private int total; 4 private int delay; 5 public ThreadPingPong( String n, int t, int d ) { 6 name = n; 7 total = t; 8 delay = d; 9 start(); 10 } 11 public void run() { 12 for (int i=0; i < total; i++) { 13 System.out.println( name + " - " + i * delay ); 14 try { Thread.sleep( delay ); } catch (Exception e) { return; } 15 } 16 } 17 public static void main( String[] args ) { 18 new ThreadPingPong( "Ping", 5, 300 ); 19 new ThreadPingPong( "Pong", 5, 500 ); 20 } 21 } 22 public class ThreadPingPong implements Runnable { 23 private String name; 24 private int total; 25 private int delay; 26 public ThreadPingPong( String n, int t, int d ) { 27 name = n; 28 total = t; 29 delay = d; 30 Thread th = new Thread( this ); 31 th.start(); 32 } 33 public void run() { 34 for (int i=0; i < total; i++) { 35 System.out.println( name + " - " + i * delay ); 36 try { Thread.sleep( delay ); } catch (Exception e) { return; } 37 } 38 } 39 public static void main( String[] args ) { 40 new ThreadPingPong( "Ping", 5, 300 ); 41 new ThreadPingPong( "Pong", 5, 500 ); 42 } 43 } 44 // Ping - 0 45 // Pong - 0 46 // Ping - 300 47 // Pong - 500 48 // Ping - 600 49 // Ping - 900 50 // Pong - 1000 51 // Ping - 1200 52 // Pong - 1500 53 // Pong - 2000