加入線程

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
0 條評論,你可以發表評論,我們會進行改進
Comment author placeholder