synchronized锁和ReentrantLock锁的使用

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

/**
 * @author: xiepanpan
 * @Date: 2020/2/17
 * @Description:
 */
@Service
public class RedisIncrService {

    @Autowired
    StringRedisTemplate redisTemplate;

	private Object obj = new Object();
	//这个锁大家都用一个。
    //this当前对象。当前service对象。spring的组件是单例的。this一个。
    //this相同,锁相同,锁ok
    //RedisIncrService对象一个。自动注入;StringRedisTemplate,redisTemplate也只能注入唯一一个。
    //RedisIncrService对象创建的时候赋值,RedisIncrService一个   private Object obj = new Object();

    //1)、synchronized(this):能
    //2)、synchronized (redisTemplate):能
    //3)、synchronized (new Object()):锁不住
    //4)、synchronized (obj):锁得住?锁得住
    //5)、synchronized (obj());锁的住
    //6)、synchronized (RedisIncrService.class);锁得住
    public void incr() {
        synchronized (this){
            ValueOperations<String, String> stringStringValueOperations = redisTemplate.opsForValue();
            String num = stringStringValueOperations.get("num");
            if(num!=null){
                Integer i = Integer.parseInt(num);
                i = i+1;
                stringStringValueOperations.set("num",i.toString());
            }
        }
    }

	public Object obj() {
        
        return obj;
    }
     //肯定锁不住
    //public Object obj() {
    //    Object o = new Object();
    //    BeanUtils.copyProperties(obj, o);
    //    return o;
    //}

}

synchronized的锁是基于对象的 只要是同一个对象 锁就能起作用



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

/**
 * @author: xiepanpan
 * @Date: 2020/2/17
 * @Description:
 */
@Service
public class RedisIncrService {

    @Autowired
    StringRedisTemplate redisTemplate;
    
	ReentrantLock lock = new ReentrantLock();

	
    public void incr() {

        /**
         *  ReentrantLock lock = new ReentrantLock();应该在成员变量位置才锁得住
         */

        //锁得住?
        lock.lock();
        ValueOperations<String, String> stringStringValueOperations = redisTemplate.opsForValue();
        String num = stringStringValueOperations.get("num");
        if (num != null) {
            Integer i = Integer.parseInt(num);
            i = i + 1;
            stringStringValueOperations.set("num", i.toString());
        }

        lock.unlock();

       
    }

}



ReentrantLock 应该在成员变量位置才锁得住

以上两种锁都是进程内的锁 单机跑,分布式肯定不好使

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
synchronized和ReentrantLock的性能不能一概而论,因为它们在不同的场景下表现可能会有所不同。早期版本的synchronized在很多场景下性能相差较大,但在后续版本中进行了较多改进,在低竞争场景中可能优于ReentrantLock。JDK6中synchronized加入了自适应自旋、消除、粗化、轻量级、偏向等一系列优化,官方也提倡在能满足需求的前提下优先考虑使用synchronized进行同步。[1][2] ReentrantLock是标准的乐观的实现,它通过内部的while循环判断是否被其他线程所持有,当其他线程持有时,就会一直自旋判断是否被释放。如果资源竞争激烈,同时竞争激烈,使用乐观可能会导致很多线程一直在循环等待,当线程数和执行时间达到一个临界值时,乐观的性能可能会比线程挂起的效率更低,循环等待的开销会大于线程挂起的开销。因此,当需要加的代码块执行时间普遍很长时,不建议使用ReentrantLock。[2] 另一方面,当资源竞争激烈,同时尝试获取的线程很多时,部分线程等待过久,如果使用synchronized,会导致慢慢膨胀,资源占有会越来越多。为了保证synchronized的性能,加的代码块需要保证执行时间稳定,不会突然暴增。[2] 总的来说,synchronized和ReentrantLock在不同的场景下可能有不同的性能表现,需要根据具体情况选择合适的机制。synchronized在低竞争场景中可能优于ReentrantLock,而ReentrantLock则提供了更多的便利方法,可以进行精细的同步操作,甚至实现synchronized难以表达的用例。[1][3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值