其他分享
首页 > 其他分享> > 二十五、生产者消费者问题

二十五、生产者消费者问题

作者:互联网

线程通信:

方法名 作用
wait() 表示线程一直等待﹐直到其他线程通知,与sleep不同,会释放锁
wait(long timeout) 指定等待的毫秒数
notify() 唤醒一个处于等待状态的线程
notifyAll() 唤醒同一个对象上所有调用wait()方法的线程﹐优先级别高的线程优先调度

 

        

        

public class ProducersAndConsumers {
    public static void main(String[] args) {

        SynContainer synContainer = new SynContainer();

        Producers p = new Producers(synContainer);
        Consumers c = new Consumers(synContainer);

        new Thread(p).start();
        new Thread(c).start();
    }
}


//生产者
class Producers implements Runnable {

    SynContainer synContainer;

    public Producers(SynContainer synContainer) {
        this.synContainer = synContainer;
    }

    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {
            try {
                synContainer.push(i);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

//消费者
class Consumers implements Runnable {

    SynContainer synContainer;

    public Consumers(SynContainer synContainer) {
        this.synContainer = synContainer;
    }

    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {
            try {
                synContainer.pop(i);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

//产品
class Chicken {
    int id;
    public Chicken(int id) {
        this.id = id;
    }

}

//缓冲区
class SynContainer {

    Chicken[] chickens = new Chicken[10];

    int count = 0;

    //生产
    public synchronized void push(int i) throws InterruptedException {
        while (chickens.length == count) {
            this.wait();
        }
        Chicken chicken = new Chicken(i);
        chickens[count] = chicken;
        count++;
        System.out.println("生产第" + i + "只鸡");
        this.notifyAll();
    }

    //消费
    public synchronized void pop(int i) throws InterruptedException {
        while (count == 0) {
            this.wait();
        }
        //模拟延迟消费
        Thread.sleep(100);
        count--;
        System.out.println("消费第" + i + "只鸡");
        Chicken chicken = chickens[count];
        this.notifyAll();
    }
}

        

        

public class ProducersAndConsumers2 {
    public static void main(String[] args) {

        Product product = new Product();

        Producers2 p = new Producers2(product);
        Consumers2 c = new Consumers2(product);

        new Thread(p).start();
        new Thread(c).start();
    }
}


//生产者
class Producers2 implements Runnable {

    Product product;

    public Producers2(Product product) {
        this.product = product;
    }

    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {
            try {
                product.push(i);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

//消费者
class Consumers2 implements Runnable {

    Product product;

    public Consumers2(Product product) {
        this.product = product;
    }

    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {
            try {
                product.pop(i);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}


//缓冲区
class Product {

    //产品
    int id;

    //标志位
    boolean flag = false;

    //生产
    public synchronized void push(int id) throws InterruptedException {
        while (flag) {
            this.wait();
        }
        this.flag = !this.flag;
        System.out.println("生产了" + id + "号");
        this.notifyAll();
    }

    //消费
    public synchronized void pop(int id) throws InterruptedException {
        while (!flag) {
            this.wait();
        }
        this.flag = !this.flag;
        System.out.println("消费了" + id + "号");
        this.notifyAll();
    }
}

 

标签:消费者,生产者,线程,二十五,new,public,synContainer
来源: https://www.cnblogs.com/epiphany8/p/16293446.html