1.wait()方法
  该方法继承于Object类。在调用obj.wait()方法后,当前线程会失去obj的锁。待其他线程调用obj.notify()或notifyAll()方法后进入锁等待池,争抢到锁后进行执行wait()后续代码。
  wait(long time)方法超时自动结束阻塞,进入锁等待池,争抢到锁后进行执行wait()后续代码。
jdk文档描述:
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.
2.notify()方法
  jdk文档描述
  Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object's monitor by calling one of the wait methods.
  The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.
This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:
  • 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.
Only one thread at a time can own an object's monitor.
 源码:
/**
* @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();
3.notifyAll()方法
jdk文档描述
  Wakes up all threads that are waiting on this object's monitor. A thread waits on an object's monitor by calling one of the wait methods.
 
  The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object. The awakened threads will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened threads enjoy no reliable privilege or disadvantage in being the next thread to lock this object.
 
  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.
源码
* @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();
 
4.生产者消费者模型
  生产者不停生产,消费者不停消费。存储产品数量为[0,maxSize]。每次生产或消费时都要获取产品的监视器,当产品数量不符合要求不能生产或消费时,当前操作线程放出监视器,进入阻塞状态,同时唤醒所有在阻塞状态的生产者和消费者。存在一个runnable队列和一个blocked队列,runnale队列中存储的线程等待cpu调度随时可以运行,blocked只有调用obj.notify或notifyall方法后才能进入runnable队列。
代码如下:
 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() 生产者消费者模型的更多相关文章

  1. java多线程:线程间通信——生产者消费者模型

    一.背景 && 定义 多线程环境下,只要有并发问题,就要保证数据的安全性,一般指的是通过 synchronized 来进行同步. 另一个问题是,多个线程之间如何协作呢? 我们看一个仓库 ...

  2. python_way ,day11 线程,怎么写一个多线程?,队列,生产者消费者模型,线程锁,缓存(memcache,redis)

    python11 1.多线程原理 2.怎么写一个多线程? 3.队列 4.生产者消费者模型 5.线程锁 6.缓存 memcache redis 多线程原理 def f1(arg) print(arg) ...

  3. Java多线程(九):生产者消费者模型

    生产者消费者模型 生产者:生产任务的个体: 消费者:消费任务的个体: 缓冲区:是生产者和消费者之间的媒介,对生产者和消费者解耦. 当 缓冲区元素为满,生产者无法生产,消费者继续消费: 缓冲区元素为空, ...

  4. (三)(2)wait/notify实现生产者-消费者模型,join方法

    生产者,消费者模型 举个例子来说明,厨师,服务员,厨师做菜,服务员上菜,如果厨师没有做好菜,那么服务员就无法上菜,厨师做好了菜,然后通知服务员消费(上菜).在这个过程之中,厨师扮演的就是生产者,服务员 ...

  5. Java多线程使用wait和notify实现生产者消费者模型

    Java多线程使用wait和notify这两个关键字的学习,通过实现生成者与消费者来成对研究比较科学. 从两个字的意义来讲就是等待与通知这个简单道理. 现在先模拟一个缓存区存储,是用一个list实现的 ...

  6. Python多线程的简单实现(生产者消费者模型)

    __author__ = "JentZhang" import time, threading, queue q = queue.Queue(maxsize=) # 声明队列 de ...

  7. java多线程15 :wait()和notify() 的生产者/消费者模式

    什么是生产者/消费者模型 一种重要的模型,基于等待/通知机制.生产者/消费者模型描述的是有一块缓冲区作为仓库,生产者可将产品放入仓库,消费者可以从仓库中取出产品,生产者/消费者模型关注的是以下几个点: ...

  8. Java多线程14:生产者/消费者模型

    什么是生产者/消费者模型 一种重要的模型,基于等待/通知机制.生产者/消费者模型描述的是有一块缓冲区作为仓库,生产者可将产品放入仓库,消费者可以从仓库中取出产品,生产者/消费者模型关注的是以下几个点: ...

  9. Python多线程-生产者消费者模型

    用多线程和队列来实现生产者消费者模型 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import threading imp ...

随机推荐

  1. java 动态规划解决上楼梯问题

    问题描述: 你正在爬楼梯. 它需要n步才能达到顶峰. 每次你可以爬1或2步. 您可以通过多少不同的方式登顶? 注意:给定n将是一个正整数. Example 1: Input: 2 Output: 2 ...

  2. JavaWeb开发中的分层思想(一)

    JavaWeb开发分层思想(一) 一.认识DAO.Service.Controller层 DAO(Data Access Object) 1.直接看英文意思就是"数据访问对象",也 ...

  3. IPFS矿机封装原理解释

    近期无论是从媒体.新闻的高度曝光,还是市场拓展的覆盖度来看,IPFS 俨然成为今年最值得关注的行业话题与入场趋势.对于许多刚了解 IPFS 的小白来说,矿机的「封装」.「有效算力」和「原值算力」这些概 ...

  4. java例题_32 取一个整数a从右端开始的4~7位

    1 /*32 [程序 32 左移右移] 2 题目:取一个整数 a 从右端开始的 4-7 位. 3 */ 4 5 /*分析 6 * 从右端开始的第四位相当于原数除以1000后结果的最后一位数, 7 * ...

  5. 利用过滤器Filter和特性Attribute实现对Web API返回结果的封装和统一异常处理

    在我们开发Web API应用的时候,我们可以借鉴ABP框架的过滤器Filter和特性Attribute的应用,实现对Web API返回结果的封装和统一异常处理,本篇随笔介绍利用AuthorizeAtt ...

  6. 前端学习 node 快速入门 系列 —— 报名系统 - [express]

    其他章节请看: 前端学习 node 快速入门 系列 报名系统 - [express] 最简单的报名系统: 只有两个页面 人员信息列表页:展示已报名的人员信息列表.里面有一个报名按钮,点击按钮则会跳转到 ...

  7. [状压DP]关灯问题II

    关 灯 问 题 I I 关灯问题II 关灯问题II 题目描述 现有n盏灯,以及 m m m个按钮.每个按钮可以同时控制这 n n n盏灯--按下了第 i i i个按钮,对于所有的灯都有一个效果.按下i ...

  8. vue+quasar+electron+springboot+mysql撸一个TODO LIST 看板

    先看效果 写本项目的目的有几点: 学习下vue+electron桌面开发 学习下java和spring开发(本人一直使用PHP) 一直缺少一款能适合自己的TODO LIST软件,能有桌面端的 可直接打 ...

  9. CTF导引(一)

    ctf预备知识: 视频:https://www.bilibili.com/video/av62214776?from=search&seid=1436604431801225989 CTF比赛 ...

  10. Oracle-DG 主库将log_archive_dest_state_2远程归档线程参数设置为defer,为什么dg还是处于实时同步状态?

    一.需求,前段时间,墨天伦有个小伙伴咨询了这个问题,搞了测试环境测试下. Oracle-DG 主库将log_archive_dest_state_2远程归档线程参数设置为defer,为什么dg还是处于 ...