Java教程

JAVA多线程 —— 3.生产者消费者问题

本文主要是介绍JAVA多线程 —— 3.生产者消费者问题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

线程的生产者与消费者

生产者生产产品放到一个区域中,之后消费者从此区域里取出产品

这里的区域指的是:共享资源

public class Goods {
    private String pinpai;
    private String name;
    /**
     * true:有商品
     * false:无商品
     */
    private boolean flag = false;

    public String getPinpai() {
        return pinpai;
    }

    public void setPinpai(String pinpai) {
        this.pinpai = pinpai;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public synchronized void get(){
        if(!flag){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("消费者取走了"+this.getPinpai()+"---------"+this.getName());
        this.flag = false;
        notifyAll();
    }

    public synchronized void set(String pinpai,String name){
        if(flag){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.setPinpai(pinpai);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.setName(name);
        System.out.println("生产者生产了"+this.getPinpai()+"---------"+this.getName());
        this.flag = true;
        notifyAll();
    }
}
public class XiaoFei implements Runnable{
    private Goods goods;

    public XiaoFei(Goods goods) {
        this.goods = goods;
    }

    @Override
    public void run() {
        for(int i=0;i<10;i++){
            goods.get();
        }
    }
}
public class ShengChan implements Runnable{
    private Goods goods;

    public ShengChan(Goods goods) {
        this.goods = goods;
    }

    @Override
    public void run() {
        for(int i=0;i<10;i++){
            if (i%2==0){
                goods.set("哇哈哈","矿泉水");
            }else{
                goods.set("旺仔","小馒头");
            }
        }
    }
}

public class Test {
    public static void main(String[] args) {
        Goods goods = new Goods();

        ShengChan shengChan = new ShengChan(goods);
        XiaoFei xiaoFei = new XiaoFei(goods);

        Thread t1 = new Thread(shengChan);
        Thread t2 = new Thread(xiaoFei);

        t1.start();
        t2.start();
    }
}

JUC方式实现生产消费者

JUC----java.util .concurrent工具包的简称。

是一个处理线程的工具包,JDK 1.5开始出现的。

public class Goods {
    private String pinpai;
    private String name;

    public String getPinpai() {
        return pinpai;
    }

    public void setPinpai(String pinpai) {
        this.pinpai = pinpai;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
public class ShengChan implements Runnable{
    private BlockingQueue<Goods> blockingQueue;

    public ShengChan(BlockingQueue blockingQueue) {
        this.blockingQueue = blockingQueue;
    }

    @Override
    public void run() {
        for(int i=0;i<10;i++){
            Goods goods = new Goods();
            if(i%2==0){
                goods.setPinpai("旺仔");
                goods.setName("小馒头");
            }else {
                goods.setPinpai("哇哈哈");
                goods.setName("矿泉水");
            }
            try {
                System.out.println("生产者========"+goods.getPinpai()+goods.getName());
                blockingQueue.put(goods);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
public class XiaoFei implements Runnable{
    private BlockingQueue<Goods> blockingQueue;

    public XiaoFei(BlockingQueue blockingQueue) {
        this.blockingQueue = blockingQueue;
    }

    @Override
    public void run() {
        for(int i=0;i<10;i++){
            try {
                Goods goods = blockingQueue.take();
                System.out.println("消费者消费========"+goods.getPinpai()+goods.getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
public class Test {
    public static void main(String[] args) {
        BlockingQueue<Goods> blockingQueue = new ArrayBlockingQueue<Goods>(5);

        ShengChan shengChan = new ShengChan(blockingQueue);
        XiaoFei xiaoFei = new XiaoFei(blockingQueue);

        Thread t1 = new Thread(shengChan);
        Thread t2 = new Thread(xiaoFei);

        t1.start();
        t2.start();
    }
}
这篇关于JAVA多线程 —— 3.生产者消费者问题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!