练习题之Wait/Notify
方案一:
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的更多相关文章
- Thread Object wait() notify()基本
package com.thread.test.thread; import java.util.ArrayDeque; import java.util.Queue; import java.uti ...
- Linux基础练习题(二)
Linux基础练习题(二) 1.复制/etc/skel目录为/home/tuer1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限. [root@www ~]# cp -r ...
- shell 脚本之 shell 练习题汇总
整理了一些 shell 相关的练习题,记录到这里. 1. 请按照这样的日期格式 xxxx-xx-xx 每日生成一个文件,例如:今天生成的文件为 2013-09-23.log, 并且把磁盘的使用情况写到 ...
- 如何在 Java 中正确使用 wait, notify 和 notifyAll(转)
wait, notify 和 notifyAll,这些在多线程中被经常用到的保留关键字,在实际开发的时候很多时候却并没有被大家重视.本文对这些关键字的使用进行了描述. 在 Java 中可以用 wait ...
- MySQL练习题
MySQL练习题 一.表关系 请创建如下表,并创建相关约束 二.操作表 1.自行创建测试数据 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号: 3.查询平均成绩大于60分的同学的学号和平均成 ...
- MySQL练习题参考答案
MySQL练习题参考答案 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号: 思路: 获取所有有生物课程的人(学号,成绩) - 临时表 获取所有有物理课程的人(学号,成绩) - 临时表 根据[ ...
- mysql练习题-查询同时参加计算机和英语考试的学生的信息-遁地龙卷风
(-1)写在前面 文章参考http://blog.sina.com.cn/willcaty. 针对其中的一道练习题想出两种其他的答案,希望网友给出更多回答. (0) 基础数据 student表 +-- ...
- java多线程wait notify join
wait notify 几个注意点: wait 与 notify/notifyAll 方法必须在同步代码块中使用,即要先对调用对象加锁. 当线程执行wait()时,会把当前的锁释放,然后让出CPU,进 ...
- 【Java并发系列02】Object的wait()、notify()、notifyAll()方法使用
一.前言 对于并发编程而言,除了Thread以外,对Object对象的wati和notify对象也应该深入了解其用法,虽然知识点不多. 二.线程安全基本知识 首先应该记住以下基本点,先背下来也无妨: ...
随机推荐
- UVA 315 315 - Network(求割点个数)
Network A Telephone Line Company (TLC) is establishing a new telephone cable network. They are con ...
- Spring AOP Interceptor transaction is not working
Problem The Spring AOP transaction is not working in following interceptors? <bean id="testA ...
- Spring入门(1)-第一个Spring项目
1. 创建maven项目,maven相关配置如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi= ...
- [C语言 - 3] 字符串
字符数组 char * 看做一个特殊的字符数组, 在字符串结束为止添加'\0'结束符 (ASCII码0), 没有\0结尾的是普通的字符数组. 使用双引号定义的字符串自动在尾部加上\0 puts(s)函 ...
- MD5验证工具:md5sum
linux 下 shell命令 ,制作md5码 也用于软件的md5校验 MD5算法常常被用来验证网络文件传输的完整性,防止文件被人篡改.MD5 全称是报文摘要算法(Message-Digest ...
- 关于EL表达式的大小写问题。谁来帮我解答?
最近在学习ssh框架,今天遇到了一个非常奇怪的问题.我想在jsp页面中的到session中的数据.<%=s.getUserYes() %>这样写能得到数据, ${sessionScope. ...
- hibernate一对多关系配置
一. 表信息 公司表 cId cName cAdress Null Null Null 表t_company 员工表 sId sName sAge cId Null Null Null Nul ...
- Unity3D之空间转换学习笔记(二):基础数学
这期笔记我们专注Unity提供的各种数学相关的类来学习. 时间Time API文档地址:http://docs.unity3d.com/ScriptReference/Time.html 时间加/减速 ...
- swift 个人笔记
swift是现代编程语言的综合体,灵活而强大. //http://www.cocoachina.com/newbie/basic/2014/0604/8675.html //语法var 变量,let ...
- 如何将松散的dll打包进需要发布的exe
我们需要发布的exe文件很多时候都可能会依赖于一堆松散的dll,如果想在发布的时候只提供exe文件,而不想把一大堆dll一起放在和exe同一个文件夹下,是有方法的,该方法由CLR via C#作者提出 ...