producer and consumer
来源:互联网 发布:作品集目录设计 知乎 编辑:程序博客网 时间:2024/05/22 17:08
1. 使用JUC包下的BlockingQueue实现,no worry using synchronized
当空的时候就不拿,直到有东西,当满是就不放,直到有空位
import java.util.Random;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;/* * producer and consumer */public class App {private static BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(10);public static void main (String[] args) throws InterruptedException {Thread t1 = new Thread(new Runnable(){public void run() {try {producer();} catch (InterruptedException e) {e.printStackTrace();}}});Thread t2 = new Thread(new Runnable(){public void run() {try {consumer();} catch (InterruptedException e) {e.printStackTrace();}}});t1.start();t2.start();t1.join();t2.join();}private static void producer() throws InterruptedException {Random random = new Random();while(true) {queue.put(random.nextInt(100));}}private static void consumer() throws InterruptedException {Random random = new Random();while(true) {Thread.sleep(100);if(random.nextInt(10) == 0) {int value = queue.take();System.out.println("Taken value:" + value + ";Queue size is:" + queue.size());}}}}
2. wait & notify
2.1基础知识
import java.util.Scanner;public class Processor {public void producer() throws InterruptedException {synchronized (this) {System.out.println("producer thread running ... ");wait();// wait只能在synchronized里面System.out.println("Resumed");}}public void consumer() throws InterruptedException {Scanner sc = new Scanner(System.in);Thread.sleep(2000);synchronized (this) {System.out.println("waiting for return key.");sc.nextLine();System.out.println("return key pressed.");notify();// wait只能在synchronized里面Thread.sleep(5000);// 即使是按了回车,也不会立即唤醒producer}}}
/* * wait and notify */public class App {public static void main(String[] args) throws InterruptedException {final Processor processor = new Processor();Thread t1 = new Thread(new Runnable(){public void run() {try {processor.producer();} catch (InterruptedException e) {e.printStackTrace();}}});Thread t2 = new Thread(new Runnable(){public void run() {try {processor.consumer();} catch (InterruptedException e) {e.printStackTrace();}}});t1.start();t2.start();t1.join();t2.join();}}
在notify之后,加上Thread.sleep(5000);即使是按了回车,也不会立即唤醒producer,因为producer需要的是锁,而不是被notify就能运行
2.2 produce-cousmer using wait & notify lower-level thread synchronization
import java.util.LinkedList;import java.util.Random;public class Processor {private LinkedList<Integer> list = new LinkedList<Integer>();private final int LIMIT = 10;private Object lock = new Object();public void producer() throws InterruptedException {int value = 0;while(true) {synchronized (lock) {while(list.size() == LIMIT)lock.wait();list.add(value++);lock.notify();}}}public void consumer() throws InterruptedException {while(true) {synchronized (lock) {while(list.size() == 0)lock.wait();System.out.print("List size is:" + list.size());int value = list.removeFirst();System.out.println("; value is " + value);lock.notify();}Thread.sleep(new Random().nextInt(100));}}}
注意在producer里面wait了,在consumer里面就要notify
0 0
- python producer and consumer
- producer and consumer
- JAVA Producer and Consumer
- camel producer and consumer templates
- Producer Consumer problem - mutex and semaphore
- MULTITHREADING - PRODUCER AND CONSUMER WITH QUEUE
- Kafka基本知识一 Producer and Consumer
- producer consumer
- producer & consumer
- Fast synchronization between a single producer and single consumer
- Java Lock and Condition Example using Producer Consumer Solution
- The Producer-Consumer Problem
- compile producer-consumer
- ProtoThreads - Producer&Consumer
- ACE_Task Consumer-Producer
- Producer-consumer problem
- Producer Consumer 模式
- Producer-Consumer Pattern
- 带标签的continue#Math.random()
- Oracle_ORA-01034: ORACLE not available
- 在活体检测的程序上把检测到的活体显示在右上角
- ActiveMQ_页面上显示队列的信息
- linux基础练习(七,八)
- producer and consumer
- Oracle_画图解释 SQL join 语句
- Oracle_查询当天记录 三种方法效率比较
- 四则运算
- 【问题解决】BootStrap.css与layDate日期选择样式起冲突的解决办法
- Java_assertion
- LabVIEW中的While循环应用示例
- Test_写给精明Java开发者的测试技巧
- 很详细的解答tree of tree 树状DP(有图)