如何用synchronized和lock锁实现生产者和消费者模式

1.synchronized

2.Lock锁

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生产者-消费者同步问题的模拟: 使用synchronized: ```java public class ProducerConsumer { private static final int BUFFER_SIZE = 10; private static final Object lock = new Object(); private static int[] buffer = new int[BUFFER_SIZE]; private static int count = 0; public static void main(String[] args) { Thread producer = new Thread(() -> { for (int i = 0; i < BUFFER_SIZE; i++) { synchronized (lock) { while (count == BUFFER_SIZE) { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } buffer[count++] = i; lock.notifyAll(); } } }); Thread consumer = new Thread(() -> { for (int i = 0; i < BUFFER_SIZE; i++) { synchronized (lock) { while (count == 0) { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(buffer[--count]); lock.notifyAll(); } } }); producer.start(); consumer.start(); } } ``` 使用Lock: ```java public class ProducerConsumer { private static final int BUFFER_SIZE = 10; private static final Lock lock = new ReentrantLock(); private static final Condition notFull = lock.newCondition(); private static final Condition notEmpty = lock.newCondition(); private static int[] buffer = new int[BUFFER_SIZE]; private static int count = 0; public static void main(String[] args) { Thread producer = new Thread(() -> { for (int i = 0; i < BUFFER_SIZE; i++) { lock.lock(); try { while (count == BUFFER_SIZE) { notFull.await(); } buffer[count++] = i; notEmpty.signalAll(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } }); Thread consumer = new Thread(() -> { for (int i = 0; i < BUFFER_SIZE; i++) { lock.lock(); try { while (count == 0) { notEmpty.await(); } System.out.println(buffer[--count]); notFull.signalAll(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } }); producer.start(); consumer.start(); } } ``` 使用阻塞队列: ```java public class ProducerConsumer { private static final int BUFFER_SIZE = 10; public static void main(String[] args) { BlockingQueue<Integer> buffer = new ArrayBlockingQueue<>(BUFFER_SIZE); Thread producer = new Thread(() -> { for (int i = 0; i < BUFFER_SIZE; i++) { try { buffer.put(i); } catch (InterruptedException e) { e.printStackTrace(); } } }); Thread consumer = new Thread(() -> { for (int i = 0; i < BUFFER_SIZE; i++) { try { System.out.println(buffer.take()); } catch (InterruptedException e) { e.printStackTrace(); } } }); producer.start(); consumer.start(); } } ``` 交替输出ABABABAB的实现使用synchronized: ```java public class AlternatePrinting { private static final Object lock = new Object(); private static boolean flag = true; public static void main(String[] args) { Thread t1 = new Thread(() -> { for (int i = 0; i < 5; i++) { synchronized (lock) { while (!flag) { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.print("A"); flag = false; lock.notifyAll(); } } }); Thread t2 = new Thread(() -> { for (int i = 0; i < 5; i++) { synchronized (lock) { while (flag) { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.print("B"); flag = true; lock.notifyAll(); } } }); t1.start(); t2.start(); } } ``` 使用Lock: ```java public class AlternatePrinting { private static final Lock lock = new ReentrantLock(); private static final Condition condition = lock.newCondition(); private static boolean flag = true; public static void main(String[] args) { Thread t1 = new Thread(() -> { for (int i = 0; i < 5; i++) { lock.lock(); try { while (!flag) { condition.await(); } System.out.print("A"); flag = false; condition.signalAll(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } }); Thread t2 = new Thread(() -> { for (int i = 0; i < 5; i++) { lock.lock(); try { while (flag) { condition.await(); } System.out.print("B"); flag = true; condition.signalAll(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } }); t1.start(); t2.start(); } } ``` 使用Semaphore: ```java public class AlternatePrinting { private static final Semaphore s1 = new Semaphore(1); private static final Semaphore s2 = new Semaphore(0); public static void main(String[] args) { Thread t1 = new Thread(() -> { for (int i = 0; i < 5; i++) { try { s1.acquire(); System.out.print("A"); s2.release(); } catch (InterruptedException e) { e.printStackTrace(); } } }); Thread t2 = new Thread(() -> { for (int i = 0; i < 5; i++) { try { s2.acquire(); System.out.print("B"); s1.release(); } catch (InterruptedException e) { e.printStackTrace(); } } }); t1.start(); t2.start(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值