synchronized和Lock实现生产者消费者

该博客对比了使用synchronized和Lock(ReentrantLock)在Java中实现生产者消费者问题的区别,包括锁的获取与释放、中断处理、公平性等方面,并提供了详细的代码示例。总结了两者在不同场景下的适用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

synchronized实现生产者消费者:

public class ProducerConsumer2 {

    public static void main(String[] args) {
        Data2 data = new Data2();

        new Thread(()->{
            for(int i = 0; i < 20; i++) {
                try {
                    data.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "A").start();

        new Thread(()->{
            for(int i = 0; i < 20; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "B").start();
    }

}

class Data2 {
    private int num = 0;

    public synchronized void increment() throws InterruptedException {
        while(num!=0) {
            this.wait();
        }
        num++;
        System.out.println(Thread.currentThread().getName()+"=>"+num);
        this.notifyAll();
    }

    public synchronized void decrement() throws InterruptedException {
        while(num!=1) {
            this.wait();
        }
        num--;
        System.out.println(Thread.currentThread().getName()+"=>"+num);
        this.notifyAll();

    }
}

Lock实现生产者消费者

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ProducerConsumer {

    public static void main(String[] args) {
        Data data = new Data();

        new Thread(()->{
            for(int i = 0; i < 20; i++) {
                data.increment();
            }
        }, "A").start();

        new Thread(()->{
            for(int i = 0; i < 20; i++) {
                data.decrement();
            }
        }, "B").start();
    }

}

class Data {
    private int num = 0;
    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();

    public void increment() {
        lock.lock();
        try {
            while(num!=0) {
                condition.await();
            }
            num++;
            System.out.println(Thread.currentThread().getName()+"=>"+num);
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void decrement() {
        lock.lock();
        try {
            while(num!=1) {
                condition.await();
            }
            num--;
            System.out.println(Thread.currentThread().getName()+"=>"+num);
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

}

总结

一. synchronized和Lock的不同

1.synchronized是Java关键字,Lock是juc下的一个接口(常用实现类ReentrantLock);

2.synchronized无法获取锁的状态,Lock可以判断是否得到了锁(tryLock方法,不会一直阻塞);

3.synchronized会自动释放锁,Lock要手动释放(再finally中,不然会死锁);

4.synchronized阻塞会傻傻的等,Lock不一定会等待(同tryLock);

5.synchronized可重入锁,不可中断的,非公平;Lock可重入锁,可以判断锁,公平/非公平;

6.synchronized适合少量代码同步问题,Lock适合锁大量的同步代码。

二. 生产者消费者问题流程

1.判断是否可以占用锁,如果可以则占用锁,调用wait/await(这里需要用while循环判断,如果用if会发生虚假唤醒);

2.执行业务代码num++/num–;

3.释放锁,唤醒其他线程notifyAll/signalAll。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值