java-多线程-join函数
2016-04-05 16:29
549 查看
join()>>不带参数
线程A调用线程B.join,意思就是线程A并入了线程B,当执行完线程B,再去执行线程A后续动作
join(int keepTims)>>带参数,与上面类似,区别在于线程B保持并入线程A中有保持时间,超过改时间,两线程再次分开
案例1
案例2
案例三
线程A调用线程B.join,意思就是线程A并入了线程B,当执行完线程B,再去执行线程A后续动作
join(int keepTims)>>带参数,与上面类似,区别在于线程B保持并入线程A中有保持时间,超过改时间,两线程再次分开
案例1
package com.wp.join; public class JoinTest implements Runnable { public static int a = 0; @Override public void run() { for(;;){ a++; System.out.println("变量A="+a); long now = System.currentTimeMillis(); while (System.currentTimeMillis() - now < 1000) { } if(a>8){ break; } } } public static void main(String[] args) { Thread task = new Thread(new JoinTest()); task.start(); try { task.join(9000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("End var = "+a); } }
案例2
package com.wp.join; public class JoinTest2 { private static int num = 0; static class JoinThreadA extends Thread { @Override public void run() { while (true) { num++; long now = System.currentTimeMillis(); while (System.currentTimeMillis() - now < 200) { } System.out.println("选手A已跑" + num + "米"); if (num >= 100) { break; } } } } static class JoinThreadB extends Thread { protected JoinThreadA joina; public JoinThreadB(JoinThreadA joina) { this.joina = joina; } @Override public void run() { try { joina.join(); } catch (InterruptedException e) { e.printStackTrace(); } while (true) { num++; long now = System.currentTimeMillis(); while (System.currentTimeMillis() - now < 100) { } System.out.println("选手B已跑" + num + "米"); if (num >= 200) { break; } } } } static class JoinThreadC extends Thread { protected JoinThreadB joinb; public JoinThreadC(JoinThreadB joinb) { this.joinb = joinb; } @Override public void run() { try { joinb.join(); } catch (InterruptedException e) { e.printStackTrace(); } while (true) { num++; long now = System.currentTimeMillis(); while (System.currentTimeMillis() - now < 50) { } System.out.println("选手C已跑" + num + "米"); if (num >= 300) { break; } } } } static class JoinThreadD extends Thread { protected JoinThreadC joinc; public JoinThreadD(JoinThreadC joinc) { this.joinc = joinc; } @Override public void run() { try { joinc.join(); } catch (InterruptedException e) { e.printStackTrace(); } while (true) { num++; long now = System.currentTimeMillis(); while (System.currentTimeMillis() - now < 20) { } System.out.println("选手D已跑" + num + "米"); if (num >= 400) { break; } } } } public static void main(String[] args) throws Exception { System.out.println("比赛开始"); JoinThreadA joina = new JoinThreadA(); JoinThreadB joinb = new JoinThreadB(joina); JoinThreadC joinc = new JoinThreadC(joinb); JoinThreadD joind = new JoinThreadD(joinc); joina.start(); joinb.start(); joinc.start(); joind.start(); joind.join(); System.out.println("比赛结束,最终跑完" + num + "米"); } }
案例三
package com.wp.join; import java.text.SimpleDateFormat; import java.util.Date; public class JoinTest3 { final static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); static class JoinThread extends Thread { Thread syncThread; public JoinThread(Thread syncThread) { this.syncThread = syncThread; } @Override public void run() { synchronized (syncThread) { System.out.println("线程A START"); long now = System.currentTimeMillis(); while (System.currentTimeMillis() - now < 5000) { } System.out.println("线程A END"); } } } public static void main(String[] args) { System.out.println("开始时间:"+format.format(new Date())); Thread t1 = new Thread(new Runnable() { @Override public void run() { System.out.println("Begin sleep"); long now = System.currentTimeMillis(); while (System.currentTimeMillis() - now < 5000) { } System.out.println("End sleep"); } }); Thread t2 = new JoinThread(t1); t1.start(); t2.start(); try { t1.join(1000);//因为T1线程对象的锁被T2持有,所以必须等T2跑完,释放锁 所以此处等待的时间 1000+5000 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("结束时间:"+format.format(new Date())); } }
相关文章推荐
- JAVA 封神之路
- java 读取文件路径空格和中文的处理
- java8中map的新方法--replace
- JAVA安全控制框架 —— Shiro
- Spring MVC 配置多数据源
- Java堆栈存储数据类型
- HashMap 源码解析
- Java多线程之ExecutorService
- Java并发编程系列之十八:读写锁
- Java HotSpot VM Options
- Eclipse 自动提示设置
- java的下拉框选择
- 浅谈Spring Mvc的设计模式
- Java 集合
- 【Spring MVC拦截器+logback日志+自定义注解】实现用户鉴权登陆和访问日志记录
- 本地Spring容器关闭的时机
- spring框架学习(四)自动装配
- spring框架学习(三)junit单元测试
- spring框架学习(二)依赖注入
- spring框架学习(一)