{"id":2197,"date":"2022-01-08T23:04:26","date_gmt":"2022-01-08T15:04:26","guid":{"rendered":"https:\/\/coderbee.net\/?p=2197"},"modified":"2022-01-08T23:04:58","modified_gmt":"2022-01-08T15:04:58","slug":"druid-%e4%b8%8e-hikaricp-%e8%8e%b7%e5%8f%96%e8%bf%9e%e6%8e%a5%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/java\/20220108\/2197","title":{"rendered":"Druid \u4e0e HikariCP \u83b7\u53d6\u8fde\u63a5\u7684\u533a\u522b"},"content":{"rendered":"<p>\u5728\u4e4b\u524d\u7684\u6587\u7ae0\u300a\u8e29\u5751 Druid \u8fde\u63a5\u6c60\u300b\u8bf4\u8e29\u4e86\u5751\uff0c\u540e\u9762\u7ecf\u4eba\u63d0\u9192\uff0c\u53d1\u73b0\u6839\u56e0\u662f\u4e00\u4e2a\u7b49\u5f85\u83b7\u53d6\u8fde\u63a5\u7684 Job \u7ebf\u7a0b\u88ab\u7ec8\u6b62\u4e86\uff0c\u901a\u8fc7\u76f4\u63a5\u8c03\u7528\u7ebf\u7a0b\u7684 stop \u65b9\u6cd5\u7ec8\u6b62\u7684\uff0c\u8fd9\u79cd\u65b9\u5f0f\u7834\u574f\u4e86 ReentrantLock \u9501\u7684\u6a21\u578b\u3002<\/p>\n<p>\u4e0b\u9762\u8fd9\u4e2a\u65b9\u6cd5\u662f\u5728\u6301\u6709\u9501\u7684\u60c5\u51b5\u4e0b\u6267\u884c\u7684\uff0c\u6267\u884c\u5230 1491 \u884c\u65f6\uff0cjob \u7ebf\u7a0b\u4f1a\u628a\u81ea\u5df1\u52a0\u5165\u6761\u4ef6\u5bf9\u8c61\u7684\u7b49\u5f85\u961f\u5217\u3001\u7136\u540e\u91ca\u653e\u9501\uff0c\u7b49\u5f85\u5176\u4ed6\u7ebf\u7a0b\u6765\u5524\u9192\uff1b<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2021\/12\/druid-polllast.png\" alt=\"\" width=\"1087\" height=\"505\" class=\"alignnone size-full wp-image-2200\" \/><\/p>\n<p>\u5176\u4ed6\u7ebf\u7a0b\u8c03\u7528 notEmpty.signal() \u65b9\u6cd5\u65f6\uff0c\u4f1a\u628a job \u7ebf\u7a0b\u4ece\u6761\u4ef6\u5bf9\u8c61\u7684\u7b49\u5f85\u961f\u5217\u8f6c\u79fb\u5230 AQS \u7684\u83b7\u53d6\u961f\u5217\u4e0a\uff0c\u8ba9 job \u7ebf\u7a0b\u91cd\u65b0\u83b7\u53d6\u9501\u3001\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<p>\u5f53\u4e0a\u4e00\u4e2a\u6301\u6709\u9501\u7684\u7ebf\u7a0b\u91ca\u653e\u9501\u540e\uff0c\u5b83\u4f1a\u5524\u9192\u4e0b\u4e00\u4e2a\uff0c\u5373\u6267\u884c 662 \u884c\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2021\/12\/aqs-unpark.png\" alt=\"\" width=\"776\" height=\"414\" class=\"alignnone size-full wp-image-2199\" \/><\/p>\n<p><!--more--><\/p>\n<p>\u7ebf\u7a0b\u88ab\u5524\u9192\u540e\u6267\u884c\u4e0b\u9762\u7684\u65b9\u6cd5\uff0c\u95ee\u9898\u662f\u5982\u679c\u88ab\u5524\u9192\u7684\u7ebf\u7a0b\uff08job \u7ebf\u7a0b\uff09\u5df2\u7ecf\u88ab\u7ec8\u6b62\u4e86\uff0c\u5c31\u4e0d\u4f1a\u6267\u884c\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u5bfc\u81f4 job \u7ebf\u7a0b\u7684\u7b49\u5f85\u8282\u70b9\u4e00\u76f4\u5728 head.next \u4f4d\u7f6e\uff0c\u5b83\u4e4b\u540e\u7684\u7ebf\u7a0b\u4e5f\u4e0d\u4f1a\u88ab\u5524\u9192\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2021\/12\/aqs-acquirequeued.png\" alt=\"\" width=\"732\" height=\"488\" class=\"alignnone size-full wp-image-2198\" \/><\/p>\n<p>\u8981\u80fd\u80fd\u660e\u767d\u7684\u662f\uff0cJVM \u91cc Thread \u5b9e\u4f8b\u8fd8\u5728\u3001\u4f46\u5bf9\u5e94\u7684\u64cd\u4f5c\u7cfb\u7edf\u7ebf\u7a0b\u662f\u5df2\u7ec8\u6b62\uff0c\u8fd9\u662f\u53ef\u4ee5\u7684\uff0c\u53ea\u662f\u662f\u4e0d\u6b63\u5e38\u7684\u3002<\/p>\n<p>HikariCP \u4e3a\u4e86\u8fbe\u5230\u9ad8\u6027\u80fd\uff0c\u505a\u4e86\u4e0d\u5c11\u6709\u610f\u601d\u7684\u5b9e\u73b0\uff0c\u901a\u8fc7\u4e00\u4e2a\u5b9a\u5236\u7684\u6570\u636e\u7ed3\u6784 ConcurrentBag \u6765\u7ba1\u7406\u8fde\u63a5\u3002<\/p>\n<pre><code class=\"java\">private final CopyOnWriteArrayList&lt;T&gt; sharedList;\nprivate final ThreadLocal&lt;List&lt;Object&gt;&gt; threadList;\nprivate final SynchronousQueue&lt;T&gt; handoffQueue;\n\npublic T borrow(long timeout, final TimeUnit timeUnit) throws InterruptedException\n{\n  \/\/ \u4f18\u5148\u4ece\u672c\u5730\u961f\u5217\u83b7\u53d6\uff0c\u7ade\u4e89\u5c0f\n  final List&lt;Object&gt; list = threadList.get();\n  for (int i = list.size() - 1; i &gt;= 0; i--) {\n     final Object entry = list.remove(i);\n     @SuppressWarnings(\"unchecked\")\n     final T bagEntry = weakThreadLocals ? ((WeakReference&lt;T&gt;) entry).get() : (T) entry;\n     if (bagEntry != null &amp;&amp; bagEntry.compareAndSet(STATE_NOT_IN_USE, STATE_IN_USE)) {\n        return bagEntry;\n     }\n  }\n\n  \/\/ \u83b7\u53d6\u4e0d\u5230\u518d\u4ece\u5168\u5c40\u961f\u5217\u83b7\u53d6\n  \/\/ \u8bfb\u591a\u5199\u5c11\uff0c\u5168\u5c40\u961f\u5217\u9002\u5408\u7528 COW \u7684\u5b9e\u73b0\u961f\u5217\n  final int waiting = waiters.incrementAndGet();\n  try {\n     for (T bagEntry : sharedList) {\n        if (bagEntry.compareAndSet(STATE_NOT_IN_USE, STATE_IN_USE)) {\n           \/\/ If we may have stolen another waiter's connection, request another bag add.\n           if (waiting &gt; 1) {\n              listener.addBagItem(waiting - 1);\n           }\n           return bagEntry;\n        }\n     }\n\n     listener.addBagItem(waiting);\n\n\/\/ \u8fd8\u83b7\u53d6\u4e0d\u5230\u5219\u5728\u540c\u6b65\u961f\u5217\u4e0a\u7b49\u5f85\u83b7\u53d6\n     timeout = timeUnit.toNanos(timeout);\n     do {\n        final long start = currentTime();\n        final T bagEntry = handoffQueue.poll(timeout, NANOSECONDS);\n        if (bagEntry == null || bagEntry.compareAndSet(STATE_NOT_IN_USE, STATE_IN_USE)) {\n           return bagEntry;\n        }\n\n        timeout -= elapsedNanos(start);\n     } while (timeout &gt; 10_000);\n\n     return null;\n  }\n  finally {\n     waiters.decrementAndGet();\n  }\n}\n<\/code><\/pre>\n<p>\u65b0\u8fde\u63a5\u52a0\u5165\u6c60\u91cc\uff1a<\/p>\n<pre><code class=\"java\">public void add(final T bagEntry)\n{\n   if (closed) {\n      LOGGER.info(\"ConcurrentBag has been closed, ignoring add()\");\n      throw new IllegalStateException(\"ConcurrentBag has been closed, ignoring add()\");\n   }\n\n   sharedList.add(bagEntry);\n\n   \/\/ spin until a thread takes it or none are waiting\n   while (waiters.get() &gt; 0 &amp;&amp; bagEntry.getState() == STATE_NOT_IN_USE &amp;&amp; !handoffQueue.offer(bagEntry)) {\n      Thread.yield();\n   }\n}\n<\/code><\/pre>\n<p>\u5982\u4e0a\uff0c\u65b0\u8fde\u63a5\u9996\u5148\u52a0\u5165\u5230\u5168\u5c40\u961f\u5217\uff0c\u7136\u540e\u770b\u662f\u5426\u6709\u7ebf\u7a0b\u5728\u7b49\u5f85\uff0c\u6709\u5219\u901a\u8fc7 handoffQueue \u8fdb\u884c\u4ea4\u4ed8\u3002<\/p>\n<p>\u53ef\u4ee5\u53d1\u73b0\uff0cHikariCP \u4e0e Druid \u5728\u8fd9\u5757\u7684\u4e0d\u540c\u662f\uff0c\u5b83\u4e0d\u901a\u8fc7 Lock\/Condition \u6765\u534f\u8c03\u521b\u5efa\u8fde\u63a5\u4e0e\u83b7\u53d6\u8fde\u63a5\u7684\u7ebf\u7a0b\u3002<\/p>\n<p>SynchronousQueue \u5e95\u5c42\u7684\u5b9e\u73b0\u662f\u65e0\u9501\u7684\uff0c\u56e0\u6b64\u5373\u4f7f\u51fa\u73b0\u67d0\u4e2a\u7b49\u5f85\u7ebf\u7a0b\u88ab\u7ec8\u6b62\uff0c\u4e5f\u4e0d\u4f1a\u5f71\u54cd\u540e\u7eed\u7684\u7ebf\u7a0b\u3002\u5b83\u662f\u9002\u5408\u4e8e\u8fd9\u79cd\u4f20\u9012\u7684\u573a\u666f\u7684\u3002<\/p>\n<hr \/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong> \u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4e4b\u524d\u7684\u6587\u7ae0\u300a\u8e29\u5751 Druid \u8fde\u63a5\u6c60\u300b\u8bf4\u8e29\u4e86\u5751\uff0c\u540e\u9762\u7ecf\u4eba\u63d0\u9192\uff0c\u53d1\u73b0\u6839\u56e0\u662f\u4e00\u4e2a\u7b49 &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/java\/20220108\/2197\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[122,309,344],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2197"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=2197"}],"version-history":[{"count":3,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2197\/revisions"}],"predecessor-version":[{"id":2203,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2197\/revisions\/2203"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=2197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=2197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=2197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}