Java生产者消费者模式

一,synchronized

package com;

class Clerk{
    private int proNum;
    public synchronized void proProduct(){
        if(proNum<20){
            System.out.println("生产者生产一个产品,剩余产品数量:" + ++proNum);
            notify();
        }else{
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public synchronized void conProduct(){
        if(proNum>0){
            System.out.println("消费者消费一个产品,剩余产品数量:" + --proNum);
            notify();
        }else{
            System.out.println("等待");
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

class Produer extends Thread{
    Clerk c;
    public Produer(Clerk c){
        this.c = c;
    }
    public void run(){
        while(true){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            c.proProduct();
        }
    }
}
class Consumer extends Thread{
    Clerk c;
    public Consumer(Clerk c){
        this.c = c;
    }
    public void run(){
        while(true){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            c.conProduct();
        }
    }
}
public class ProAndCon {
    public static void main(String[] args) {
        Clerk c = new Clerk();
        Produer produer = new Produer(c);
        Consumer consumer = new Consumer(c);
        produer.start();
        consumer.start();
        new Produer(c).start();
        new Consumer(c).start();
    }
}

二,阻塞队列

package concurrent;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

class Resoures{
    private volatile boolean flag = true;
    AtomicInteger number = new AtomicInteger();
    private BlockingQueue<String> queue = null;

    public Resoures(BlockingQueue<String> queue) {
        this.queue = queue;
    }
    public void stop(){
        flag = false;
    }
    public void produce() throws InterruptedException {
        String str = null;
        boolean res;
        while(flag){
            str = number.incrementAndGet()+"";
            res = queue.offer(str,2l, TimeUnit.SECONDS);
            if(res){
                System.out.println(Thread.currentThread().getName() + "生产产品成功,产品:" + str);
            }else{
                System.out.println(Thread.currentThread().getName() + "生产产品失败,产品:" + str);
            }
            Thread.sleep(1000);
        }
        System.out.println("工厂停止运转");
    }
    public void consume() throws InterruptedException {
        String res;
        while(true){
             res = queue.poll(2l, TimeUnit.SECONDS);
            if(res == null || res.equalsIgnoreCase("")){
                System.out.println(Thread.currentThread().getName() + "消费产品失败,停止消费线程");
                return;
            }else{
                System.out.println(Thread.currentThread().getName() + "消费产品成功,产品:" + res);
            }
            Thread.sleep(1000);
        }
    }
}
public class ProducterAndConsumer {
    public static void main(String[] args) {
        System.out.println(Runtime.getRuntime().availableProcessors());
        ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(10);
        Resoures resoures = new Resoures(queue);
        new Thread(()->{
            try {
                resoures.consume();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"消费者").start();
        new Thread(()->{
            try {
                resoures.produce();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"生产者").start();



        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        resoures.stop();
    }
}

你可能感兴趣的:(Java,java,多线程)