- public class Main {
- public static void main(String[] args) {
- // TODO 自動產生的方法 Stub
- MyThread1 t = new MyThread1();
- MyThread2 t2 = new MyThread2();
- t.start();
- t2.start();
- }
- }
複製代碼- public class MyThread1 extends Thread{
- public void run()
- {
- for(int i=5;i>=1;i--)
- {
- System.out.println(i+"秒!"+Thread.currentThread().getName());
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- // TODO 自動產生的 catch 區塊
- e.printStackTrace();
- }
- }
- System.out.println("時間到!"+Thread.currentThread().getName());
- }
- }
複製代碼- public class MyThread2 extends Thread{
- public void run()
- {
- for(int i=1;i<=5;i++)
- {
- System.out.println(i+"秒!"+Thread.currentThread().getName());
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- // TODO 自動產生的 catch 區塊
- e.printStackTrace();
- }
- }
- System.out.println("時間到!"+Thread.currentThread().getName());
- }
- }
複製代碼 |