public class CommonConsumerService { //库存个数 static int goodsCount = 900; //卖出个数 static int saleCount = 0; public static void main(String[] args) throws InterruptedException { for (int i = 0; i < 1000; i++) { new Thread(() -> { try {Thread.sleep(2);} catch (InterruptedException e) {} if (goodsCount > 0) { goodsCount--; System.out.println("剩余库存:" + goodsCount + " 卖出个数" + ++saleCount); } }).start(); } Thread.sleep(3000); } }
運行一次,最後幾行的輸出結果如下,很明顯出錯了,剩餘0個商品卻只賣出了899個商品,很明顯有商品被某個線程私吞了。
...
剩餘庫存:5 賣出個數893
剩餘庫存:5 賣出個數894
剩餘庫存:4 賣出個數895
剩餘庫存:2 賣出個數896
剩餘庫存:2 賣出個數897
剩餘庫存:1 賣出個數898
剩餘庫存:0 賣出個數899
redis是單執行緒的,串列執行,那麼接下來使用redis為資源加鎖。
1.首先引入依賴
compile "org.springframework.boot:spring-boot-starter-data-redis"
2.引入redis加鎖工具類別
package com.kingboy.common.utils; import redis.clients.jedis.Jedis; import java.util.Collections; /** * @author kingboy--KingBoyWorld@163.com * @date 2017/12/29 下午1:57 * @desc Redis工具. */ public class RedisTool { private static final String LOCK_SUCCESS = "OK"; private static final String SET_IF_NOT_EXIST = "NX"; private static final String SET_WITH_EXPIRE_TIME = "PX"; private static final Long RELEASE_SUCCESS = 1L; /** * 尝试获取分布式锁 * @param jedis Redis客户端 * @param lockKey 锁 * @param requestId 请求标识 * @param expireTime 超期时间 * @return 是否获取成功 */ public static boolean tryGetDistributedLock(Jedis jedis, String lockKey, String requestId, int expireTime) { String result = jedis.set(lockKey, requestId, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, expireTime); if (LOCK_SUCCESS.equals(result)) { return true; } return false; } /** * 释放分布式锁 * @param jedis Redis客户端 * @param lockKey 锁 * @param requestId 请求标识 * @return 是否释放成功 */ public static boolean releaseDistributedLock(Jedis jedis, String lockKey, String requestId) { String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end"; Object result = jedis.eval(script, Collections.singletonList(lockKey), Collections.singletonList(requestId)); if (RELEASE_SUCCESS.equals(result)) { return true; } return false; } }
3.將上面沒有鎖定的範例程式碼改編如下:
public class RedisLockConsumerService { //库存个数 static int goodsCount = 900; //卖出个数 static int saleCount = 0; @SneakyThrows public static void main(String[] args) { JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "192.168.0.130", 6379, 1000); for (int i = 0; i < 1000; i++) { new Thread(() -> { try {Thread.sleep(2);} catch (InterruptedException e) {} Jedis jedis = jedisPool.getResource(); boolean lock = false; while (!lock) { lock = RedisTool.tryGetDistributedLock(jedis, "goodsCount", Thread.currentThread().getName(), 10); } if (lock) { if (goodsCount > 0) { goodsCount--; System.out.println("剩余库存:" + goodsCount + " 卖出个数" + ++saleCount); } } RedisTool.releaseDistributedLock(jedis, "goodsCount", Thread.currentThread().getName()); jedis.close(); }).start(); } Thread.sleep(3000); jedisPool.close(); } }
執行幾次程式輸出結果如下,可以看到結果是有序,並且正確的。
...
剩餘庫存:6 賣出個數894
剩餘庫存:5 賣出個數895
剩餘庫存:4 賣出個數896
剩餘庫存:3 賣出個數897
剩餘庫存:2 賣出個數898
剩餘庫存:1 賣出個數899
剩餘庫存:0 賣出個數900
以上是SpringBoot中如何使用Redis作為全域鎖的詳細內容。更多資訊請關注PHP中文網其他相關文章!