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
原创粉丝点击