class the MyThread the extends the Thread { Private int Time; public the MyThread (String name, int Time) { Super (name); // Set the name of the thread the this .time = Time; // Set Sleep Time } public void RUN () { the try { the Thread .sleep ( the this .time); // sleep specified time } the catch (InterruptedException E) { e.printStackTrace (); } System.out.println (Thread.currentThread () getName (). + "thread, sleep" + the this .time + "msec." ); } }; Public class ExecDemo01 { public static void main (String args []) { MTl the MyThread = new new the MyThread ( "thread A", 10000); // define the thread object, specified sleep time the MyThread MT2 = new new the MyThread ( "thread B", 20000); // define the thread object, specified sleep time the MyThread MT3 = new new MyThread ( "thread C", 30000); // define the thread object, designated sleep time mt1.start (); //Start a thread mt2.start (); //Start a thread mt3.start (); // start the thread } };
class the MyThread the implements the Runnable { Private String name; Private int Time; public the MyThread (String name, int Time) { the this .name = name; // Set the name of the thread the this .time = Time; // Set Sleep Time } public void RUN ( ) { the try { the Thread.sleep ( the this .time); // sleep specified time } the catch (InterruptedException E) { e.printStackTrace (); } System.out.println ( the this .name + "thread, sleep" + the this .time + "msec." ); } }; Public class ExecDemo02 { public static void main (String args []) { the MyThread MTl = new new the MyThread ( "thread A", 10000); // define the thread object, specified sleep time the MyThread MT2 = new new the MyThread ( "thread B", 20000); // define the thread object, specified sleep time the MyThread MT3 = new new the MyThread ( "thread C" 30000); // define the thread object, designated sleep time new new the thread (MT1) .start (); //Start a thread new new the Thread (MT2) .start (); // start the thread new new the Thread (MT3) .start (); // start the thread } };