仓库类

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()用法的更多相关文章

  1. java多线程-消费者和生产者模式

    /* * 多线程-消费者和生产者模式 * 在实现消费者生产者模式的时候必须要具备两个前提,一是,必须访问的是一个共享资源,二是必须要有线程锁,且锁的是同一个对象 * */ /*资源类中定义了name( ...

  2. Java多线程消费者、生产者的基本思路

    多线程主要考察的就是 线程的同步控制   生产者消费者的思路就是,当 一个线程执行时让另一个线程 挂起就行了 ThreadOne.ThreadTwo同时运行,添加一个变量在一个公共类(下边的Funct ...

  3. OO学习体会与阶段总结(多线程程序)

    前言 在最近一个月的面向对象编程学习中,我们进入了编写多线程程序的阶段.线程的创建.调度和信息传递,共享对象的处理,线程安全类的编写,各种有关于线程的操作在一定程度上增加了近三次作业的复杂度与难度,带 ...

  4. Java程序设计之消费者和生产者

    新建一个Break类,表示食物数量. public class Break { public static final int MAX = 10; //最多一次性煮十个面包 Stack<Inte ...

  5. RabbitMQ消息队列之二:消费者和生产者

    在使用RabbitMQ之前,需要了解RabbitMQ的工作原理. RabbitMQ的工作原理 RabbitMQ是消息代理.从本质上说,它接受来自生产者的信息,并将它们传递给消费者.在两者之间,它可以根 ...

  6. java并发:初探消费者和生产者模式

    消费者和生产者模式 用继承Thread方式,用wait和notifyAll方法实现. 消费者和生产者模式的特点 1. 什么时候生产:仓库没有满的时候,生产者这可以生产,消费者也可以消费,仓库满的时候停 ...

  7. zz剖析为什么在多核多线程程序中要慎用volatile关键字?

    [摘要]编译器保证volatile自己的读写有序,但由于optimization和多线程可以和非volatile读写interleave,也就是不原子,也就是没有用.C++11 supposed会支持 ...

  8. 使用gdb调试多线程程序总结

    转:使用gdb调试多线程程序总结 一直对GDB多线程调试接触不多,最近因为工作有了一些接触,简单作点记录吧. 先介绍一下GDB多线程调试的基本命令. info threads 显示当前可调试的所有线程 ...

  9. JAVA——利用wait和notify实现生产者和消费者

    经典的消费者和生产者的的实现: 注意事项: 1:在循环里面用wait(),因为当线程获得了锁,但是有可能还没有满足其他条件: 2:公用的缓冲池要用锁机制: package demo; import j ...

随机推荐

  1. 利用URLConnection来发送POST和GET请求

    URL的openConnection()方法将返回一个URLConnection对象,该对象表示应用程序和 URL 之间的通信链接.程序可以通过URLConnection实例向该URL发送请求.读取U ...

  2. 【GZAdmin】开源BS demo快速搭建

    下载搭建项目:链接:https://pan.baidu.com/s/1jHZ3Kkm 密码:5k4q 项目源码: GZAdmin_API:https://github.com/GarsonZhang/ ...

  3. [转]将oracle数据库的编码变成utf-8

    1.改客户端字符集:通过WINDOWS的运行菜单运行Regedit,修改注册表 Start -> Run -> Rededit <-| Under registry Editor - ...

  4. 【Python】__slots__ 、@property、多重继承、定制类、枚举类、元类

    __slots__ @property 多重继承 定制类 枚举类 元类 [使用__slots__] 1.动态语言的一个特点就是允许给实例绑定任意的方法和变量,而静态语言(例如Java)必须事先将属性方 ...

  5. PHP(Mysql/Redis)消息队列的介绍及应用场景案例

    在进行网站设计的时候,有时候会遇到给用户大量发送短信,或者订单系统有大量的日志需要记录,还有做秒杀设计的时候,服务器无法承受这种瞬间的压力,无法正常处理,咱们怎么才能保证系统正常有效的运行呢?这时候我 ...

  6. CentOS系统下yum命令的详细使用方法

    yum是什么yum = Yellow dog Updater, Modified 主要功能是更方便的添加/删除/更新RPM包. 它能自动解决包的倚赖性问题. 它能便于管理大量系统的更新问题 yum特点 ...

  7. 用C#连接SFTP服务器并进行上传下载文件

    1.使用软件连接可采用WinSCP进行: 文件协议选择SFTP,端口号默认22 2.使用C#代码操作 参考:http://www.cnblogs.com/binw/p/4065642.html 主要引 ...

  8. GATK的硬过滤

    https://software.broadinstitute.org/gatk/documentation/article.php?id=2806

  9. LeetCode (236):Lowest Common Ancestor of a Binary Search Tree

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  10. linux下tar的使用方法

    1.仅打包 tar -cvf hello.tar hello (输出文件大小为10240) 2.打包后压缩成gzip压缩格式 tar -czvf hello.tar.gz hello (输出文件大小为 ...