手写生产者消费者模式

手写了一种生产者消费者模式,调用了sychronized,wait,notifyall函数

 

package proAndCsmModel01;

 

import java.util.LinkedList;

 

/**

 * 实现缓冲区

 *

 */

public class Resource01 {

    //最大缓冲区

    private final int MAX_SIZE = 10;

    //缓冲区队列

    LinkedList<datatype> list = new LinkedList<>();

 

    /**

     * 生产资料同步方法

     */

    public  synchronized void increaseData(){

            while (list.size() >= MAX_SIZE){

                try {

                    System.out.println(Thread.currentThread().getId()+"资料仓库已满!");

                    wait();

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

            }

            datatype d = new datatype();

            d.setData((int) (Math.random()*1000));

            list.add(d);

            System.out.println(Thread.currentThread().getId()+"生产:"+d.getData()+" 库存量:"+list.size());

            notifyAll();

 

    }

 

    /**

     * 消费资料同步方法

     */

    public synchronized void decreaseData(){

            while (list.size() <= 0){

                try {

                    System.out.println(Thread.currentThread().getId()+"资料仓库为空!");

                    wait();

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

            }

            datatype d = list.poll();

            System.out.println(Thread.currentThread().getId()+"消费:"+d.getData()+" 库存量:"+list.size());

            notifyAll();

    }

}

 

package proAndCsmModel01;

 

/**

 * 生产者:生产资料

 *

 */

public class producer01 implements Runnable {

    private Resource01 resource01;

    producer01(Resource01 resource01){

        this.resource01 = resource01;

    }

    @Override

    public void run() {

        while (true){

            try {

                //随机休眠后在生产资料

                Thread.sleep((long) (Math.random()*1000));

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

            //调用同步方法进行生产资料

            resource01.increaseData();

        }

    }

}

package proAndCsmModel01;

 

/**

 * 消费者:消费资料

 *

 */

public class consumer01 implements Runnable {

    private Resource01 resource01;

    consumer01(Resource01 resource01){

        this.resource01 = resource01;

    }

    @Override

    public void run() {

         while (true){

             try {

                 //随机休眠后再消费

                 Thread.sleep((long) (Math.random()*1000));

             } catch (InterruptedException e) {

                 e.printStackTrace();

             }

             //调用同步方法进行消费资料

             resource01.decreaseData();

         }

    }

}

package proAndCsmModel01;

 

/**

 * 基本数据类型

 *

 */

public class datatype {

    private int data;

 

    public void setData(int data) {

        this.data = data;

    }

 

    public int getData() {

        return data;

    }

}

package proAndCsmModel01;

 

/**

 * 调用

 *

 */

public class test {

    public static void main(String agrs[]){

        Resource01 resource01 = new Resource01();;

        System.out.println(Thread.currentThread().getName());

 

        new Thread(new producer01(resource01)).start();

        new Thread(new producer01(resource01)).start();

        new Thread(new producer01(resource01)).start();

        new Thread(new producer01(resource01)).start();

        new Thread(new producer01(resource01)).start();

        new Thread(new producer01(resource01)).start();

 

        new Thread(new consumer01(resource01)).start();

        new Thread(new consumer01(resource01)).start();

        new Thread(new consumer01(resource01)).start();

 

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值