通过消费者和生产者的多线程程序,了解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 ...
随机推荐
- Netty资料
netty 资料 转自 http://calvin1978.blogcn.com/articles/netty-info.html Netty资料皆阵列在前 Posted on 2016-08- ...
- 负载均衡技术在CDN中发挥着重要作用
转载地址:http://www.qicaispace.com/gonggao/server/page01/info07.asp CDN是一个经策略性部署的整体系统,能够帮助用户解决分布式存储.负载均衡 ...
- JQuery的click、bind、delegate、off、unbind
.click与.bind .click和.bind都是给每个元素绑定事件,对于只绑定一个click事件,.bind事件的简写就是.click那种方式. 这两种方式都会出现两个问题: 第一个问题,如果要 ...
- js正则匹配两位小数
今天写一个用js正则校验最多保留两位小数的格式. a = /^\d+|\d+\.\d{1,2}$/; 测试 a.test(1.222); 结果:true 一下蒙了,怎么可能,最后找了好久,原来需要把^ ...
- MVC 中 注册不成功 或其他操作不成功 提示办法
在Controller中 .cs public ActionResult AddUser(User u) { …… try { …… GetInsertUser(u); // 注册 ...
- logstash编写2以及结合kibana使用
有时候根据日志的内容,可能一行不能全部显示,会延续在下一行,为了将上下内容关联在一起,于是codec插件中的multiline插件 就派上用场了,源日志内容: [2017-09-20T16:04:34 ...
- 20145109 《Java程序设计》第九周学习总结
JDBC 1 . DriverManager Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); 2 . ...
- (转)C#调用C函数(DLL)传递参数问题
备忘: 1.C函数参数为字符串char*.如果是入参,对应C#中string或StringBuilder:如果是出参对应C#中StringBuider: 2.C函数参数为结构体指针,需在C#中对应定义 ...
- PHP 根据IP地址获取所在城市
header('Content-Type:text/html;Charset=utf-8'); function GetIp(){ $realip = ''; $unknown = 'unknown' ...
- java 反序列化 漏洞
java在反序列化的时候会默认调用被重写的readObject(ObjectInputStream )方法. 在远程服务端一个需要反序列化的接口中,比如一个web服务,他那个接口调用链中有反序 ...