用 wait-notify 写一段代码来解决生产者-消费者问题?
2016-12-13 15:48
489 查看
一、生产者代码:
二、消费者代码:
三、测试函数:
package com.edu.chapter03.test; import java.util.Vector; import java.util.logging.Level; import java.util.logging.Logger; public class Producer implements Runnable { private final Vector sharedQueue; private final int SIZE; public Producer(Vector sharedQueue, int size) { this.sharedQueue = sharedQueue; this.SIZE = size; } @Override public void run() { // TODO Auto-generated method stub for (int i = 0; i < 7; i++) { System.out.println("Produced:" + i); try { produce(i); } catch (InterruptedException ex) { Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex); } } } private void produce(int i) throws InterruptedException { //wait if queue is full while (sharedQueue.size() == SIZE) { synchronized (sharedQueue) { System.out.println("Queue is full " + Thread.currentThread().getName() + " is waiting , size: " + sharedQueue.size()); sharedQueue.wait(); } } //producing element and notify consumers synchronized (sharedQueue) { sharedQueue.add(i); sharedQueue.notifyAll(); } } }
二、消费者代码:
package com.edu.chapter03.test; import java.util.Vector; import java.util.logging.Level; import java.util.logging.Logger; public class Consumer implements Runnable { private final Vector sharedQueue; private final int SIZE; public Consumer(Vector sharedQueue, int size) { this.sharedQueue = sharedQueue; this.SIZE = size; } @Override public void run() { // TODO Auto-generated method stub while (true) { try { System.out.println("Consumer: " + consume()); Thread.sleep(50); } catch (InterruptedException ex) { Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex); } } } private int consume() throws InterruptedException { //wait if queue is empty while (sharedQueue.isEmpty()) { synchronized (sharedQueue) { System.out.println("Queue is empty " + Thread.currentThread().getName() + " is waiting , size: " + sharedQueue.size()); sharedQueue.wait(); } } //otherwise consume element and notify waiting producer synchronized (sharedQueue) { sharedQueue.notifyAll(); return (Integer) sharedQueue.remove(0); } } }
三、测试函数:
package com.edu.chapter03.test; import java.util.Vector; public class ProducerConsumerSolution { public static void main(String[] args) { Vector sharedQueue = new Vector(); int size = 4; Thread prodThread = new Thread(new Producer(sharedQueue, size), "Producer"); Thread consThread = new Thread(new Consumer(sharedQueue, size), "Consumer"); prodThread.start(); consThread.start(); } }
相关文章推荐
- 面试题:用 wait-notify 写一段代码来解决生产者-消费者问题
- linux下生产者与消费者问题代码,以及编译c代码时error:undefined reference to sem_wait 解决方法之一
- C++ 用critical_section 代码临界区模拟信号量,解决生产者消费者的问题
- 使用wait和notify实现经典生产者消费者问题
- 线程学习二,notify和wait实现消费者-生产者同步问题
- 若还 不懂 java synchronized 同步,锁,对象wait,notify问题,看完这两段代码,解决问题。
- 多线程-生产者消费者问题代码2并解决线程安全问题
- 多线程--生产者消费者问题(wait和notify方法)
- 利用wait()和notify()实现生产者与消费者问题
- wait()、notify()实现生产者、消费者问题
- 利用wait()和notify()实现生产者与消费者问题
- java线程的wait方法和notify方法的运用(包含生产者消费者代码【已练习】)
- 多线程五,线程间通信3,wait、notify,notifyAll方法,生产者和消费者问题(14,毕向东老师)
- java多线程-线程间通信-示例代码-解决安全问题-等待唤醒机制wait()notify()notifyAll()
- Java 多线程实现生产者消费者问题(wait/notify)
- 浅谈生产者消费者问题中的wait()和notify()的使用
- 使用Notify 和 wait ,使用Linklist实现生产者消费者问题
- 多线程程序一段问题代码分析(生产者消费者)
- 多线程_生产者消费者题代码2并解决线程安全问题
- 【Java】线程wait() notify()通信 实现生产者 消费者问题