生产者消费者

注意:必须使用while循环,因为当前线程被唤醒之前,同时有消费者和生产线程都有执行过,导致当前的条件不满足。例如,size为0导致消费者线程thread1被阻塞,后来成产者线程thread2和消费者线程thread3都有执行,thread2唤醒了thread1,但是此时thread1发现size还是0,继续阻塞

wait() / notify()方法

public class Storage1 {

  private int MAX_SIZE = 4;

  private List<Object> list = new ArrayList<>();

  public void produce() {
    synchronized (list) {
      while (list.size() == MAX_SIZE) {
        System.out.println("full,wait!");
        try {
          list.wait();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      list.add(new Object());
      System.out.println("produce,now size is:" + list.size());
      list.notifyAll();
    }
  }

  public void consume() {
    synchronized (list) {
      while (list.size() == 0) {
        System.out.println("empty,wait!");
        try {
          list.wait();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      list.remove(0);
      System.out.println("consume,new size is:" + list.size());
      list.notifyAll();
    }
  }

  public static void main(String[] args) {
    Storage1 storage1 = new Storage1();
    for (int i = 0; i < 10; i++) {
      new Thread(new Runnable() {
        @Override
        public void run() {
          storage1.consume();
        }
      }).start();
    }
    for (int i = 0; i < 10; i++) {
      new Thread(new Runnable() {
        @Override
        public void run() {
          storage1.produce();
        }
      }).start();
    }
  }
}

await() / signal()方法

通过在Lock对象上调用newCondition()方法,将一个condition和一个约束条件进行绑定,进而控制并发程序访问竞争资源的安全

public class Storage2 {

  private int MAX_SIZE = 4;

  private List<Object> list = new ArrayList<>();

  private Lock lock = new ReentrantLock();

  private Condition notEmpty = lock.newCondition();
  private Condition notFull = lock.newCondition();

  public void produce() {
    lock.lock();
    try {
      while (list.size() == MAX_SIZE) {
        System.out.println("full,wait!");
        notFull.await();
      }
      list.add(new Object());
      System.out.println("produce,now size is:" + list.size());
      notEmpty.signalAll();
    } catch (InterruptedException e) {
      e.printStackTrace();
    } finally {
      lock.unlock();
    }
  }

  public void consume() {
    lock.lock();
    try {
      while (list.isEmpty()) {
        System.out.println("empty,wait!");
        notEmpty.await();
      }
      list.remove(0);
      System.out.println("consume,now size is:" + list.size());
      notFull.signalAll();
    } catch (InterruptedException e) {
      e.printStackTrace();
    } finally {
      lock.unlock();
    }
  }

  public static void main(String[] args) {
    Storage2 Storage2 = new Storage2();
    for (int i = 0; i < 10; i++) {
      new Thread(new Runnable() {
        @Override
        public void run() {
          Storage2.consume();
        }
      }).start();
    }
    for (int i = 0; i < 10; i++) {
      new Thread(new Runnable() {
        @Override
        public void run() {
          Storage2.produce();
        }
      }).start();
    }
  }
}

BlockingQueue

  • put()方法:容量达到最大时,自动阻塞。
  • take()方法:容量为0时,自动阻塞。
public class Storage3 {

  private int MAX_SIZE = 4;

  private LinkedBlockingDeque<Object> deque = new LinkedBlockingDeque<>(MAX_SIZE);

  public void produce() {
    if (deque.size() == MAX_SIZE) {
      System.out.println("full,wait!");
    }

    try {
      deque.put(new Object());
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("produce,now size is:" + deque.size());
  }

  public void consume() {
    if (deque.isEmpty()) {
      System.out.println("empty,wait!");
    }

    try {
      deque.takeFirst();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("consume,now size is:" + deque.size());

  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值