加入线程

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();
    }
}

Java

执行上面示例代码,得到以下结果:

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

Shell

正如在上面的示例中所看到的,当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();
    }
}

Java

执行上面示代码,得到以下结果:

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

Shell

在上面的例子中,当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());
    }
}

Java

执行上面示例代码,得到以下结果:

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

Shell

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();
    }
}

Java

执行上面示例代码,得到以下结果:

Thread-1
Thread-0

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智慧浩海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值