消费者和生产者问题的实现-基于线程安全的容器来和非线程安全的容器类
2017-10-27 20:36
525 查看
生产者和消费者的实现的大概思路
缓冲区的接口import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; /** * All rights reserved. * Created by zhaideyin on 2017/10/27. * Description: */ public interface Plate { final int PLATE_SIZE=10; ReentrantLock reentrantLock=new ReentrantLock(); Condition notEmpty=reentrantLock.newCondition(); Condition notFull=reentrantLock.newCondition(); void takeApple(); void putApple(); }
非线程安全的容器类,以list为例
import java.util.ArrayList; import java.util.List; /** * All rights reserved. * Created by zhaideyin on 2017/10/27. * Description: */ public class NoSafePlate implements Plate { List<Integer> plateList=new ArrayList<Integer>(); @Override public void takeApple(){ reentrantLock.lock(); if(plateList.size()==0){ try { System.out.println(Thread.currentThread()+"other consumer wait"); notEmpty.await();//阻塞消费进程 } catch (InterruptedException e) { e.printStackTrace(); } }else { plateList.remove(0); System.out.println(Thread.currentThread()+"consumer :"+plateList.size()+" apple"); notFull.signalAll();//通知生产线程可以放苹果 } reentrantLock.unlock(); } @Override public void putApple(){ reentrantLock.lock(); if(plateList.size()==PLATE_SIZE){ try { System.out.println(Thread.currentThread()+"other producer wait"); notFull.await(); } catch (InterruptedException e) { e.printStackTrace(); } }else { plateList.add(1); System.out.println(Thread.currentThread()+" producer :"+plateList.size()+" apple"); notEmpty.signalAll(); } reentrantLock.unlock(); } }
线程安全的容器类
import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * All rights reserved. * Created by zhaideyin on 2017/10/27. * Description: */ public class SafePlate implements Plate{ //一下的方法虽然生成的list是线程安全的list,也只是仅仅局限于原子操作的方法,在多线程的条件下,这个list也有可能会被修改 List<Integer> plateList= Collections.synchronizedList(new ArrayList<Integer>()); @Override public void takeApple(){ synchronized (plateList) { while (plateList.size()==0){ System.out.println(Thread.currentThread()+"consumer wait"); try { plateList.wait(); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } plateList.remove(0); System.out.println(Thread.currentThread() + "consumer : " + plateList.size()); plateList.notifyAll(); } } @Override public void putApple(){ synchronized (plateList) { while (plateList.size()==PLATE_SIZE){ System.out.println(Thread.currentThread()+"producer wait"); try { plateList.wait(); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } plateList.add(1); System.out.println(Thread.currentThread() + "producer : " + plateList.size()); plateList.notifyAll(); } } }
生产者
import java.util.Random; /** * All rights reserved. * Created by zhaideyin on 2017/10/27. * Description: */ public class Producer implements Runnable { private Plate plate; Random r=new Random(10); public Producer(NoSafePlate noSafePlate) { this.plate = noSafePlate; } public Producer(SafePlate safePlate) { this.plate = safePlate; } @Override public void run() { while (true) { plate.putApple(); } } }
消费者
/** * All rights reserved. * Created by zhaideyin on 2017/10/27. * Description: */ public class Consumer implements Runnable { private Plate plate; public Consumer(NoSafePlate noSafePlate) { this.plate = noSafePlate; } public Consumer(SafePlate safePlate) { this.plate = safePlate; } @Override public void run() { //消费者先等待一段时间,等待生产者生产了部分之后再开始消费 try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } while (true) { plate.takeApple(); } } }
容器安全的测试类
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * Copyright 2010-2020 OPPO Mobile Comm Corp., Ltd. * All rights reserved. * Created by zhaideyin on 2017/10/27. * Description: */ public class SafePlateTest { public static void main(String[] args) throws InterruptedException { SafePlate safePlate=new SafePlate(); ExecutorService service= Executors.newCachedThreadPool(); for(int i=0;i<5;i++){ service.submit(new Producer(safePlate)); } for(int i=0;i<5;i++){ service.submit(new Consumer(safePlate)); } Thread.sleep(10*1000); service.shutdown(); } }
运行结果
线程不安全的容器类的测试类
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ConsumerAndProducerTest { public static void main(String[] args) throws InterruptedException{ NoSafePlate noSafePlate =new NoSafePlate(); ExecutorService service= Executors.newCachedThreadPool(); for(int i=0;i<5;i++){ service.submit(new Producer(noSafePlate)); } for(int i=0;i<5;i++){ service.submit(new Consumer(noSafePlate)); } Thread.sleep(10*1000); service.shutdown(); } }
运行结果
相关文章推荐
- 实现一个线程安全的队列,并模拟进行生产者-消费者问题
- 用vc实现生产者消费者问题(转载)
- 经典线程同步问题(生产者&消费者)--Java实现
- 生产者与消费者问题是典型的同步问题。这里简单介绍两种不同的实现方法。
- 生产者-消费者问题【Java实现】
- 用JAVA实现多线程(生产者与消费者问题)
- 生产者-消费者问题(condition)Java实现
- 生产者消费者问题的Java实现
- 生产者/消费者问题的多种Java实现方式
- 用JAVA 实现“生产者-消费者”问题
- 用vc实现生产者消费者问题
- linux C语言,PV实现生产者消费者问题
- 用vc实现生产者消费者问题
- 基于线程实现的生产者消费者模型(Object.wait(),Object.notify()方法)
- Java 线程同步问题 生产者-消费者 算法实现 -Java学习笔记(29)
- .net实现--多线程之生产者/消费者问题
- linux 下 条件变量实现生产者消费者问题
- 生产者/消费者问题在windows2000下的实现
- 生产者与消费者问题(java多线程实现)
- 生产者-消费者问题实现 (linux下C语言)