本文共 5948 字,大约阅读时间需要 19 分钟。
在生产消费问题中,Java的synchronized关键字提供了一种简单且有效的方式来管理共享资源的访问。通过对对象本身进行锁定,确保在多线程环境下,同一时间只允许一个线程执行特定方法。这种机制避免了数据竞争和不一致性问题。
public class App { private int num; public void product() { try { Thread.sleep(1000); synchronized (this) { while (num >= 100) { wait(); } num++; System.out.println("生产"); System.out.println("库存量:" + num); notifyAll(); } } catch (InterruptedException e) { e.printStackTrace(); } } public void sell() { try { Thread.sleep(500); synchronized (this) { while (num <= 0) { wait(); } num--; System.out.println("卖出"); System.out.println("库存量:" + num); notifyAll(); } } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception { App app = new App(); Runnable product = new Runnable() { @Override public void run() { while (true) { app.product(); } } }; Runnable sell = new Runnable() { @Override public void run() { while (true) { app.sell(); } } }; for (int i = 0; i < 5; i++) { new Thread(product).start(); } for (int i = 0; i < 10; i++) { new Thread(sell).start(); } }} wait() 和 notify() 方法自动协调线程等待和唤醒。synchronized的性能相对较低,特别是在高并发场景下。阻塞队列是一种更高级的生产消费模式,通过自动管理资源的分配和等待,减少了生产消费问题中常见的死锁和不一致性问题。
import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;import java.util.concurrent.atomic.AtomicInteger;public class App { private BlockingQueue blockingQueue = new ArrayBlockingQueue<>(100); private AtomicInteger atomicInteger = new AtomicInteger(); public void product() { try { Thread.sleep(1000); blockingQueue.offer(atomicInteger.incrementAndGet()); System.out.println("工厂" + Thread.currentThread().getName() + "生产\n库存量:" + blockingQueue.size()); } catch (InterruptedException e) { e.printStackTrace(); } } public void sell() { try { Thread.sleep(500); System.out.println("消费者" + Thread.currentThread().getName() + "购买" + blockingQueue.take() + "号商品\n库存量:" + blockingQueue.size()); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception { App app = new App(); Runnable product = new Runnable() { @Override public void run() { while (true) { app.product(); } } }; Runnable sell = new Runnable() { @Override public void run() { while (true) { app.sell(); } } }; for (int i = 0; i < 3; i++) { new Thread(product).start(); } for (int i = 0; i < 5; i++) { new Thread(sell).start(); } }} synchronized,阻塞队列在多线程场景下表现更为高效。synchronized,阻塞队列的使用需要更深入的理解和掌握。ReentrantLock是一种更高级的互斥机制,结合了传统锁和信号量的优点,支持多级重入,适用于复杂的多线程场景。
import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;public class App { private ReentrantLock lock = new ReentrantLock(); private final Condition notFull = lock.newCondition(); private final Condition notEmpty = lock.newCondition(); private int num = 0; public void product() { try { Thread.sleep(1000); lock.lock(); while (num > 100) { notFull.await(); } num++; System.out.println("工厂" + Thread.currentThread().getName() + "生产\n库存:" + num); notEmpty.signalAll(); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } public void sell() { try { Thread.sleep(800); lock.lock(); while (num <= 0) { notEmpty.await(); } num--; System.out.println("顾客" + Thread.currentThread().getName() + "购买\n库存:" + num); notFull.signalAll(); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } public static void main(String[] args) { App app = new App(); for (int i = 0; i < 10; i++) { new Thread(new Runnable() { @Override public void run() { while (true) { app.product(); } } }).start(); } for (int i = 0; i < 5; i++) { new Thread(new Runnable() { @Override public void run() { while (true) { app.sell(); } } }).start(); } }} Condition对象,实现了对资源状态的细致监控。synchronized,ReentrantLock的性能更高,特别是在多线程高并发场景下。synchronized,ReentrantLock的使用稍微复杂,需要配置Condition对象。java.util.concurrent.locks包中的类,增加了依赖性。在生产消费问题中,选择合适的锁机制至关重要。synchronized简单易用,但在高并发场景下性能不足;BlockingQueue提供了更高级的资源管理,但需要较高的学习成本;ReentrantLock则在性能和灵活性之间找到了平衡,适用于更复杂的多线程场景。根据具体需求选择合适的实现方案,可以最大化资源利用率和系统性能。
转载地址:http://upvj.baihongyu.com/