(Java生产者消费者问题)http://blog.csdn.net/jhj735412/article/details/6931135
转载自:http://blog.csdn.net/jhj735412/article/details/6931135
生产者消费者问题是研究多线程程序时绕不开的问题,它的描述是有一块生产者和消费者共享的有界缓冲区,生产者往缓冲区放入产品,消费者从缓冲区取走产品,这个过程可以无休止的执行,不能因缓冲区满生产者放不进产品而终止,也不能因缓冲区空消费者无产品可取而终止。
import java.util.LinkedList;
public class Sycn1...{
private LinkedList<Object> myList =new LinkedList<Object>();
private int MAX = 10;
public Sycn1()...{
}
public void start()...{
new Producer().start();
new Consumer().start();
}
public static void main(String[] args) throws Exception...{
Sycn1 s1 = new Sycn1();
s1.start();
}
class Producer extends Thread...{
public void run()...{
while(true)...{
synchronized(myList)...{
try...{
while(myList.size() == MAX)...{
System.out.println("warning: it's full!");
myList.wait();
}
Object o = new Object();
if(myList.add(o))...{
System.out.println("Producer: " + o);
myList.notify();
}
}catch(InterruptedException ie)...{
System.out.println("producer is interrupted!");
}
}
}
}
}
class Consumer extends Thread...{
public void run()...{
while(true)...{
synchronized(myList)...{
try...{
while(myList.size() == 0)...{
System.out.println("warning: it's empty!");
myList.wait();
}
Object o = myList.removeLast();
System.out.println("Consumer: " + o);
myList.notify();
}catch(InterruptedException ie)...{
System.out.println("consumer is interrupted!");
}
}
}
}
}
}
import java.util.LinkedList;
import java.util.concurrent.locks.*;
public class Sycn2...{
private LinkedList<Object> myList = new LinkedList<Object>();
private int MAX = 10;
private final Lock lock = new ReentrantLock();
private final Condition full = lock.newCondition();
private final Condition empty = lock.newCondition();
public Sycn2()...{
}
public void start()...{
new Producer().start();
new Consumer().start();
}
public static void main(String[] args) throws Exception...{
Sycn2 s2 = new Sycn2();
s2.start();
}
class Producer extends Thread...{
public void run()...{
while(true)...{
lock.lock();
try...{
while(myList.size() == MAX)...{
System.out.println("warning: it's full!");
full.await();
}
Object o = new Object();
if(myList.add(o))...{
System.out.println("Producer: " + o);
empty.signal();
}
}catch(InterruptedException ie)...{
System.out.println("producer is interrupted!");
}finally...{
lock.unlock();
}
}
}
}
class Consumer extends Thread...{
public void run()...{
while(true)...{
lock.lock();
try...{
while(myList.size() == 0)...{
System.out.println("warning: it's empty!");
empty.await();
}
Object o = myList.removeLast();
System.out.println("Consumer: " + o);
full.signal();
}catch(InterruptedException ie)...{
System.out.println("consumer is interrupted!");
}finally...{
lock.unlock();
}
}
}
}
}
import java.util.concurrent.*;
public class Sycn3...{
private LinkedBlockingQueue<Object> queue = new LinkedBlockingQueue<Object>(10);
private int MAX = 10;
public Sycn3()...{
}
public void start()...{
new Producer().start();
new Consumer().start();
}
public static void main(String[] args) throws Exception...{
Sycn3 s3 = new Sycn3();
s3.start();
}
class Producer extends Thread...{
public void run()...{
while(true)...{
//synchronized(this){
try...{
if(queue.size() == MAX)
System.out.println("warning: it's full!");
Object o = new Object();
queue.put(o);
System.out.println("Producer: " + o);
}catch(InterruptedException e)...{
System.out.println("producer is interrupted!");
}
//}
}
}
}
class Consumer extends Thread...{
public void run()...{
while(true)...{
//synchronized(this){
try...{
if(queue.size() == 0)
System.out.println("warning: it's empty!");
Object o = queue.take();
System.out.println("Consumer: " + o);
}catch(InterruptedException e)...{
System.out.println("producer is interrupted!");
}
//}
}
}
}
}
import java.io.*;
public class Sycn4...{
private PipedOutputStream pos;
private PipedInputStream pis;
//private ObjectOutputStream oos;
//private ObjectInputStream ois;
public Sycn4()...{
try...{
pos = new PipedOutputStream();
pis = new PipedInputStream(pos);
//oos = new ObjectOutputStream(pos);
//ois = new ObjectInputStream(pis);
}catch(IOException e)...{
System.out.println(e);
}
}
public void start()...{
new Producer().start();
new Consumer().start();
}
public static void main(String[] args) throws Exception...{
Sycn4 s4 = new Sycn4();
s4.start();
}
class Producer extends Thread...{
public void run() ...{
try...{
while(true)...{
int b = (int) (Math.random() * 255);
System.out.println("Producer: a byte, the value is " + b);
pos.write(b);
pos.flush();
//Object o = new MyObject();
//oos.writeObject(o);
//oos.flush();
//System.out.println("Producer: " + o);
}
}catch(Exception e)...{
//System.out.println(e);
e.printStackTrace();
}finally...{
try...{
pos.close();
pis.close();
//oos.close();
//ois.close();
}catch(IOException e)...{
System.out.println(e);
}
}
}
}
class Consumer extends Thread...{
public void run()...{
try...{
while(true)...{
int b = pis.read();
System.out.println("Consumer: a byte, the value is " + String.valueOf(b));
//Object o = ois.readObject();
//if(o != null)
//System.out.println("Consumer: " + o);
}
}catch(Exception e)...{
//System.out.println(e);
e.printStackTrace();
}finally...{
try...{
pos.close();
pis.close();
//oos.close();
//ois.close();
}catch(IOException e)...{
System.out.println(e);
}
}
}
}
//class MyObject implements Serializable {
//}
}
(Java生产者消费者问题)http://blog.csdn.net/jhj735412/article/details/6931135相关推荐
- 转自: http://blog.csdn.net/xiaxiaorui2003/article/details/3838631
2019独角兽企业重金招聘Python工程师标准>>> 转自: http://blog.csdn.net/xiaxiaorui2003/article/details/3838631 ...
- http://blog.csdn.net/neiloid/article/details/7037093#
1. 显示系统中全部Android平台: android list targets 2. 显示系统中全部AVD(模拟器): android list avd 3. 创建AVD(模拟器): androi ...
- 系统权限管理设计 (转:http://blog.csdn.net/chexlong/article/details/37697555)
权限设计(转:http://blog.csdn.net/chexlong/article/details/37697555) 1. 前言: 权限管理往往是一个极其复杂的问题,但也可 ...
- http://blog.csdn.net/luoshengyang/article/details/6651971
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6651971 在Android系统中,提供了独特 ...
- GitHu的诞生记 转 https://blog.csdn.net/fanpeihua123/article/details/58151161
前言:这篇文章应读者要求所写,主要聊聊版本管理系统(Version Control System,简称 VCS), 这篇文章不能让你学会一门技术, 但是希望能帮你理解版本管理的原理. "人肉 ...
- http://blog.csdn.net/baidu_31657889/article/details/52315902
Java技术--你真的了解String类的intern()方法吗 转载 2016年08月25日 16:30:14 标签: java intern / intern / java 技术 6542 0.引 ...
- 学习mongoDB的一些感受(转自:http://blog.csdn.net/liusong0605/article/details/11581019)
曾经使用过mongoDB来保存文件,最一开始,只是想总结一下在开发中如何实现文件与mongoDB之间的交互.在此之前,并没有系统的了解过mongoDB,虽然知道我们用它来存储文件这些非结构化数据,但是 ...
- http://blog.csdn.net/LANGXINLEN/article/details/50421988
GitHub上史上最全的Android开源项目分类汇总 今天在看博客的时候,无意中发现了 @Trinea在GitHub上的一个项目 Android开源项目分类汇总, 由于类容太多了,我没有一个个完整地 ...
- VBS 常用总汇 (http://blog.csdn.net/sgear/article/details/1380223)
VBS 常用总汇 (http://blog.csdn.net/sgear/article/details/1380223) 1 VBS VBS脚本病毒的大量流行使我们对VBS的功能有了一个全新的 ...
最新文章
- java web调用c_Java调用C/C++程序
- 哇赛!我是小龙女啊!
- 小熊派4G cat1模块体验测试报告,AT命令发送短信拨号无脑教程,cat1移动网速测试...
- dart系列之:元世界pubspec.yaml文件详解
- extjs年月日时分选择控件_UI设计|网站公共控件及交互事件
- webbrowser设置横向打印_C# 日常记录:指定打印机/纸张/纸盒(静默打印)(不弹窗打印)WinForm篇...
- CentOS6.5+Python2.7+ GIT +IPython
- 厦门大学计算机学硕复试,【图片】一战厦大计算机上岸,经验帖。慢更【考研吧】_百度贴吧...
- php如何循环输出图片,thinkphp 循环显示图片问题!!!~~~~
- Atitit 算法原理与导论 目录 1. Attilax总结的有用算法 按用途分类	1 1.1. 排序算法 字符串匹配(String Matching)	1 1.2. 加密算法 编码算法 序列
- c语言冒泡排序知识点,c语言冒泡排序
- Navitas GaN IC 驱动 Vivo 可折叠手机
- 盲盒小程序源码下载、附赠完整图片素材源码
- 移动机器人定位方法概述
- Node.js Web 模块(客户端 服务端实例)
- 「DR老兵寻访」游戏正规军的“万智牌生态”链改计划
- c语言编程实现简单的注册登录,C语言编程:实现用户的注册和登录
- 红花玉兰种植要求与养护要点
- 文件下载显示进度条以及调取浏览器下载进程
- 驱动程序(9) 利用Windriver修改代码实现DMA传输定制功能的驱动程序
热门文章
- GB28181实时视频抓图功能如何实现
- 万用表的使用方法,焊接
- 复盘:一份完整的活动运营策划方案是这样的
- 51单片机:流水灯蜂鸣器控制
- Win32:INI文件读写结构体/Int/String/遍历
- 解决php报错Warning: date(): It is not safe to rely on the system's timezone settings
- Jetson AGX Orin刷机教程,奶奶看完都说会了!
- ygomobile卡组下载网站_YGOMobile:用手机也能免费玩游戏王
- java企业级快速开发平台jeeadminlte(jeesite大胖老师修改版)
- 计算机主机显卡安装,安装后如何在计算机中启用独立显卡