- public class Ch124
- {
- private MyThread1 th1;
- private MyThread2 th2;
- Ch124()
- {
- th1=new MyThread1();
- th1.start();
- th2=new MyThread2();
- th2.start();
- }
- public static void main(String args[])
- {
- new Ch124();
- }
- }
- class MyThread1 extends Thread
- {
- public void run(){
- for(int i=5;i>=1;i--)
- {
- System.out.println(i+"秒 "+Thread.currentThread().getName());
- try
- {
- sleep(1000);
- }catch(InterruptedException e){}
-
- }
- System.out.println("時間到");
- System.out.println("執行緒名稱"+Thread.currentThread().getName());
- }
- }
- class MyThread2 extends Thread
- {
- public void run(){
- for(int i=5;i>=1;i--)
- {
- System.out.println(i+"秒 "+Thread.currentThread().getName());
- try
- {
- sleep(1000);
- }catch(InterruptedException e){}
-
- }
- System.out.println("時間到");
- System.out.println("執行緒名稱"+Thread.currentThread().getName());
- }
- }
複製代碼 |