生产者与消费者问题
2016-03-18 22:54
337 查看
听说面试会考到生产者消费者问题,今天看了下,根据java的并发程序写了以下代码。
写代码最重要的是要弄清楚,一个类里面到底需要哪些属性以及方法,将这些搞清楚后就会发现代码写起来很流畅。
一. 单缓冲区 的生产者消费者问题
要有:1.仓库 2.消费者 3. 生产者
仓库:由于是单缓冲区,只需要一个boolean类型来判断仓库是否为空即可,以及货物的大小,以及放入和提取方法。
import javax.swing.text.StyledEditorKit.ForegroundAction;
public class CubbyHole {
private int goods;
private boolean empty;
public CubbyHole() {
empty=true; //仓库一开始为空
}
public synchronized int get(int id){
while (empty) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("消费者拿走了货物"+goods);
empty=true;
notify();
return goods;
}
public synchronized void put(int val){
while (!empty) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
goods=val;
System.out.println("生产者"放入了货物"+goods;
empty=false;
notify();
}
}
主程序,图方便生产者和消费者作为内部类写了
public class Main {
public static void main(String[] args){
CubbyHole cubbyHole=new CubbyHole();
Producer producer=new Producer();
producer.start();
Consumer consumer=new Consumer();
consumers.start();
}
public static class Producer extends Thread{
private CubbyHole cubbyHole;
public Producer(CubbyHole c) {
this.cubbyHole=c;
}
public void run(){
for(int i=0;i<50;i++){
cubbyHole.put((int)(100*Math.random()));
}
}
}
public static class Consumer extends Thread{
private CubbyHole cubbyHole;
public Consumer(CubbyHole c) {
this.cubbyHole=c;
}
public void run(){
for(int i=0;i<50;i++){
cubbyHole.get();
}
}
}
二.多缓冲区 的生产者消费者问题
要有:1.仓库 2.消费者 3. 生产者
仓库:由于是多缓冲区,需要一个参数nbuf设置缓冲大小,count记录仓库中的货物数量,head记录生产者每次存放货物的位置
rear记录消费者每次提取货物的位置,一个goods[]数组用来存放货物以及放入和提取方法。
import javax.swing.text.StyledEditorKit.ForegroundAction;
public class CubbyHole {
private int[] goods;
private int nbuf;
private int count;
private int front;
private int rear;
public CubbyHole(int nbuf) {
this.nbuf=nbuf;
goods=new int[nbuf];
count=0;
front=0;
rear=0;
}
public synchronized int get(int id){
while (count<=0) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
front=(front+1)%nbuf;
count--;
notifyAll();
System.out.println("消费者"+id+"拿走了货物"+goods[front]);
return goods[front];
}
public synchronized void put(int val,int id){
while (count>=nbuf) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
rear=(rear+1)%nbuf;
goods[rear]=val;
count++;
notifyAll();
System.out.println("生产者"+id+"放入了货物"+goods[rear]);
}
}
下面是主程序,定义了4个生成者和4个消费者一起执行 (不然不就没意义了,和单缓冲区的一样了)
public class Main {
public static void main(String[] args){
int nbuf=8;
CubbyHole cubbyHole=new CubbyHole(nbuf);
Producer[] producers=new Producer[4];
for(int i=0;i<4;i++){
producers[i]=new Producer(cubbyHole,i+1);
producers[i].start();
}
Consumer[] consumers=new Consumer[4];
for(int i=0;i<4;i++){
consumers[i]=new Consumer(cubbyHole,i+1);
consumers[i].start();
}
}
public static class Producer extends Thread{
private CubbyHole cubbyHole;
private int id;
public Producer(CubbyHole c,int id) {
this.cubbyHole=c;
this.id=id;
}
public void run(){
for(int i=0;i<50;i++){
cubbyHole.put((int)(100*Math.random()),id);
}
}
}
public static class Consumer extends Thread{
private CubbyHole cubbyHole;
private int id;
public Consumer(CubbyHole c,int id) {
this.cubbyHole=c;
this.id=id;
}
public void run(){
for(int i=0;i<50;i++){
cubbyHole.get(id);
}
}
}
}
以上代码挺简单的,就不加什么详细的备注了。以下是多缓冲区生产者消费者问题的运行结果
只是实现了最简单的生产者和消费者问题~哇哈哈,欢迎交流
写代码最重要的是要弄清楚,一个类里面到底需要哪些属性以及方法,将这些搞清楚后就会发现代码写起来很流畅。
一. 单缓冲区 的生产者消费者问题
要有:1.仓库 2.消费者 3. 生产者
仓库:由于是单缓冲区,只需要一个boolean类型来判断仓库是否为空即可,以及货物的大小,以及放入和提取方法。
import javax.swing.text.StyledEditorKit.ForegroundAction;
public class CubbyHole {
private int goods;
private boolean empty;
public CubbyHole() {
empty=true; //仓库一开始为空
}
public synchronized int get(int id){
while (empty) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("消费者拿走了货物"+goods);
empty=true;
notify();
return goods;
}
public synchronized void put(int val){
while (!empty) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
goods=val;
System.out.println("生产者"放入了货物"+goods;
empty=false;
notify();
}
}
主程序,图方便生产者和消费者作为内部类写了
public class Main {
public static void main(String[] args){
CubbyHole cubbyHole=new CubbyHole();
Producer producer=new Producer();
producer.start();
Consumer consumer=new Consumer();
consumers.start();
}
public static class Producer extends Thread{
private CubbyHole cubbyHole;
public Producer(CubbyHole c) {
this.cubbyHole=c;
}
public void run(){
for(int i=0;i<50;i++){
cubbyHole.put((int)(100*Math.random()));
}
}
}
public static class Consumer extends Thread{
private CubbyHole cubbyHole;
public Consumer(CubbyHole c) {
this.cubbyHole=c;
}
public void run(){
for(int i=0;i<50;i++){
cubbyHole.get();
}
}
}
二.多缓冲区 的生产者消费者问题
要有:1.仓库 2.消费者 3. 生产者
仓库:由于是多缓冲区,需要一个参数nbuf设置缓冲大小,count记录仓库中的货物数量,head记录生产者每次存放货物的位置
rear记录消费者每次提取货物的位置,一个goods[]数组用来存放货物以及放入和提取方法。
import javax.swing.text.StyledEditorKit.ForegroundAction;
public class CubbyHole {
private int[] goods;
private int nbuf;
private int count;
private int front;
private int rear;
public CubbyHole(int nbuf) {
this.nbuf=nbuf;
goods=new int[nbuf];
count=0;
front=0;
rear=0;
}
public synchronized int get(int id){
while (count<=0) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
front=(front+1)%nbuf;
count--;
notifyAll();
System.out.println("消费者"+id+"拿走了货物"+goods[front]);
return goods[front];
}
public synchronized void put(int val,int id){
while (count>=nbuf) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
rear=(rear+1)%nbuf;
goods[rear]=val;
count++;
notifyAll();
System.out.println("生产者"+id+"放入了货物"+goods[rear]);
}
}
下面是主程序,定义了4个生成者和4个消费者一起执行 (不然不就没意义了,和单缓冲区的一样了)
public class Main {
public static void main(String[] args){
int nbuf=8;
CubbyHole cubbyHole=new CubbyHole(nbuf);
Producer[] producers=new Producer[4];
for(int i=0;i<4;i++){
producers[i]=new Producer(cubbyHole,i+1);
producers[i].start();
}
Consumer[] consumers=new Consumer[4];
for(int i=0;i<4;i++){
consumers[i]=new Consumer(cubbyHole,i+1);
consumers[i].start();
}
}
public static class Producer extends Thread{
private CubbyHole cubbyHole;
private int id;
public Producer(CubbyHole c,int id) {
this.cubbyHole=c;
this.id=id;
}
public void run(){
for(int i=0;i<50;i++){
cubbyHole.put((int)(100*Math.random()),id);
}
}
}
public static class Consumer extends Thread{
private CubbyHole cubbyHole;
private int id;
public Consumer(CubbyHole c,int id) {
this.cubbyHole=c;
this.id=id;
}
public void run(){
for(int i=0;i<50;i++){
cubbyHole.get(id);
}
}
}
}
以上代码挺简单的,就不加什么详细的备注了。以下是多缓冲区生产者消费者问题的运行结果
只是实现了最简单的生产者和消费者问题~哇哈哈,欢迎交流
相关文章推荐
- 绑定服务调用远程服务中的方法
- bzoj 2038: [2009国家集训队]小Z的袜子(hose)
- You and Your Research
- iOS各版本特性
- Android四种跨进程通信
- 【C++】知识回顾1
- 二级联动列表
- MUI-基础01
- NanoPC-T2 uboot分析(1)
- 关于openStream方法和openConnection方法的区别
- string 转 int,int 转 string
- AlphaGo 4:1 李世石
- iOS开发——Swift & Xcode
- C语言类型提升
- nsoj A. Oil Deposits
- Setting Up WS2016 Storage Spaces Direct SOFS
- 汇编与高级语言(插图结合Delphi代码,来自linzhengqun)
- JUnit 3.8 演示递归删除文件目录的 测试类程序 .
- IOS NavigitionController导航栏的基本设置
- txt中的内容复制到excel中无法对齐, 该怎么处理?