生产者,消费者

生产者,消费者

lock

//生产者消费者  lock
public class PC02 {
    public static void main(String[] args) {
        datas da=new datas();
        new Thread(()->{
            for(int i=1;i<=10;i++){
                da.producter();
            }
        },"p1").start();

        new Thread(()->{
            for(int i=1;i<=10;i++){
                da.consumer();
            }

        },"c1").start();
    }
}
class datas{
    private int num=0;
    Lock  lock=new ReentrantLock();
    Condition cond= lock.newCondition();//

    //生产者
    public void producter() {
        lock.lock();
        try {
            while(num!=0){ //这里,必须使用while,wait()必须在循环里,不能if()判断语句,当线程过多时,容易出现虚假唤醒
                //等待
                cond.await();
            }
            //生产
            System.out.println(Thread.currentThread().getName()+"生产产品"+(++num));
            //通知
            cond.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            lock.unlock();

        }
    }
    //消费者
    public  void consumer() {
        lock.lock();
        try {
            while(num==0){
                //等待
                cond.await();
            }
            //消费
            System.out.println(Thread.currentThread().getName()+"消费产品"+(num--)+"剩余产品"+num);

            //通知
            cond.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
}

synchronized

//生产者消费者  synchronized
public class PC01 {

    public static void main(String[] args) {
        data da=new data();
        new Thread(()->{
            for(int i=1;i<=10;i++){
                da.producter();
            }
        },"p1").start();

        new Thread(()->{
            for(int i=1;i<=10;i++){
                da.consumer();
            }

        },"c1").start();
    }

}
class data{
    private int num=10;
    //生产者
    public synchronized void producter() {
        while(num>0){ //这里,必须使用while,wait()必须在循环里,不能if()判断语句,当线程过多时,容易出现虚假唤醒
            //等待
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        //生产
        System.out.println(Thread.currentThread().getName()+"生产产品"+(++num));

        //通知
        notifyAll();

    }
    //消费者
    public synchronized void consumer() {
        while(num==0){
            //等待
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //消费
        System.out.println(Thread.currentThread().getName()+"消费产品"+(num--)+"剩余产品"+num);

        //通知
        notifyAll();

    }
}

管程法

//生产者消费者问题:管程法
public class PCThread {
    public static void main(String[] args) {
        Buffers bf = new Buffers();
        new Thread(new Productor(bf)).start();
        new Thread(new Consumer(bf)).start();
    }
}
//生产者
class Productor implements Runnable {
    public Buffers bf;
    public Productor(Buffers bf) {
        this.bf = bf;
    }
    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {
            bf.push(new Product(i));
            System.out.println("生产第" + i + "个产品");
        }
    }
}
//消费者
class Consumer implements Runnable {
    public Buffers bf;
    public Consumer(Buffers bf) {
        this.bf = bf;
    }
    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {
            System.out.println("消费第" + bf.pop().id + "个产品");
        }
    }
}

class Product {
    public int id;
    public Product(int id) {
        this.id = id;
    }
}

//缓冲区
class Buffers {
    Product[] products = new Product[10];
    int count = 0;
    //生产者 生产产品
    public synchronized void push(Product product) {
        if (count == products.length) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        products[count] = product;
        count++;
        this.notifyAll();
    }
   //消费者消费
    public synchronized Product pop() {
        if (count == 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        count--;
        Product pt = products[count];

        this.notifyAll();
        return pt;
    }
}

BlockingQueue 阻塞队列法

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

//生产者消费者  BlockingQueue  阻塞队列法
public class PC_bq {
    public static void main(String[] args) {
        BlockingQueue<String> block=new LinkedBlockingQueue<>(2);//默认大小为Integer.MAX_VALUE
     Producter p=new Producter(block);
     consumer c=new consumer(block);
     for(int i=0;i<5;i++){
         new Thread(p,"producet"+(i+1)).start();

         new Thread(c,"consume"+(i+1)).start();
     }
    }
}
class Producter implements  Runnable{
    BlockingQueue<String> bq;

    public Producter(BlockingQueue<String> bq) {
        this.bq = bq;
    }
    @Override
    public void run() {

        try {
            String msg="消费一个产品,生产线程是"+Thread.currentThread().getName();
            System.out.println( Thread.currentThread().getName()+"生产了一个产品,before put");
            bq.put(msg);//队列满的话,会阻塞
           System.out.println( Thread.currentThread().getName()+"生产了一个产品,afterd put"  );
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
class consumer implements  Runnable{
    BlockingQueue<String> bq;

    public consumer(BlockingQueue<String> bq) {
        this.bq = bq;
    }

    @Override
    public void run() {
        try {
            String msg= bq.take();
            System.out.println(msg);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值