本文主要是介绍生产者消费者模式sychronized实现 java,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
生产者消费者模式sychronized实现 java
相信大家都对消费者和生产者模式有一定了解,这个场景经常会用到多线程,而且因为涉及到共享资源的获取和修改,必然是需要线程同步的,那这边我就用synchronized来试下消费者和生产者,希望大家能看明白,程序中有注释,基本上能看懂的,主要是对共享对象buffer中的两个方法进行同步
代码:
import java.util.Date;
import java.util.LinkedList;
import java.util.List;//生产者消费者模式
public class Producer_Consumer {public static void main(String[] args) {Buffer buffer = new Buffer(10);Producer producer = new Producer(buffer);Consumer consumer = new Consumer(buffer);//创建线程执行生产和消费for(int i = 0; i < 3; i++) {new Thread(producer,"Producer-"+i).start();}
for(int i = 0; i < 3; i++) {
new Thread(consumer,"Consumer-"+(i+3)).start();
}}
}//模拟生产者消费者的对象
class Buffer {private int maxSize;private List<Date> storage;Buffer(int size) {maxSize = size;storage = new LinkedList<>();}//生产方法public synchronized void put () {try {while (storage.size() == maxSize) {//如果队列满了System.out.println(Thread.currentThread().getName()+": wait \n");wait();//阻塞线程}storage.add(new Date());System.out.println(Thread.currentThread().getName()+": put:"+storage.size()+"\n");Thread.sleep(1000);notifyAll();//唤起线程} catch (InterruptedException e) {e.printStackTrace();}}//消费方法public synchronized void take () {try {while (storage.size() == 0) {//如果队列空了System.out.println(Thread.currentThread().getName()+": wait \n");wait();//阻塞线程}Date d = ((LinkedList<Date>)storage).poll();System.out.println(Thread.currentThread().getName()+": take:"+storage.size()+"\n");Thread.sleep(1000);notifyAll();//唤起线程} catch (InterruptedException e) {e.printStackTrace();}}
}//生产者
class Producer implements Runnable {private Buffer buffer;Producer(Buffer b) {buffer = b;}@Overridepublic void run() {while(true) {buffer.put();}}
}//消费者
class Consumer implements Runnable {private Buffer buffer;Consumer(Buffer b) {buffer = b;}@Overridepublic void run () {while (true) {buffer.take();}}
}
输出结果:
Producer-0: put:1Producer-0: put:2Producer-0: put:3Producer-0: put:4Producer-0: put:5Producer-0: put:6Producer-0: put:7Producer-0: put:8Producer-0: put:9Producer-0: put:10Producer-0: wait Consumer-5: take:9Consumer-5: take:8Consumer-5: take:7Consumer-5: take:6Consumer-5: take:5Consumer-5: take:4Consumer-5: take:3Consumer-5: take:2Consumer-5: take:1Consumer-5: take:0Consumer-5: wait Consumer-4: wait Consumer-3: wait Producer-2: put:1Producer-2: put:2Producer-2: put:3Producer-2: put:4Producer-2: put:5Producer-2: put:6Producer-2: put:7Producer-2: put:8Producer-2: put:9Producer-2: put:10Producer-2: wait Producer-1: wait Consumer-3: take:9Consumer-3: take:8Consumer-3: take:7Consumer-3: take:6Consumer-3: take:5Consumer-3: take:4Consumer-3: take:3Consumer-3: take:2Consumer-3: take:1Consumer-3: take:0Consumer-3: wait Consumer-4: wait Consumer-5: wait Producer-0: put:1Producer-0: put:2Producer-0: put:3Consumer-5: take:2Consumer-5: take:1Consumer-5: take:0Consumer-5: wait Consumer-4: wait Consumer-3: wait Producer-1: put:1Producer-1: put:2Producer-1: put:3Producer-1: put:4
这篇关于生产者消费者模式sychronized实现 java的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!