// // simple example of threads in Java, showing whether your system // performs timeslicing: if you see "ping" and "pong" messages // interleaved, your system does timeslicing; if not, it doesn't. // // see main method below for command-line arguments. // // // a SimplerThread object repeatedly prints message msg. // public class SimplerThread extends Thread { String msg ; int cycles ; SimplerThread(String m, int c) { msg = m ; cycles = c ; } // overrides run() in Thread class to define object's // behavior. public void run() { for (int i = 0 ; i < cycles ; i++) { System.out.println(msg) ; } } // command-line arguments are integers C. // builds and starts two threads of type SimplerThread. // continues for C cycles. public static void main(String[] args) { if (args.length < 1) { System.out.println("Arguments are:") ; System.out.println(" cycles") ; System.exit(-1) ; } int C = Integer.parseInt(args[0]) ; SimplerThread t1 = new SimplerThread("ping---->", C) ; SimplerThread t2 = new SimplerThread("<----pong", C) ; t1.start() ; t2.start() ; } }