前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >synchronized锁和ReentrantLock锁的使用

synchronized锁和ReentrantLock锁的使用

作者头像
周杰伦本人
发布2022-10-25 15:46:07
1930
发布2022-10-25 15:46:07
举报
文章被收录于专栏:同步文章
代码语言:javascript
复制
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的锁是基于对象的 只要是同一个对象 锁就能起作用

代码语言:javascript
复制
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 应该在成员变量位置才锁得住

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-02-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档