加入线程

join()方法等待线程死亡。 换句话说,它会导致当前运行的线程停止执行,直到它加入的线程完成其任务。

语法:

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

join()方法的示例

package com.zyiz;

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.zyiz;

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.zyiz;

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.zyiz;

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

上一篇:线程直接调用run()方法而不是start()方法

下一篇:命名线程和当前线程

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程