{"id":258299,"date":"2022-12-26T09:16:25","date_gmt":"2022-12-26T01:16:25","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=258299"},"modified":"2022-11-29T10:17:30","modified_gmt":"2022-11-29T02:17:30","slug":"redis-atomicinteger-linux","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/redis-atomicinteger-linux.html","title":{"rendered":"redis\u4fdd\u5b58AtomicInteger\u5bf9\u8c61\u8e29\u5751\u53ca\u89e3\u51b3\u8be6\u89e3"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86redis\u4fdd\u5b58AtomicInteger\u5bf9\u8c61\u8e29\u5751\u53ca\u89e3\u51b3\u65b9\u6848\uff0c\u5177\u6709\u5f88\u597d\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\u3002\u5982\u6709\u9519\u8bef\u6216\u672a\u8003\u8651\u5b8c\u5168\u7684\u5730\u65b9\uff0c\u671b\u4e0d\u541d\u8d50\u6559<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
redis\u4fdd\u5b58AtomicInteger\u5bf9\u8c61\u8e29\u5751<\/strong><\/div>\n

redisTemplate \u4fdd\u5b58AtomicInteger\u5bf9\u8c61\u5f02\u5e38\uff1a<\/p>\n

java.lang.ClassCastException: java.util.concurrent.atomic.AtomicInteger cannot be cast to java.lang.String\r\n    at org.springframework.data.redis.serializer.StringRedisSerializer.serialize(StringRedisSerializer.java:36)\r\n    at org.springframework.data.redis.core.AbstractOperations.rawValue(AbstractOperations.java:127)\r\n    at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:235)\r\n    at com.quan.starter.service.impl.RedisServiceImpl.set(RedisServiceImpl.java:139)<\/pre>\n

\u8ddf\u8e2a\u6e90\u7801\u53d1\u73b0\u5176\u6267\u884c\u7684\u662f StringRedisSerializer \u7684\u5b9e\u73b0\uff0cserialize\u9ed8\u8ba4\u63a5\u6536\u7684\u53c2\u6570\u7c7b\u578b\u4e3aString \u4ece\u800c\u629b\u51fa\u4ee5\u4e0a\u5f02\u5e38<\/p>\n

\u7ecf\u8fc7\u68c0\u67e5\uff0c\u53d1\u73b0\u662fRedisTemplate\u6cdb\u578b\u60f9\u7684\u7978\uff1a<\/p>\n

@Autowired\r\nprivate RedisTemplate redisTemplate;<\/pre>\n
\u89e3\u51b3\u65b9\u6848<\/strong><\/span><\/div>\n

\u53bb\u9664\u6cdb\u578b\uff1a<\/p>\n

@Autowired\r\nprivate RedisTemplate redisTemplate;<\/pre>\n

\u8fd0\u884c\u670d\u52a1\u518d\u6b21\u8ddf\u8e2a\u6e90\u7801\uff0c\u6267\u884c\u7684\u662f DefaultValueOperations \u7684\u5b9e\u73b0\uff0c\u95ee\u9898\u89e3\u51b3<\/p>\n

RedisAtomicInteger\u7684\u4f7f\u7528<\/strong><\/div>\n

RedisAtomicInteger \u4ece\u540d\u5b57\u4e0a\u6765\u8bf4\u5c31\u662f redis \u7684\u539f\u5b50Integer \u6570\u636e\u7c7b\u578b\uff0c\u7531\u4e8e\u5176\u539f\u5b50\u6027\uff0c\u53ef\u7528\u4e8e\u79d2\u6740\u6d3b\u52a8\u7269\u54c1\u6570\u91cf\u7684\u63a7\u5236\u3002<\/p>\n

\u4ee5\u53ca\u4fdd\u8bc1\u987a\u5e8f\u751f\u6210\u6570\u5b57\u3002<\/p>\n

@Resource\r\n  RedisTemplate redisTemplate;\r\n \r\n \r\n  \/**\r\n   * RedisAtomicInteger\r\n   *\r\n   * @throws Exception \u5f02\u5e38\r\n   *\/\r\n  @Test\r\n  public void testTransaction1() throws Exception {\r\n      RedisAtomicInteger redisCount = new RedisAtomicInteger(\"key1\", this.redisTemplate.getConnectionFactory());\r\n      redisCount.set(0);\r\n      \/\/ \u521b\u5efa 100 \u4e2a\u7ebf\u7a0b \u5e76\u53d1\u6267\u884c  increment \u64cd\u4f5c\r\n      ExecutorService pool = Executors.newFixedThreadPool(10);\r\n      for (int i = 0; i < 100; i++) {\r\n          pool.submit(() -> {\r\n              \/\/ \u914d\u989d\u7801\u539f\u5b50\u53d8\u91cf\u503c\u589e\u52a0,\u6bcf\u6b21\u589e\u52a01\r\n              for (int j = 0; j < 100; j++) {\r\n                  int count = redisCount.incrementAndGet();\r\n                  log.info(Thread.currentThread().getName() + \": \" + count);\r\n              }\r\n          });\r\n      }\r\n  }<\/pre>\n

\u7ed3\u679c<\/p>\n

.\r\n.\r\n.\r\npool-2-thread-90: 9989\r\npool-2-thread-61: 9987\r\npool-2-thread-3: 9986\r\npool-2-thread-12: 9990\r\npool-2-thread-25: 9991\r\npool-2-thread-90: 9992\r\npool-2-thread-12: 9994\r\npool-2-thread-61: 9993\r\npool-2-thread-25: 9995\r\npool-2-thread-61: 10000\r\npool-2-thread-12: 9996\r\npool-2-thread-61: 9997\r\npool-2-thread-25: 9998\r\npool-2-thread-12: 9999<\/pre>\n","protected":false},"excerpt":{"rendered":"

redisTemplate \u4fdd\u5b58AtomicInteger\u5bf9\u8c61\u5f02\u5e38\uff1a java.lang.ClassCastE […]<\/p>\n","protected":false},"author":1479,"featured_media":84130,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-258299","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/258299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1479"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=258299"}],"version-history":[{"count":6,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/258299\/revisions"}],"predecessor-version":[{"id":258523,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/258299\/revisions\/258523"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/84130"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=258299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=258299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=258299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}