通过消费者和生产者的多线程程序,了解Java的wait()和notify()用法
仓库类
public class Store {
private int size = 0;//当前容量
private final int MAX = 10;//最大容量
//向仓库中增加货物
public synchronized void add()
{
while(size >= MAX)//每次执行都要检查是否已满
{
try {
wait();//如果满了,进入等待池等待
} catch (InterruptedException e) {
e.printStackTrace();
}
}
size++;//放入货物
//输出相关信息
System.out.println("*************Add***************");
System.out.println(Thread.currentThread().toString());
System.out.println("Size: " + Integer.toString(size));
//唤醒等待池中的消费者线程
notify();
}
//从仓库中取货物的方法
public synchronized void remove()
{
while(size == 0)//每次执行前检查仓库中是否有货物
{
try {
wait();//如果仓库为空,进入等待池
} catch (InterruptedException e) {
e.printStackTrace();
}
}
size--;//取走货物
//输出相关信息
System.out.println("*************Remove***************");
System.out.println(Thread.currentThread().toString());
System.out.println("Size: " + Integer.toString(size));
//唤醒等待池中的生产者进程
notify();
}
public int getSize()
{
return this.size;
}
}
生产者类
public class Producer extends Thread{
private Store s;
public Producer(Store s) {
this.s = s;
}
@Override
public void run()
{
while(true)
{
s.add();//增加货物
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
消费者类
public class Customer extends Thread{
Store s;
public Customer(Store s)
{
this.s = s;
}
@Override
public void run()
{
while(true)
{
s.remove();//取走货物
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Main
public class Main {
public static void main(String[] args) {
Store s = new Store();
Producer p1 = new Producer(s);
Producer p2 = new Producer(s);
Producer p3 = new Producer(s);
Customer c1 = new Customer(s);
Customer c2 = new Customer(s);
p1.setName("Producer1");
p2.setName("Producer2");
p3.setName("Producer3");
c1.setName("Customer1");
c2.setName("Customer2");
p1.start();
p2.start();
p3.start();
c1.start();
c2.start();
}
}
输出(部分)
*************Add***************
Thread[Producer1,5,main]
Size: 1
*************Remove***************
Thread[Customer2,5,main]
Size: 0
*************Add***************
Thread[Producer3,5,main]
Size: 1
*************Add***************
Thread[Producer2,5,main]
Size: 2
*************Remove***************
Thread[Customer1,5,main]
Size: 1
*************Add***************
Thread[Producer2,5,main]
Size: 2
*************Remove***************
Thread[Customer1,5,main]
Size: 1
*************Remove***************
Thread[Customer2,5,main]
Size: 0
*************Add***************
Thread[Producer1,5,main]
Size: 1
*************Add***************
Thread[Producer3,5,main]
Size: 2
*************Add***************
Thread[Producer2,5,main]
Size: 3
*************Remove***************
Thread[Customer2,5,main]
Size: 2
*************Add***************
Thread[Producer3,5,main]
Size: 3
*************Add***************
Thread[Producer1,5,main]
Size: 4
*************Remove***************
Thread[Customer1,5,main]
Size: 3
*************Add***************
Thread[Producer2,5,main]
Size: 4
*************Add***************
Thread[Producer1,5,main]
Size: 5
*************Remove***************
Thread[Customer1,5,main]
Size: 4
*************Add***************
Thread[Producer3,5,main]
Size: 5
*************Remove***************
Thread[Customer2,5,main]
Size: 4
*************Add***************
Thread[Producer2,5,main]
Size: 5
*************Remove***************
Thread[Customer1,5,main]
Size: 4
*************Add***************
Thread[Producer3,5,main]
Size: 5
*************Remove***************
Thread[Customer2,5,main]
Size: 4
*************Add***************
Thread[Producer1,5,main]
Size: 5
wait()可以让持有当前对象进入等待状态,等待notify()的唤醒。
通过消费者和生产者的多线程程序,了解Java的wait()和notify()用法的更多相关文章
- java多线程-消费者和生产者模式
/* * 多线程-消费者和生产者模式 * 在实现消费者生产者模式的时候必须要具备两个前提,一是,必须访问的是一个共享资源,二是必须要有线程锁,且锁的是同一个对象 * */ /*资源类中定义了name( ...
- Java多线程消费者、生产者的基本思路
多线程主要考察的就是 线程的同步控制 生产者消费者的思路就是,当 一个线程执行时让另一个线程 挂起就行了 ThreadOne.ThreadTwo同时运行,添加一个变量在一个公共类(下边的Funct ...
- OO学习体会与阶段总结(多线程程序)
前言 在最近一个月的面向对象编程学习中,我们进入了编写多线程程序的阶段.线程的创建.调度和信息传递,共享对象的处理,线程安全类的编写,各种有关于线程的操作在一定程度上增加了近三次作业的复杂度与难度,带 ...
- Java程序设计之消费者和生产者
新建一个Break类,表示食物数量. public class Break { public static final int MAX = 10; //最多一次性煮十个面包 Stack<Inte ...
- RabbitMQ消息队列之二:消费者和生产者
在使用RabbitMQ之前,需要了解RabbitMQ的工作原理. RabbitMQ的工作原理 RabbitMQ是消息代理.从本质上说,它接受来自生产者的信息,并将它们传递给消费者.在两者之间,它可以根 ...
- java并发:初探消费者和生产者模式
消费者和生产者模式 用继承Thread方式,用wait和notifyAll方法实现. 消费者和生产者模式的特点 1. 什么时候生产:仓库没有满的时候,生产者这可以生产,消费者也可以消费,仓库满的时候停 ...
- zz剖析为什么在多核多线程程序中要慎用volatile关键字?
[摘要]编译器保证volatile自己的读写有序,但由于optimization和多线程可以和非volatile读写interleave,也就是不原子,也就是没有用.C++11 supposed会支持 ...
- 使用gdb调试多线程程序总结
转:使用gdb调试多线程程序总结 一直对GDB多线程调试接触不多,最近因为工作有了一些接触,简单作点记录吧. 先介绍一下GDB多线程调试的基本命令. info threads 显示当前可调试的所有线程 ...
- JAVA——利用wait和notify实现生产者和消费者
经典的消费者和生产者的的实现: 注意事项: 1:在循环里面用wait(),因为当线程获得了锁,但是有可能还没有满足其他条件: 2:公用的缓冲池要用锁机制: package demo; import j ...
随机推荐
- 【转】使用DataConnectionDialog在运行时设置数据源连接字符串
介绍: DataConnectionDialog 类: 打开“数据连接”对话框,获取用户选择的数据连接信息. 命名空间为:Microsoft.Data.ConnectionUI 所在程序集:Micro ...
- linux在文件中包含某个关键词的指定行插入内容
1. 在包含某个关键字的行上面插入一行文字 sed -i '/wangzai/i\doubi' 1.txt 把内容doubi插入到包含wangzai关键字的上一行 2. 在包含某个关键字的行下面插入一 ...
- TortoiseSVN_1.9.1.267_x64版本控制系统(针对Visual SVN Server)使用简单介绍
软件下载地址:TortoiseSVN(SVN客户端)64位 V1.9.1.267简体中文免费版 软件详细操作说明:TortoiseSVN使用说明书(超详细) 文章内容:此篇是简单记录如何从Visual ...
- WWDC 2017 苹果开发者大会
1.更新了系统固件 iOS 11 macOS High Sierra watchOS 4 tvOS 11 2.更新了硬件以及新设备 升级了 iMac 以及 iMac Pro 升级了 MacBook ...
- dubbo-admin 监控中心 部署
dubbo-admin部署 下载: GitHub:https://github.com/search?q=dubbo-admin 百度网盘: 链接:https://pan.baidu.com/s/1v ...
- 第三方CSS安全吗?
原文:https://jakearchibald.com/201...翻译:疯狂的技术宅 本文首发微信公众号:jingchengyideng欢迎关注,每天都给你推送新鲜的前端技术文章 前一段时间,有很 ...
- Apache httpd服务部署
1. yum安装 yum install httpd yum install httpd-devel yum install httpd-manual 2. 配置 vim /etc/httpd/con ...
- ubuntu下wget的配置文件在哪里
答:/etc/wgetrc 这个文件里可以指定代理,如: http_proxy = http://myproxy.com:8080
- zip unzip tar 压缩解压
yum install -y unzip zip yum安装zip -r mydata.zip mydata mydata目录压缩为mydata.zipunzip mydata.zip - ...
- SSM到Spring Boot-从零开发校园商铺平台
第1章 开发准备 本章包含课程介绍,同时讲解开发网站所需要准备的事情,并且带领大家从零开始搭建一个Maven Web. 1-1 课程导学 1-2 开发准备 第2章 项目设计和框架搭建 本章主要先带领大 ...