手写生产者消费者模式

手写了一种生产者消费者模式,调用了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();

 

    }

}

猜你喜欢

转载自blog.csdn.net/qq_33391981/article/details/91044693