方案一:

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. 【转】新建maven工程为什么jdk会是默认版本 而不是自己设置的版本?

    原文链接:为什么我eclipse新建项目的时候默认的是JRE1.5? 修改Maven中conf目录里的setting.xml文件内容,加上如下内容: <profiles> <!-- ...

  2. CodeForces 176B Word Cut (计数DP)

    Word Cut Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit St ...

  3. WebForm中如何防止页面刷新,后退导致的重复提交

    当用户按下浏览器中的 F5 键刷新当前页面时,对这一过程进行检测所需的操作步骤.页面刷新是浏览器对特定用户操作(按 F5 键或单击"刷新"工具栏按钮)的响应.页面刷新操作是浏览器内 ...

  4. poj3083

    Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8046   Accep ...

  5. Python中的基本语句

    本文简单的介绍下Python的几个基本语句. print语句 print可同时打印多个表达式,只要将他们用逗号隔开. >>> name='Gumy' >>> gre ...

  6. 利用AssetsManager实现在线更新脚本文件lua、js、图片等资源(免去平台审核周期)

    转自:http://www.himigame.com/iphone-cocos2dx/1354.html 首先说明一个问题: 为什么要在线更新资源和脚本文件!? 对于此问题,那要说的太多了,简单概括, ...

  7. applicationContext.xml存放的位置

    web.xml中classpath:和classpath*:  有什么区别? classpath:只会到你的class路径中查找找文件; classpath*:不仅包含class路径,还包括jar文件 ...

  8. Swift学习笔记六

    集合类型(Collection Type) Swift提供三种主要的集合类型:数组(array).集合(set).字典(dictionary).数组是有序的值序列,集合是无序的值序列,字典是无序的键值 ...

  9. Js Pattern - Self Define Function

    This pattern is useful when your function has some initial preparatory work to do andit needs to do ...

  10. Codeforces Gym 100286G Giant Screen 水题

    Problem G.Giant ScreenTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/con ...