`

Join方法小结

 
阅读更多

 Join  方法的意思就是后面的线程要运行 ,只有调用join方法的这个线程is dead 或 超过指定的时间,才能执行;

public final void join() //只有当前线程isdead 后面的才能运行
 
public final synchronized void join(long millis) // 线程isdead or over  millis

 

public class ThreadJoinExample {
 
    public static void main(String[] args) {
        Thread t1 = new Thread(new MyRunnable(), "t1");
        Thread t2 = new Thread(new MyRunnable(), "t2");
        Thread t3 = new Thread(new MyRunnable(), "t3");
         
        t1.start();
         
        //start second thread after waiting for 2 seconds or if it's dead
        try {
            t1.join(2000);// 如果把这里改成t1.join()那么就只能等到t1dead 才执行t2
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
         
        t2.start();
         
        //start third thread only when first thread is dead
        try {
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
         
        t3.start();
         
        //let all threads finish execution before finishing main thread
        try {
            t1.join();
            t2.join();
            t3.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         
        System.out.println("All threads are dead, exiting main thread");
    }
 
}
 
class MyRunnable implements Runnable{
 
    @Override
    public void run() {
        System.out.println("Thread started:::"+Thread.currentThread().getName());
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Thread ended:::"+Thread.currentThread().getName());
    }
     
}

 output :

Thread started:::t1
Thread started:::t2
Thread ended:::t1
Thread started:::t3
Thread ended:::t2
Thread ended:::t3
All threads are dead, exiting main thread

 

  

 

分享到:
评论

相关推荐

    MySQL中Nested-Loop Join算法小结

    数据库中JOIN操作的实现主要有三种:嵌套循环连接(Nested Loop Join),归并连接(Merge Join)和散列连接或者哈稀连接(Hash Join)。其中嵌套循环连接又视情况又有两种变形:块嵌套循环连接和索引嵌套循环连接。

    SQL 外链接操作小结 inner join left join right join

    SQL 外链接操作小结 inner join left join right join

    数据文件处理命令小结(tr,sort,cut,paste,join,uniq,split)

    数据文件处理命令小结(tr,sort,cut,paste,join,uniq,split),参数的使用说明和大量实例

    PowerShell String对象方法小结

    从之前的章节中,我们知道PowerShell将一切存储在对象中,那这些对象中包含了一系列中的称之为方法的指令。默认文本存储在String对象中,它包含了许多非常有用的处理文本的命令。例如,要确定一个文件的扩展名,可以...

    Python简单过滤字母和数字的方法小结

    本文实例讲述了Python简单过滤字母和数字的方法。分享给大家供大家参考,具体如下: 实例1 crazystring = 'dade142.!0142f[., ]ad' # 只保留数字 new_crazy = filter(str.isdigit, crazystring) print(''.join(list...

    js中数组的常用方法小结

    主要介绍了js中数组的常用方法,结合实例形式分析了js中的常用数组方法,如push、concat、pop、splice、reverse、join等功能与用法,需要的朋友可以参考下

    JS判断数组里是否有重复元素的方法小结

    本文实例讲述了JS判断数组里是否有重复元素的方法。分享给大家供大家参考,具体如下: ... return /(\x0f[^\x0f]+)\x0f[\s\S]*\1/.test("\x0f" + a.join("\x0f\x0f") + "\x0f"); } mm(ary11) alert(mm(a

    Python数组遍历的简单实现方法小结

    本文实例总结了Python数组遍历的简单实现方法。分享给大家供大家参考,具体如下: >>> os.__file__.split('\\') ['E:', 'Python', 'Python25', 'lib', 'os.pyc'] >>> os.path.split(os.__file__) ('E:\\Python\\...

    Mysql update多表联合更新的方法小结

    下面我建两个表,并执行一系列sql语句,仔细观察sql执行后表中数据的变化,很容易就能理解多表...2. 执行 UPDATE student s JOIN class c ON s.class_id = c.id SET s.class_name=’test11′,c.stu_name=’test11′ stu

    Grails 技术精解与Web开发实践【源码+样章】----下载不扣分,回帖加1分,欢迎下载,童叟无欺

    1.6 本章小结 4 第一篇 入门篇 第2章 Hello Grails 6 2.1 Grails的安装 6 2.1.1 JDK的安装与配置 6 2.1.2 Grails的安装 7 2.2 创建Grails工程 8 2.3 Grails的MVC架构 11 2.4 Scaffold应用程序 14 2.5 开发工具的...

    (E文)基于成本的Oracle优化法则.pdf

    1.5 本章小结 8 1.6 测试用例 8 第2章 表扫描 9 2.1 入门 10 2.2 提高 14 2.2.1 块大小的影响 14 2.2.2 CPU成本计算 16 2.2.3 CPU成本计算的作用 22 2.3 BCHR 24 2.4 并行执行 27 2.5 索引快速全扫描 30 2.6 分区 32...

    Python常见字符串操作函数小结【split()、join()、strip()】

    主要介绍了Python常见字符串操作函数,结合实例形式总结分析了split()、join()及strip()的常见使用技巧与注意事项,需要的朋友可以参考下

    LINQ入门及应用 10/13

     4.3.5 Take、TakeWhile、Skip、SkipWhile小结  4.3.6 Reverse方法  4.3.7 Distinct方法  4.3.8 Union方法  4.3.9 Concat方法  4.3.10 Intersect方法  4.3.11 Except方法  4.3.12 Range方法  4.3.13 Repeat...

    轻松学C#(图解版)

    1.4 小结 11 1.5 习题 12 第二篇 面向对象基础篇 第2章 类和对象 16 2.1 分析Hello World程序 16 2.2 语法规范 17 2.2.1 标识符 17 2.2.2 关键字 18 2.2.3 注释 19 2.3 定义类 20 2.4 实例化对象 20 2.5 小结 20 ...

    TCPIP详解--共三卷

    1.17 小结 13 第2章 链路层 15 2.1 引言 15 2.2 以太网和IEEE 802封装 15 2.3 尾部封装 17 2.4 SLIP:串行线路IP 17 2.5 压缩的SLIP 18 2.6 PPP:点对点协议 18 2.7 环回接口 20 2.8 最大传输单元MTU 21 2.9 路径MTU...

    MySQL5.1性能调优与架构设计.mobi

    1.4 小结 第2章 MySQL架构组成 2.0 引言 2.1 MySQL物理文件组成 2.2 MySQL Server系统架构 2.3 MySQL自带工具使用介绍 2.4 小结 第3章 MySQL存储引擎简介 3.0 引言 3.1 MySQL存储引擎概述 3.2 MyISAM存储...

Global site tag (gtag.js) - Google Analytics