方案一:

public class PrintABC {
public static void main(String[] args) {
AtomicInteger synObj = new AtomicInteger();
Runnable r = new ThreadModel(synObj);
new Thread(r,"A").start();
new Thread(r,"B").start();
new Thread(r,"C").start();
}
} public class ThreadModel implements Runnable {
public volatile AtomicInteger ato; public ThreadModel(AtomicInteger ato) {
this.ato = ato;
} public void run() {
for(int i=;i<;i++) {
synchronized(ato) {
if(ato.get()%==) {
System.out.println("A);
ato.set(ato.get()+);
ato.notifyAll();
} else if(ato.get()%==) {
System.out.println("B);
ato.set(ato.get()+);
ato.notifyAll();
} else if(ato.get()%==) {
System.out.println("C);
ato.set(ato.get()+);
ato.notifyAll();
} else {
ato.wait();
}
}
}
}
}

方案二:

public class printABC {

 public static void main(String [] args) {
AtomicInteger ato = new AtomicInteger();
ThreadModel modelA = new ThreadModel(ato,"A",);
ThreadModel modelB = new ThreadModel(ato,"B",);
ThreadModel modelC = new ThreadModel(ato,"C",);
new Thread(modelA).start();
new Thread(modelB).start();
new Thread(modelC).start();
} } public class ThreadModel { private AtomicInteger ato;
private String printChar;
private int flag;
private int count = ;
public ThreadModel(AtomicInteger ato,String printChar,int flag) {
this.ato = ato;
this.printChar = printChar;
this.flag = flag;
} public void run() {
synchronized(ato) {
while(true) {
if(ato.get()%==flag) {
System.out.println(printChar);
ato.set(ato.get()+);
ato.notifyAll();
count++;
if(count==) {
break;
}
}else {
ato.wait();
}
}
}
} }

练习题之Wait/Notify的更多相关文章

  1. Thread Object wait() notify()基本

    package com.thread.test.thread; import java.util.ArrayDeque; import java.util.Queue; import java.uti ...

  2. Linux基础练习题(二)

    Linux基础练习题(二) 1.复制/etc/skel目录为/home/tuer1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限. [root@www ~]# cp -r ...

  3. shell 脚本之 shell 练习题汇总

    整理了一些 shell 相关的练习题,记录到这里. 1. 请按照这样的日期格式 xxxx-xx-xx 每日生成一个文件,例如:今天生成的文件为 2013-09-23.log, 并且把磁盘的使用情况写到 ...

  4. 如何在 Java 中正确使用 wait, notify 和 notifyAll(转)

    wait, notify 和 notifyAll,这些在多线程中被经常用到的保留关键字,在实际开发的时候很多时候却并没有被大家重视.本文对这些关键字的使用进行了描述. 在 Java 中可以用 wait ...

  5. MySQL练习题

    MySQL练习题 一.表关系 请创建如下表,并创建相关约束 二.操作表 1.自行创建测试数据 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号: 3.查询平均成绩大于60分的同学的学号和平均成 ...

  6. MySQL练习题参考答案

    MySQL练习题参考答案 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号: 思路: 获取所有有生物课程的人(学号,成绩) - 临时表 获取所有有物理课程的人(学号,成绩) - 临时表 根据[ ...

  7. mysql练习题-查询同时参加计算机和英语考试的学生的信息-遁地龙卷风

    (-1)写在前面 文章参考http://blog.sina.com.cn/willcaty. 针对其中的一道练习题想出两种其他的答案,希望网友给出更多回答. (0) 基础数据 student表 +-- ...

  8. java多线程wait notify join

    wait notify 几个注意点: wait 与 notify/notifyAll 方法必须在同步代码块中使用,即要先对调用对象加锁. 当线程执行wait()时,会把当前的锁释放,然后让出CPU,进 ...

  9. 【Java并发系列02】Object的wait()、notify()、notifyAll()方法使用

    一.前言 对于并发编程而言,除了Thread以外,对Object对象的wati和notify对象也应该深入了解其用法,虽然知识点不多. 二.线程安全基本知识 首先应该记住以下基本点,先背下来也无妨: ...

随机推荐

  1. CodeForces 682D Alyona and Strings (四维DP)

    Alyona and Strings 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/D Description After re ...

  2. UVALive 6910 Cutting Tree(离线逆序并查集)

    [题目]:(地址:) http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97671#problem/E [题意]: 给出多棵树和两类操作:操作 ...

  3. [iOS基础控件 - 6.9.2] 静态单元格 QQ功能列表

    使用storyboard设计静态的表格数据   A.实现步骤 1.控制器继承UITableViewController 2.在storyboard中使用TableViewController,删除原来 ...

  4. C++库研究笔记——操作符重载实现类型转换&这样做的意义

    目标: 已知这个接口: std::vector<double> add_vec(double *d1, double *d2) { ..... return result; } 我们自定义 ...

  5. oracle ibatis 存储过程 返回游标 嵌套表

    自己解决问题了 问题总结: 1.index by表不能存储在数据库中的type中,故选择嵌套表. 2.ibatis不支持oracle的复合数据类型的返回.(个人理解) 3.替代方案:用返回oracle ...

  6. ElasticSearch+Springboot实际应用:索引同步建设,搜索过程

    1.介绍 springboot框架,众多自动化的部署和约定配置,造成了springboot的着手麻烦,熟练后可以快速快捷进行开发,常用作快捷开发的java底层框架.各位看官都是大神,自行体会.     ...

  7. nexus建立maven仓库私服及Snapshots、release的版本管理

    环境搭建   1.linux安装maven   wget http://mirrors.cnnic.cn/apache/maven/maven-3/3.0.5/binaries/apache-mave ...

  8. 微软IIS服务器的最佳优化工具- IIS Tuner

      dudu的 <让Windows Server 2008 + IIS 7+ ASP.NET 支持10万个同时请求>,里面涉及到需要手工调整参数的地方.在这篇文章中,我们给你介绍一个IIS ...

  9. 离散信号MATLAB频谱分析程序

    from http://blog.csdn.net/u012129372/article/details/26565611 %FFT变换,获得采样数据基本信息,时域图,频域图 %这里的向量都用行向量, ...

  10. ORACLE表坏块处理

    1.对于普通表,可以考虑使用事件方式处理 事件10231:设置在全表扫描时忽略损坏的数据块 ALTER SYSTEM SET EVENTS='10231 trace name context fore ...