- public class Main {
- public static void main(String[] args) {
- // TODO 自動產生的方法 Stub
- test();
- test2();
-
- }
- public static void test()
- {
- Thread t = new 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());
- }
- };
- t.start();
- }
- public static void test2()
- {
- Thread t2 = new 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());
- }
- };
- t2.start();
- }
- }
複製代碼 |