加入執行緒


join()方法等待執行緒死亡。 換句話說,它會導致當前執行的執行緒停止執行,直到它加入的執行緒完成其任務。

語法:

  • public void join()throws InterruptedException
  • public void join(long milliseconds)throws InterruptedException

join()方法的範例

package com.yiibai;

class TestJoinMethod1 extends Thread {
    public void run() {
        for (int i = 1; i <= 5; i++) {
            try {
                Thread.sleep(500);
            } catch (Exception e) {
                System.out.println(e);
            }
            System.out.println(i);
        }
    }

    public static void main(String args[]) {
        TestJoinMethod1 t1 = new TestJoinMethod1();
        TestJoinMethod1 t2 = new TestJoinMethod1();
        TestJoinMethod1 t3 = new TestJoinMethod1();
        t1.start();
        try {
            t1.join();
        } catch (Exception e) {
            System.out.println(e);
        }

        t2.start();
        t3.start();
    }
}

執行上面範例程式碼,得到以下結果:

1
2
3
4
5
1
1
2
2
3
3
4
4
5
5

正如在上面的範例中所看到的,當t1完成其任務時,t2t3才開始執行。

join(long miliseconds)方法的範例

package com.yiibai;

class TestJoinMethod2 extends Thread {
    public void run() {
        for (int i = 1; i <= 5; i++) {
            try {
                Thread.sleep(500);
            } catch (Exception e) {
                System.out.println(e);
            }
            System.out.println(i);
        }
    }

    public static void main(String args[]) {
        TestJoinMethod2 t1 = new TestJoinMethod2();
        TestJoinMethod2 t2 = new TestJoinMethod2();
        TestJoinMethod2 t3 = new TestJoinMethod2();
        t1.start();
        try {
            t1.join(1500);
        } catch (Exception e) {
            System.out.println(e);
        }

        t2.start();
        t3.start();
    }
}

執行上面示程式碼,得到以下結果:

1
2
3
1
1
4
2
2
5
3
3
4
4
5
5

在上面的例子中,當t1完成其任務1500毫秒(3次),然後t2t3開始執行。

getName(),setName(String) 和 getId() 方法範例:

package com.yiibai;

class TestJoinMethod3 extends Thread {
    public void run() {
        System.out.println("running...");
    }

    public static void main(String args[]) {
        TestJoinMethod3 t1 = new TestJoinMethod3();
        TestJoinMethod3 t2 = new TestJoinMethod3();
        System.out.println("Name of t1:" + t1.getName());
        System.out.println("Name of t2:" + t2.getName());
        System.out.println("id of t1:" + t1.getId());

        t1.start();
        t2.start();

        t1.setName("New ThreadName-1");
        System.out.println("After changing name of t1:" + t1.getName());
    }
}

執行上面範例程式碼,得到以下結果:

Name of t1:Thread-0
Name of t2:Thread-1
id of t1:10
After changing name of t1:New ThreadName-1
running...
running...

currentThread()方法:
currentThread()方法返回對當前正在執行的執行緒物件的參照。

語法:

  • public static Thread currentThread()

currentThread()方法的範例

package com.yiibai;

class TestJoinMethod4 extends Thread {
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }


    public static void main(String args[]) {
        TestJoinMethod4 t1 = new TestJoinMethod4();
        TestJoinMethod4 t2 = new TestJoinMethod4();

        t1.start();
        t2.start();
    }
}

執行上面範例程式碼,得到以下結果:

Thread-1
Thread-0