一、传统的线程间通信的方式
使用
Object:
synchronized:阻塞锁
wait():阻塞当前队列
notifyAll():唤醒所有阻塞队列
package ProduceAndConsumer;/** @Auther:生产者消费者案例1* @Date:2024/6/20* @Description:qinhao* @VERSON:1.8*/public class PACTest {public static void main(String[] args) {Clerk clerk = new Clerk();new Thread(new Product(clerk)).start();new Thread(new Consumer(clerk)).start();}
}class Clerk{private int count = 0;private static final int maxCount = 3;public synchronized void product() throws InterruptedException {if(count < maxCount){Thread.sleep(500);count++;System.out.println(Thread.currentThread().getName() + "生产了一个商品,当前商品有:" + count);notifyAll();}else{System.out.println(Thread.currentThread().getName() + "仓库已满");this.wait();}}public synchronized void consumer() throws InterruptedException {if(count > 0){Thread.sleep(500);count--;System.out.println(Thread.currentThread().getName() + "消费了一个商品,当前商品有:" + count);notifyAll();}else{System.out.println(Thread.currentThread().getName() + "缺货");this.wait();}}
}//生产者
class Product implements Runnable{private Clerk clerk;Product(Clerk clerk){this.clerk = clerk;}@Overridepublic void run() {while(true){try {clerk.product();} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}//消费者
class Consumer implements Runnable{private Clerk clerk;Consumer(Clerk clerk){this.clerk = clerk;}@Overridepublic void run() {while(true){try {clerk.consumer();} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}
二、Condition控制线程通信
使用 :
lock :
lock():加锁
unlock():释放锁
condition:
await():阻塞当前线程
signalall():唤醒阻塞线程
package ProduceAndConsumer;/** @Auther:勤学好问* @Date:2024/6/20* @Description:qinhao* @VERSON:1.8*/import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;public class PACTest {public static void main(String[] args) {Clerk clerk = new Clerk();new Thread(new Product(clerk)).start();new Thread(new Consumer(clerk)).start();}
}class Clerk {private int count = 0;private static final int maxCount = 3;private Lock lock = new ReentrantLock();private Condition condition = lock.newCondition();public void product() throws InterruptedException {//枷锁lock.lock();//确保锁释放try {if (count < maxCount) {Thread.sleep(500);count++;System.out.println(Thread.currentThread().getName() + "生产了一个商品,当前商品有:" + count);condition.signalAll();} else {System.out.println(Thread.currentThread().getName() + "仓库已满");condition.await();}} finally {lock.unlock();}}public void consumer() throws InterruptedException {//枷锁lock.lock();//确保锁会释放try {if (count > 0) {Thread.sleep(500);count--;System.out.println(Thread.currentThread().getName() + "消费了一个商品,当前商品有:" + count);condition.signalAll();} else {System.out.println(Thread.currentThread().getName() + "缺货");condition.await();}} finally {lock.unlock();}}
}class Product implements Runnable {private Clerk clerk;Product(Clerk clerk) {this.clerk = clerk;}@Overridepublic void run() {while (true) {try {clerk.product();} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}class Consumer implements Runnable {private Clerk clerk;Consumer(Clerk clerk) {this.clerk = clerk;}@Overridepublic void run() {while (true) {try {clerk.consumer();} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}