Java多线程之Wait()和Notify()
1.Wait()和Notify、NotifyAll都是Object的方法
2.多线程的协作是通过控制同一个对象的Wait()和Notify()完成
3.当调用Wait()方法时,当前线程进入阻塞状态,直到有另一线程调用了该对象的Notify()方法
package Thread.Wait; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; class Car {
private boolean waxOn = false; //上蜡
public synchronized void waxed() {
waxOn = true;
notifyAll();
} //抛光
public synchronized void buffed() {
waxOn = false;
notifyAll();
} public synchronized void waitForWaxing() throws InterruptedException {
while (waxOn == false)
wait();
} public synchronized void waitForBuffing() throws InterruptedException {
while (waxOn == true)
wait();
}
} class WaxOn implements Runnable {
private Car car; public WaxOn(Car c) {
car = c;
} @Override
public void run() {
try {
while (!Thread.interrupted()) {
System.out.println("Wax On!");
TimeUnit.MILLISECONDS.sleep(200);
car.waxed();//上完蜡 waxOn = true;notifyAll();
car.waitForBuffing();//等待抛光while (waxOn == true) wait();
}
} catch (Exception e) {
System.out.println("Exiting via interrupt");
}
System.out.println("Ending Wax On task");
}
} class WaxOff implements Runnable {
private Car car; public WaxOff(Car c) {
car = c;
} @Override
public void run() {
try {
while (!Thread.interrupted()) {
car.waitForWaxing();//等待上蜡 while (waxOn == false) wait();
System.out.println("Wax Off!");
TimeUnit.MILLISECONDS.sleep(200);
car.buffed();//已经抛光 waxOn = false; notifyAll();
}
} catch (Exception e) {
System.out.println("Exiting via interrupt");
}
System.out.println("Ending Wax Off task");
}
} public class WaxOMatic {
public static void main(String[] args) throws Exception {
Car car = new Car();
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(new WaxOff(car));
exec.execute(new WaxOn(car));
TimeUnit.SECONDS.sleep(5);
exec.shutdownNow();
}
}
Java多线程之Wait()和Notify()的更多相关文章
- java多线程之wait和notify协作,生产者和消费者
这篇直接贴代码了 package cn.javaBase.study_thread1; class Source { public static int num = 0; //假设这是馒头的数量 } ...
- Java多线程之wait、notify/notifyAll 详解,用wait 和notifyAll 以及synchronized实现阻塞队列,多线程拓展之ReentrantLock与Condition
前言:这几天看了很多关于多线程的知识,分享一波.(但是目前接触的项目还未用到过,最多用过线程池,想看线程池 请看我之前的博客) 关于基本的理论等 参考如下: https://www.cnblogs.c ...
- java多线程之wait和notify
多线程中的通信是非常重要的概念,线程直接实现通信就可以并发完成很多复杂工作. java在Object类中就设计了wait()和notify()两个方法,以解决这个问题. 1.释义: wait()方法将 ...
- java 多线程之synchronized wait/notify解决买票问题
一.Java线程具有五中基本状态 新建状态(New):当线程对象对创建后,即进入了新建状态,如:Thread t = new MyThread(); 就绪状态(Runnable):当调用线程对象的st ...
- 1.Java多线程之wait和notify
1.首先我们来从概念上理解一下这两个方法: (1)obj.wait(),当obj对象调用wait方法时,这个方法会让当前执行了这条语句的线程处于等待状态(或者说阻塞状态),并释放调用wait方法的对象 ...
- JAVA多线程之wait/notify
本文主要学习JAVA多线程中的 wait()方法 与 notify()/notifyAll()方法的用法. ①wait() 与 notify/notifyAll 方法必须在同步代码块中使用 ②wait ...
- JAVA多线程之volatile 与 synchronized 的比较
一,volatile关键字的可见性 要想理解volatile关键字,得先了解下JAVA的内存模型,Java内存模型的抽象示意图如下: 从图中可以看出: ①每个线程都有一个自己的本地内存空间--线程栈空 ...
- java多线程之yield,join,wait,sleep的区别
Java多线程之yield,join,wait,sleep的区别 Java多线程中,经常会遇到yield,join,wait和sleep方法.容易混淆他们的功能及作用.自己仔细研究了下,他们主要的区别 ...
- Java多线程之ConcurrentSkipListMap深入分析(转)
Java多线程之ConcurrentSkipListMap深入分析 一.前言 concurrentHashMap与ConcurrentSkipListMap性能测试 在4线程1.6万数据的条件下, ...
随机推荐
- Machine and Deep Learning with Python
Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...
- java linux book
calvin1978.blogcn.com/articles/javabookshelf.html
- MapReduce: 一个巨大的倒退
前言 databasecolumn 的数据库大牛们(其中包括PostgreSQL的最初伯克利领导:Michael Stonebraker)最近写了一篇评论当前如日中天的MapReduce 技术的文章, ...
- 替换linux下的rm命令,并对-rf进行判断
补充了对根目录,以及-r -rf -f的判断 1.使用root用户在/usr/local目录创建.rmov.sh #!/bin/shPARA_CNT=$#TRASH_DIR="/data01 ...
- WCF入门教程一[什么是WCF]
一.概述 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NE ...
- Python 统计IIS日志行数
__author__ = 'Administrator' import codecs def blocks(file, size=65536): while True: b = file.read(s ...
- OpenJudge计算概论-称体重【枚举法、信息数字化】
/*====================================================================== 称体重 总时间限制: 1000ms 内存限制: 655 ...
- HDFS 文件读写过程
HDFS 文件读写过程 HDFS 文件读取剖析 客户端通过调用FileSystem对象的open()来读取希望打开的文件.对于HDFS来说,这个对象是分布式文件系统的一个实例. Distributed ...
- CSharp使用log4net记录日志
一.先下载log4net.dll.Newtonsoft.Json.dll和配置log4net.config 相关DLL下载地址:log4net相关dll 下载地址:http://logging.apa ...
- Andaroid L新特性
1.Material Design”(材料设计)的全新设计理念,和Holo相比,Material Design更加色彩丰富,不像Holo那样灰暗 2.1)卡片风格(锁屏界面) 2)环动式设计 And ...