{"id":1399,"date":"2023-03-19T11:16:06","date_gmt":"2023-03-19T03:16:06","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1399"},"modified":"2023-04-28T21:09:51","modified_gmt":"2023-04-28T13:09:51","slug":"about-use-and-understanding-of-redisson-locks","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/19\/about-use-and-understanding-of-redisson-locks\/","title":{"rendered":"\u5173\u4e8eRedisson\u9501\u7684\u4f7f\u7528\u548c\u7406\u89e3"},"content":{"rendered":"<h2>\u5bf9\u4e8e\u5206\u5e03\u5f0f\u9501\u7684\u8981\u6c42<\/h2>\n<ul>\n<li>\u4e92\u65a5\u6027\uff1a\u5206\u5e03\u5f0f\u9501\u9700\u8981\u4fdd\u8bc1\u5728\u4e0d\u540c\u8282\u70b9\u7684\u4e0d\u540c\u7ebf\u7a0b\u7684\u4e92\u65a5\u3002\u8fd9\u662f\u6700\u6839\u672c\u7684<\/li>\n<li>\u53ef\u91cd\u5165\u6027\uff1a\u540c\u4e00\u4e2a\u8282\u70b9\u4e0a\u7684\u540c\u4e00\u4e2a\u7ebf\u7a0b\u5982\u679c\u83b7\u53d6\u4e86\u9501\u4e4b\u540e\u4e5f\u53ef\u4ee5\u518d\u6b21\u83b7\u53d6\u8fd9\u4e2a\u9501<\/li>\n<li>\u9501\u8d85\u65f6\uff1a\u548c\u672c\u5730\u9501\u4e00\u6837\u652f\u6301\u9501\u8d85\u65f6\uff0c\u9632\u6b62\u6b7b\u9501<\/li>\n<li>\u9ad8\u53ef\u7528\uff1a\u52a0\u9501\u548c\u89e3\u9501\u9700\u8981\u9ad8\u6548\uff0c\u540c\u65f6\u4e5f\u9700\u8981\u4fdd\u8bc1\u9ad8\u53ef\u7528\u9632\u6b62\u5206\u5e03\u5f0f\u9501\u5931\u6548\uff0c\u53ef\u4ee5\u589e\u52a0\u964d\u7ea7<\/li>\n<li>\u652f\u6301\u963b\u585e\u548c\u975e\u963b\u585e\uff1a\u548c ReentrantLock \u4e00\u6837\u652f\u6301 lock \u548c trylock \u4ee5\u53ca tryLock(long timeOut)<\/li>\n<li>\u652f\u6301\u516c\u5e73\u9501\u548c\u975e\u516c\u5e73\u9501(\u53ef\u9009)\uff1a\u516c\u5e73\u9501\u7684\u610f\u601d\u662f\u6309\u7167\u8bf7\u6c42\u52a0\u9501\u7684\u987a\u5e8f\u83b7\u5f97\u9501\uff1b\u975e\u516c\u5e73\u9501\u5c31\u76f8\u53cd\u662f\u65e0\u5e8f\u7684\uff0c\u4e00\u822c\u6765\u8bf4\u5b9e\u73b0\u7684\u6bd4\u8f83\u5c11<\/li>\n<\/ul>\n<p><!-- more --><\/p>\n<h2>\u5173\u4e8eredisson\u9501<\/h2>\n<p>\u5176\u5b9e\u6211\u4eec\u90fd\u77e5\u9053ReentrantLock\u5df2\u7ecf\u6709\u5f88\u597d\u7684\u9501\u7684\u6027\u80fd\u548c\u5b9e\u73b0\uff0c\u5728\u4e92\u65a5\u6027\u3001\u53ef\u91cd\u5165\u6027\u3001\u9501\u8d85\u65f6\u3001\u652f\u6301\u963b\u585e\u3001\u652f\u6301\u516c\u5e73\u9501\u90fd\u6709\u5f88\u597d\u7684\u6027\u80fd\u548c\u5b9e\u73b0\uff0c\u4f46\u4e0d\u9002\u7528\u5206\u5e03\u5f0f\u573a\u666f\u3002redisson\u662f\u5206\u5e03\u5f0f\u9501\uff0c\u5f25\u8865\u8fd9\u4e00\u7f3a\u61be\uff08\u5206\u5e03\u5f0f\u9501\u6709\u5f88\u591a\u79cd\uff0c\u5176\u4ed6\u6b64\u6587\u4e0d\u505a\u8ba8\u8bba\uff09\uff0c\u5176\u4e2dRLock\u63a5\u53e3\u7ee7\u627f\u4e86Lock\u63a5\u53e3\uff0c\u81ea\u7136\u4e5f\u4f1a\u4f18\u96c5\u7684\u5b9e\u73b0\u4ee5\u4e0a\u5bf9\u9501\u7684\u8981\u6c42\u3002<\/p>\n<h2>\u6a21\u62df\u5e76\u53d1\u6d4b\u8bd5<\/h2>\n<h3>\u6d4b\u8bd5\u7c7b<\/h3>\n<pre><code class=\"language-java\">@RunWith(SpringRunner.class)\n@SpringBootTest\npublic class RedissLockUtilTest {\n\n    @Autowired\n    private RestTemplate restTemplate;\n\n    @Test\n    public void test() {\n        String helloworld = &quot;helloworld&quot;;\n        ConcurrentTestUtil.test(helloworld, e -&gt; {\n            ResponseEntity&lt;String&gt; forEntity = restTemplate\n                    .getForEntity(&quot;http:\/\/localhost:8080\/sendMessage?message=&quot;+Thread.currentThread().getName(), String.class);\n            String body = forEntity.getBody();\n            System.out.println(body);\n        }, 2); \/\/\u5e76\u53d12\u4e2a\u7ebf\u7a0b\u6d4b\u8bd5\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-java\">@Slf4j\n@RestController\n@SpringBootApplication\n@ComponentScan(&quot;cn.appblog&quot;)\npublic class SpringbootApplication {\n\n    @GetMapping(&quot;sendMessage&quot;)\n    public String sendMessage(String message, @RequestParam(required = false) Integer timeout) {\n        return doRedisson(message);\n    }\n\n    @Autowired\n    private RedissonClient redissonClient;\n\n    private String doRedisson(String message) {\n        RLock redLock = redissonClient.getLock(&quot;REDLOCK_KEY&quot;);\n        boolean tryLock = false;\n        try {\n            \/\/tryLock = redLock.tryLock(2, 5, TimeUnit.SECONDS);\n            \/\/if (!tryLock) return message;\n            redLock.lock(2, TimeUnit.SECONDS);\n            StopWatch stopWatch = new StopWatch();\n            stopWatch.start();\n            System.out.println(Thread.currentThread().getName());\n            countdown(5);\n            stopWatch.stop();\n            double totalTimeSeconds = stopWatch.getTotalTimeSeconds();\n            System.out.println(totalTimeSeconds);\n            return &quot;success&quot;;\n        } catch (Exception e) {\n            e.printStackTrace();\n        } finally {\n            \/\/if (redLock.isLocked())  \/\/\u9700\u8981unlock\u524d\u7684\u5224\u65ad\n            \/\/if (redLock.isHeldByCurrentThread())\n            redLock.unlock();\n        }\n        return message;\n    }\n\n    private void countdown(Integer timeout) {\n        Random random = new Random();\n        int i = random.nextInt(timeout);\n        \/\/int i = timeout;\n        while (i != 0) {\n            i--;\n            log.warn(String.valueOf(i));\n            try {\n                TimeUnit.SECONDS.sleep((long) 1);\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n        }\n    }\n\n    @Bean\n    public RestTemplate restTemplate(){\n        return new RestTemplate();\n    }\n\n    public static void main(String[] args) {\n        SpringApplication.run(SpringbootApplication.class, args);\n    }\n}<\/code><\/pre>\n<h3>\u65e5\u5fd7\u4fe1\u606f\u548c\u5206\u6790<\/h3>\n<pre><code class=\"language-java\">\/\/ \u9501\u7684\u91ca\u653e\u65f6\u95f4\n\/\/ \u65f6\u95f4\u5355\u4f4d\nvoid lock(long leaseTime, TimeUnit unit);<\/code><\/pre>\n<p>\u9009\u62e9<code>lock(2, TimeUnit.SECONDS);<\/code>\u8868\u793a\u9501\u8d85\u8fc7\u4e24\u79d2\u9501\u5373\u91ca\u653e\uff1b\u4f46\u662f\u7b2c\u4e00\u4e2a\u8bf7\u6c42\u83b7\u53d6\u5230\u9501\uff0c\u6267\u884c\u65b9\u6cd5\u9700\u89813\u79d2\uff0c\u81ea\u7136\u9501\u65e9\u5df2\u7ecf\u8d85\u65f6\u91ca\u653e\u3002\u5f53\u7b2c\u4e8c\u4e2a\u8bf7\u6c42\u6267\u884cunlock\u5c31\u4f1a\u62a5\u9519\u3002<\/p>\n<pre><code>2019-01-17 13:25:22.843  WARN 23556 --- [io-8080-exec-10] c.d.appblog.redis.SpringbootApplication  : 2\n2019-01-17 13:25:23.844  WARN 23556 --- [io-8080-exec-10] c.d.appblog.redis.SpringbootApplication  : 1\n2019-01-17 13:25:24.845  WARN 23556 --- [io-8080-exec-10] c.d.appblog.redis.SpringbootApplication  : 0\nhttp-nio-8080-exec-10----3.002\n2019-01-17 13:25:24.848  WARN 23556 --- [nio-8080-exec-9] c.d.appblog.redis.SpringbootApplication  : 2\n2019-01-17 13:25:25.849  WARN 23556 --- [nio-8080-exec-9] c.d.appblog.redis.SpringbootApplication  : 1\njava.lang.IllegalMonitorStateException: attempt to unlock lock, not locked by current thread by node id: 74ca2ff2-34c1-49c2-9c1d-5543780c952b thread-id: 69\norg.apache.catalina.connector.ClientAbortException: java.io.IOException: \u8fdc\u7a0b\u4e3b\u673a\u5f3a\u8feb\u5173\u95ed\u4e86\u4e00\u4e2a\u73b0\u6709\u7684\u8fde\u63a5\u3002<\/code><\/pre>\n<pre><code class=\"language-java\">\/\/ \u7ebf\u7a0b\u7684\u7b49\u5f85\u65f6\u95f4\uff0c\u5176\u4ed6\u8d85\u8fc7\u5373\u53d6\u6d88\n\/\/ \u9501\u7684\u91ca\u653e\u65f6\u95f4\n\/\/ \u65f6\u95f4\u5355\u4f4d\ntryLock(long waitTime, long leaseTime, TimeUnit unit);<\/code><\/pre>\n<p>\u9009\u62e9<code>tryLock(2, 5, TimeUnit.SECONDS);<\/code>\u8868\u793a\u7ebf\u7a0b\u7b49\u5f85\u8d85\u8fc7\u4e24\u79d2\u9501\u5373\u53d6\u6d88\u7b49\u5f85\uff1b\u4f46\u662f\u7b2c\u4e00\u4e2a\u8bf7\u6c42\u83b7\u53d6\u5230\u9501\uff0c\u6267\u884c\u65b9\u6cd5\u9700\u89813\u79d2\uff0c\u81ea\u7136\u8d85\u8fc7\u7b49\u5f85\u65f6\u95f4\u3002\u7b2c\u4e8c\u4e2a\u8bf7\u6c42\u4f1a\u53d8\u88ab\u6267\u884cunlock\u62a5\u9519\u3002<\/p>\n<pre><code>2019-01-17 14:52:49.493  WARN 18544 --- [nio-8080-exec-6] c.d.appblog.redis.SpringbootApplication  : 2\n2019-01-17 14:52:50.494  WARN 18544 --- [nio-8080-exec-6] c.d.appblog.redis.SpringbootApplication  : 1\n2019-01-17 14:52:51.495  WARN 18544 --- [nio-8080-exec-6] c.d.appblog.redis.SpringbootApplication  : 0\njava.lang.IllegalMonitorStateException: attempt to unlock lock, not locked by current thread by node id: 2e9e88ca-a4e0-4c53-be05-98ed6a96ba83 thread-id: 62\nhttp-nio-8080-exec-6----3.004<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5bf9\u4e8e\u5206\u5e03\u5f0f\u9501\u7684\u8981\u6c42 \u4e92\u65a5\u6027\uff1a\u5206\u5e03\u5f0f\u9501\u9700\u8981\u4fdd\u8bc1\u5728\u4e0d\u540c\u8282\u70b9\u7684\u4e0d\u540c\u7ebf\u7a0b\u7684\u4e92\u65a5\u3002\u8fd9\u662f\u6700\u6839\u672c\u7684 \u53ef\u91cd\u5165\u6027\uff1a\u540c\u4e00\u4e2a\u8282\u70b9\u4e0a\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[105],"class_list":["post-1399","post","type-post","status-publish","format-standard","hentry","category-distributed","tag-redisson"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1399","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1399"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1399\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}