多线程顺序打印ABC
2016-04-26 00:00
399 查看
摘要: 多线程的基本应用
在实例化该线程的时候指定线程的名称:
public class ThreadOfWait extends Thread { private int count = 0; @Override public void run() { String name = Thread.currentThread().getName(); while (count <30){ synchronized (this){ if(count %3==0){ printAndIncrease("A"); } if(count %3==1){ printAndIncrease("B"); } if(count %3==2){ printAndIncrease("C"); } } } } public void printAndIncrease(String a){ String name = Thread.currentThread().getName(); if(a.equals(name)){ System.out.println(name); count ++; this.notifyAll(); }else{ try { this.wait(); }catch (Exception e){ } } } }
在实例化该线程的时候指定线程的名称:
public class TestThread { public static void main(String[] arags){ ThreadOfWait wait = ThreadOfWait(); Thread a1 = Thread(wait,"A"); Thread a2 = Thread(wait,"B"); Thread a3 = Thread(wait,"C"); a1.start(); a2.start(); a3.start(); } }
相关文章推荐
- Jackson readValue 方法处理List Map等带泛型的类型
- 购物车的简单示例
- 安卓手机视频分享app
- quartz定时任务时间设置
- 记录
- 使用HostXen云服务器Centos7之后遇到的问题记录
- JAVA反射获取T泛型子类构造方法
- 常用原生JS方法总结(兼容性写法)
- 在同一行布局的技巧
- 移动端:解决ios软键盘获得焦点后,fixed悬挂失败的问题
- json文件转excel表格
- ubuntu服务器分区基础
- 移动广告优化平台”KeyMob”帮开发者投放多家广告平台
- 第一阶段冲刺个人博客09
- 选移动广告优化平台就选KeyMob
- KeyMob--开发者最信任的移动广告优化平台!
- Key移动广告优化平台-国内优质广告优化平台
- 移动广告优化平台新利器--KeyMob!
- Apache的Mesos和Google的Kubernetes 有什么区别?
- 创业的第一百七十五天