JUC学习--利用synchronized和Lock实现生产者消费者、利用Lock相关知识唤醒指定线程
实现生产者与消费者模型还可以利用阻塞队列来实现,本文就不赘述了。
利用sychronized实现生产者与消费者模型
package com.example.demo; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * @author : lijie * @version V1.0 * @Description: 利用sychronized实现生产者与消费者模型 * @date Date : 2021年07月29日 23:16 */ public class Test1 { public static void main(String[] args) { ExecutorService service = Executors.newFixedThreadPool(8); Ticket ticket = new Ticket(); for (int i = 0; i < 4; i++) { service.submit(() -> { while (true) { ticket.decrement(); } }); } for (int i = 0; i < 4; i++) { service.submit(() -> { while(true) { ticket.increment(); } }); } // new Thread(() -> { // for (int i = 0; i < 40; i++) { // tickct.decrement(); // } // }, "consumer").start(); // // new Thread(() -> { // for (int i = 0; i < 40; i++) { // tickct.increment(); // } // }, "producer").start(); } } class Ticket { int num = 0; public synchronized void decrement() { // 抢锁 while (num == 0) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } // 业务 // num--; System.out.println(Thread.currentThread().getName() + "减少1个,还剩" + (--num)); // 释放锁 this.notifyAll(); } public synchronized void increment() { while (num != 0) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } // num++; System.out.println(Thread.currentThread().getName() + "增加1个,还剩" + (++num)); this.notifyAll(); } }
利用Lock锁来实现生产者与消费者模型
package com.example.demo; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * @author : lijie * @version V1.0 * @Description: 利用Lock锁来实现生产者与消费者模型 * @date Date : 2021年08月01日 22:51 */ public class Test2 { private static ExecutorService threadPool = Executors.newFixedThreadPool(4); private static Ticket2 ticket2 = new Ticket2(); public static void main(String[] args) { for (int i = 0; i < 2; i++) { threadPool.submit(() -> { while (true) { ticket2.increment(); } }); } for (int i = 0; i < 2; i++) { threadPool.submit(() -> { while (true) { ticket2.decremrnt(); } }); } } } class Ticket2 { Lock lock = new ReentrantLock(); Condition condition = lock.newCondition(); int num = 0; public void increment() { lock.lock(); try { while (num != 0) { condition.await(); } num++; System.out.println(Thread.currentThread().getName() + "增加了1,还有" + num); condition.signalAll(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } public void decremrnt() { lock.lock(); try { while (num == 0) { condition.await(); } num--; System.out.println(Thread.currentThread().getName() + "减少了1,还有" + num); condition.signalAll(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } }
执行唤醒线程:让线程按照某种规则执行
package com.example.demo; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * @author : lijie * @version V1.0 * @Description: 执行唤醒线程:让线程按照某种规则执行 * @date Date : 2021年08月01日 23:35 */ public class Test3 { public static void main(String[] args) { Hello hello = new Hello(); new Thread(() -> { while (true) { hello.sayA(); } }, "AA").start(); new Thread(() -> { while (true) { hello.sayB(); } }, "BB").start(); new Thread(() -> { while (true) { hello.sayC(); } }, "CC").start(); } } class Hello { private Lock lock = new ReentrantLock(); Condition conditionA = lock.newCondition(); Condition conditionB = lock.newCondition(); Condition conditionC = lock.newCondition(); int num = 1; public void sayA() { try { lock.lock(); while (num != 1) { conditionA.await(); } System.out.println(Thread.currentThread().getName() + "--AA"); num = 2; conditionB.signal(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } public void sayB() { lock.lock(); try { while (num != 2) { conditionB.await(); } System.out.println(Thread.currentThread().getName() + "--BB"); num = 3; conditionC.signal(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } public void sayC() { lock.lock(); try { while (num != 3) { conditionC.await(); } System.out.println(Thread.currentThread().getName() + "--CC"); num = 1; conditionA.signal(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } }