多线程-4.wait() notify() notifyAll() 生产者消费者模型
Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0). The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution. As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
synchronized (obj) {
while (<condition does not hold>)
obj.wait();
... // Perform action appropriate to condition
}
This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.
- By executing a synchronized instance method of that object.
- By executing the body of a synchronized statement that synchronizes on the object.
- For objects of type Class, by executing a synchronized static method of that class.
/**
* @throws IllegalMonitorStateException if the current thread is not
* the owner of this object's monitor.
* @see java.lang.Object#notifyAll()
* @see java.lang.Object#wait()
*/
public final native void notify();
* @throws IllegalMonitorStateException if the current thread is not
* the owner of this object's monitor.
* @see java.lang.Object#notify()
* @see java.lang.Object#wait()
*/
public final native void notifyAll();
1 public class TestSetAndGet {
2 static class Goods {
3 public String lock = Thread.currentThread().getName()+"test";
4 volatile static int number = 1;
5 private static final int max_size = 10;
6
7 // 生产者增加一个产品
8 public void set() throws InterruptedException {
9 synchronized (this) {
10 while (number >= max_size) {
11 // wait()方法一定要持有锁对象的minitor监视器,所以一定要放在notify之前
12 wait();
13 }
14 if (number == 0) {
15 System.out.print("-同时唤醒所有的消费者和生产者。");
16 notifyAll();
17 }
18 number++;
19 System.out.print("生产者增加一个,当前数量:"+number);
20 System.out.println();
21 }
22 }
23 // 消费者消费一个产品
24 public void get() throws InterruptedException {
25 synchronized (this) {
26 while (number <= 0) {
27 // wait()方法一定要持有锁对象的minitor监视器,所以一定要放在notify之前
28 wait();
29 }
30 if (number == max_size) {
31 System.out.print("唤醒所有的消费者和生产者。");
32 notifyAll();
33 }
34 number--;
35 System.out.print("消费者消费一个,当前数量:"+number);
36 System.out.println();
37 }
38 }
39 }
40
41 public static void main(String[] args) throws InterruptedException {
42 Goods goods = new Goods();
43
44 for (int i = 0; i < 20; i++) {
45 // 生产者慢一点,可以让货物在0-1之间盘桓
46 //Thread.sleep(100);
47 new Thread(()->{
48 try {
49 goods.set();
50 } catch (InterruptedException e) {
51 e.printStackTrace();
52 }
53 }).start();
54 }
55 for (int i = 0; i < 20; i++) {
56 new Thread(()->{
57 try {
58 goods.get();
59 } catch (InterruptedException e) {
60 e.printStackTrace();
61 }
62 }).start();
63 }
64
65 }
66 }
多线程-4.wait() notify() notifyAll() 生产者消费者模型的更多相关文章
- java多线程:线程间通信——生产者消费者模型
一.背景 && 定义 多线程环境下,只要有并发问题,就要保证数据的安全性,一般指的是通过 synchronized 来进行同步. 另一个问题是,多个线程之间如何协作呢? 我们看一个仓库 ...
- python_way ,day11 线程,怎么写一个多线程?,队列,生产者消费者模型,线程锁,缓存(memcache,redis)
python11 1.多线程原理 2.怎么写一个多线程? 3.队列 4.生产者消费者模型 5.线程锁 6.缓存 memcache redis 多线程原理 def f1(arg) print(arg) ...
- Java多线程(九):生产者消费者模型
生产者消费者模型 生产者:生产任务的个体: 消费者:消费任务的个体: 缓冲区:是生产者和消费者之间的媒介,对生产者和消费者解耦. 当 缓冲区元素为满,生产者无法生产,消费者继续消费: 缓冲区元素为空, ...
- (三)(2)wait/notify实现生产者-消费者模型,join方法
生产者,消费者模型 举个例子来说明,厨师,服务员,厨师做菜,服务员上菜,如果厨师没有做好菜,那么服务员就无法上菜,厨师做好了菜,然后通知服务员消费(上菜).在这个过程之中,厨师扮演的就是生产者,服务员 ...
- Java多线程使用wait和notify实现生产者消费者模型
Java多线程使用wait和notify这两个关键字的学习,通过实现生成者与消费者来成对研究比较科学. 从两个字的意义来讲就是等待与通知这个简单道理. 现在先模拟一个缓存区存储,是用一个list实现的 ...
- Python多线程的简单实现(生产者消费者模型)
__author__ = "JentZhang" import time, threading, queue q = queue.Queue(maxsize=) # 声明队列 de ...
- java多线程15 :wait()和notify() 的生产者/消费者模式
什么是生产者/消费者模型 一种重要的模型,基于等待/通知机制.生产者/消费者模型描述的是有一块缓冲区作为仓库,生产者可将产品放入仓库,消费者可以从仓库中取出产品,生产者/消费者模型关注的是以下几个点: ...
- Java多线程14:生产者/消费者模型
什么是生产者/消费者模型 一种重要的模型,基于等待/通知机制.生产者/消费者模型描述的是有一块缓冲区作为仓库,生产者可将产品放入仓库,消费者可以从仓库中取出产品,生产者/消费者模型关注的是以下几个点: ...
- Python多线程-生产者消费者模型
用多线程和队列来实现生产者消费者模型 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import threading imp ...
随机推荐
- Java视频教程免费分享(网盘直接取)
Java基础 Java马士兵:链接:https://pan.baidu.com/s/1jJRvxGi密码:v3xb Java刘意:链接:https://pan.baidu.com/s/1kVZQCqr ...
- mysql 单机多实例重启数据库服务
1.# cat db.txtbackend 3310base 3320storage 3330payment 3340promotion 3350 2.# cat restart_mysql_slav ...
- EntityFrameworkCore之工作单元的封装
1. 简介 2. DbContext 生命周期和使用规范 2.1. 生命周期 2.2. 使用规范 2.3. 避免 DbContext 线程处理问题 3. 封装-工作单元 3.1. 分析 3.2. 设计 ...
- springboot集成swagger实战(基础版)
1. 前言说明 本文主要介绍springboot整合swagger的全过程,从开始的swagger到Knife4j的进阶之路:Knife4j是swagger-bootstarp-ui的升级版,包括一些 ...
- ffmpeg第五篇:让水印图片旋转起来
这篇把上次挖的坑填上 ffmpeg正式篇的上一篇(传送门)说了,这一篇要让水印旋转起来,但是后面有事情一直没有时间搞,今天,它来了............ 如果想实现旋转的功能,需要使用ffmpeg过 ...
- SCIP:构造数据抽象--数据结构中队列与树的解释
现在到了数学抽象中最关键的一步:让我们忘记这些符号所表示的对象.不应该在这里停滞不前,有许多操作可以应用于这些符号,而根本不必考虑它们到底代表着什么东西. --Hermann Weyi <思维的 ...
- [hash-bfs]USACO 3.2 Magic Squares 魔板
魔 板 魔板 魔板 题目描述 在成功地发明了魔方之后,拉比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 我们知道魔板的每一个方格都有一种颜色 ...
- Dynamics CRM实体系列之字段
本节开始讲实体中的基础数据存储对象,也就是字段. Dynamics CRM目前总共有13种字段类型,分别为单行文本.选项集.多选选项集.两个选项.图像.整数.浮点数.十进制数.货币.多行文本.日期和时 ...
- Day08_40_集合_List
List集合 List接口是继承Collection接口,所以Collection集合中有的方法,List集合也会继承过来,可以直接使用. All Superinterfaces: Collectio ...
- Pandas的loc,iloc与ix的用法及区别
1.先来谈一谈loc,loc这个方法就是你有啥我就用啥,你没有的我不用,pandas对象的index,columns有什么,pd.loc[index,column],index就是pd.index的其 ...