Handwriting producer-consumer model

Handwritten a producer-consumer model, called sychronized, wait, notifyall function

 

package proAndCsmModel01;

 

import java.util.LinkedList;

 

/**

 * Realization buffer

 *

 */

public class Resource01 {

    // maximum buffer

    private final int MAX_SIZE = 10;

    // buffer queue

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

 

    /**

     * Synchronization method of production

     */

    public  synchronized void increaseData(){

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

                try {

                    System.out.println (. Thread.currentThread () getId () + "data warehouse is full!");

                    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();

 

    }

 

    /**

     * Consumption data synchronization method

     */

    public synchronized void decreaseData(){

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

                try {

                    System.out.println (. Thread.currentThread () getId () + "data warehouse is empty!");

                    wait();

                } catch (InterruptedException e) {

                    e.printStackTrace ();

                }

            }

            datatype d = list.poll();

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

            notifyAll();

    }

}

 

package proAndCsmModel01;

 

/**

 * Manufacturer: production

 *

 */

public class producer01 implements Runnable {

    private Resource01 resource01;

    producer01(Resource01 resource01){

        this.resource01 = resource01;

    }

    @Override

    public void run() {

        while (true){

            try {

                // sleep at random after production

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

            } catch (InterruptedException e) {

                e.printStackTrace ();

            }

            // Call the synchronization method of production

            resource01.increaseData();

        }

    }

}

package proAndCsmModel01;

 

/**

 * Consumer: Consumer Information

 *

 */

public class consumer01 implements Runnable {

    private Resource01 resource01;

    consumer01(Resource01 resource01){

        this.resource01 = resource01;

    }

    @Override

    public void run() {

         while (true){

             try {

                 // random sleep after consumption

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

             } catch (InterruptedException e) {

                 e.printStackTrace ();

             }

             // Call the synchronization method of consumption

             resource01.decreaseData();

         }

    }

}

package proAndCsmModel01;

 

/**

 * Basic data types

 *

 */

public class datatype {

    private int data;

 

    public void setData(int data) {

        this.data = data;

    }

 

    public int getData() {

        return data;

    }

}

package proAndCsmModel01;

 

/**

 * transfer

 *

 */

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();

 

    }

}

Guess you like

Origin blog.csdn.net/qq_33391981/article/details/91044693