Java 多线程 生产者消费者问题

 1 package producer;
 2
 3 public class SyncStack {
 4     int index =0;
 5     SteamedBun[] bunArr = new SteamedBun[6];        //栈里只能放6个元素
 6
 7     public synchronized void push(SteamedBun bun)
 8     {
 9         while(index >= bunArr.length)                //栈满等待
10         {
11             bun.setIndex(bunArr.length -1);            //修正馒头编号
12             try {
13                 this.wait();
14             } catch (InterruptedException e) {
15                 // TODO Auto-generated catch block
16                 e.printStackTrace();
17             }
18         }
19         //this.notify();                            //单个线程时调用
20         this.notifyAll();                            //广播给所有大厨
21
22         bunArr[index] = bun;
23         index++;
24     }
25
26     public synchronized SteamedBun pop()
27     {
28         while(index ==0)                            //栈空,没有馒头时等待生产
29         {
30             try {
31                 this.wait();
32             } catch (InterruptedException e) {
33                 // TODO Auto-generated catch block
34                 e.printStackTrace();
35             }
36         }
37         this.notify();                                //通知吃货有馒头了
38
39         index--;
40         return bunArr[index];
41     }
42
43
44 }
 1 package producer;
 2
 3 public class Producer implements Runnable{
 4     SyncStack ss = null;
 5     int pId;
 6     public Producer(SyncStack ss,int pId) {
 7         this.ss = ss;
 8         this.pId = pId;
 9     }
10
11     @Override
12     public void run() {
13         // TODO Auto-generated method stub
14         for(int i=0;i<10;i++)
15         {
16             SteamedBun bun = new SteamedBun(ss.index,pId*1000+i);
17             ss.push(bun);
18             System.out.println("****做了"+bun);
19
20             try {
21                 int iRandom = (int)(Math.random()*2000);
22                 Thread.sleep(iRandom);
23             } catch (InterruptedException e) {
24                 // TODO Auto-generated catch block
25                 e.printStackTrace();
26             }
27         }
28     }
29
30 }
 1 package producer;
 2
 3 public class Consumer implements Runnable{
 4
 5     SyncStack ss = null;
 6     public Consumer(SyncStack ss)
 7     {
 8         this.ss = ss;
 9     }
10
11     @Override
12     public void run() {
13         // TODO Auto-generated method stub
14         for(int i= 0;i<30;i++)
15         {
16             SteamedBun bun = ss.pop();
17             System.out.println("吃了 "+bun);
18
19             try {
20                 Thread.sleep((int)(Math.random()* 1000));
21             } catch (InterruptedException e) {
22                 // TODO Auto-generated catch block
23                 e.printStackTrace();
24             }
25         }
26     }
27
28 }
 1 package producer;
 2
 3 public class SteamedBun {
 4     private int id;
 5     private int index;
 6     public SteamedBun(int index,int id) {
 7         this.id = id;
 8         this.index = index;
 9     }
10
11     public String toString()
12     {
13         return "一个馒头,是筐里的第" + (index+1) + "个馒头,编号"+id;
14     }
15
16     public void setIndex(int index) {
17         this.index = index;
18     }
19 }
 1 package producer;
 2
 3 public class Test {
 4     public static void main(String[] args) {
 5         SyncStack ss = new SyncStack();
 6
 7         //new 3个大厨和1个吃货的线程
 8         new Thread(new Producer(ss,1)).start();
 9         new Thread(new Producer(ss,2)).start();
10         new Thread(new Producer(ss,3)).start();
11
12         Consumer consumer = new Consumer(ss);
13         new Thread(consumer).start();
14     }
15 }

时间: 2024-11-07 08:38:01

Java 多线程 生产者消费者问题的相关文章

java多线程 生产者消费者模式

package de.bvb; /** * 生产者消费者模式 * 通过 wait() 和 notify() 通信方法实现 * */ public class Test1 { public static void main(String[] args) { Godown godown = new Godown(50); for (int i = 0; i < 5; i++) { new ProducerThread(i * 10, godown).start(); new ConsumerThre

java 多线程-生产者消费者模式-管程法

生产者消费者模式管程法通过容器中介,将数据放入和取出 wait()导致当前线程等待,直到另一个线程调用该对象的notify()或notyfyAll()方法notify()唤醒正在等待对象监视器的单个线程,notifyAll()唤醒正在等待对象监视器的所有线程 public class tuble { public static void main(String[]args) { SynContainer container=new SynContainer(); new Productor(co

java 多线程生产者消费者

class Res { private String name; private int count = 1; private boolean flag; public synchronized void set(String name) { while (flag) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.name = name + "--" + cou

java多线程 生产者消费者案例-虚假唤醒

package com.java.juc; public class TestProductAndConsumer { public static void main(String[] args) { Clerk clerk = new Clerk(); Produce produce = new Produce(clerk); Consumer consumer = new Consumer(clerk); new Thread(produce, "线程A").start(); ne

[JAVA 多线程] 生产者消费者实例

正好有人问,就直接将代码记录下来. 背景:有一个仓库存储货物,存在着生产者和消费者,设计一个可以并发的实现. 设计思路:设计一个仓库类,类中保存最大的容量限制和当前的count,类中包含生产和消费的方法,并且都是synchronized. 具体代码: package com.test.tiny; public class Store { private final int MAX_SIZE; //最大 private int count; // 当前 public Store(int n) {

Java多线程-生产者/消费者模式实现

单生产者与单消费者 示例: public class ProduceConsume { public static void main(String[] args) { String lock = new String(""); Produce produce = new Produce(lock); Consume consume = new Consume(lock); new Thread(() -> { while (true) { produce.setValue();

java多线程生产者消费者

//Java Thread producer customer class ThreadTest { public static void main(String[] args) { Q q=new Q(); Producer p=new Producer(q); Customer c=new Customer(q); Thread t0=new Thread(p); Thread t1=new Thread(c); t0.start(); t1.start(); for(int i=0;i<5

Java多线程--生产者与消费者问题

说明 Java中,线程之间的通信主要是由java.lang.Object类提供的wait.notify和notifyAll这3个方法来完成: ①对象的wait方法被调用后,线程进入对象的等待队列中,并释放对象锁,其它线程可以竞争使用此对象锁:sleep方法使得一个线程进入睡眠状态,但是线程所占有的资源并没有释放. ②当对象的notify方法被调用,该方法会从对象的等待队列中随机取出一个线程来唤醒:notifyAll是唤醒等待队列中所有线程,这些线程会与其它正在执行的线程共同竞争对象锁. ③wai

java多线程 生产消费者模型

[seriesposts sid=500] 下面的代码讲述了一个故事 一个面包生产铺里目前有30个面包,有三个人来买面包,第一个人要买50个,第二个要买20个,第三个要买30个. 第一个人不够,所以等着,让第二个买了.面包铺继续生产面包.有7个人在生产. package com.javaer.thread; public class CPMode { public static void main(String[] args) { Godown godown = new Godown(30);