{"id":10008111,"date":"2023-08-18T10:43:28","date_gmt":"2023-08-18T02:43:28","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008111"},"modified":"2023-08-18T10:43:28","modified_gmt":"2023-08-18T02:43:28","slug":"%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9ajava-%e7%ba%bf%e7%a8%8b%e6%9c%89%e5%93%aa%e5%87%a0%e7%a7%8d%e7%8a%b6%e6%80%81%ef%bc%9f%e5%ae%83%e4%bb%ac%e4%b9%8b%e9%97%b4%e6%98%af%e6%80%8e%e4%b9%88%e5%88%87","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008111","title":{"rendered":"\u9762\u8bd5\u5b98\uff1aJava \u7ebf\u7a0b\u6709\u54ea\u51e0\u79cd\u72b6\u6001\uff1f\u5b83\u4eec\u4e4b\u95f4\u662f\u600e\u4e48\u5207\u6362\u7684\uff1f"},"content":{"rendered":"<p>\u6765\u6e90\uff1a<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=7JSTTRkhFfGu%2BQRlVA20iw%3D%3D.AB%2BDewCGAsTc46%2FiSmOBYt%2FKtEc2WUsGJO79LnrS10XchYizHyDefdAHiRa%2FD8XJM%2Fwefp0yPVNBcxyimIAb1w%3D%3D\" rel=\"noopener noreferrer\">https:\/\/blog.csdn.net\/limenghua9112\/article\/details\/106975105<\/a><\/p>\n<h2>\u4e3a\u4f55\u8981\u4e86\u89e3Java\u7ebf\u7a0b\u72b6\u6001<\/h2>\n<p>\u7ebf\u7a0b\u662f JVM \u6267\u884c\u4efb\u52a1\u7684\u6700\u5c0f\u5355\u5143\uff0c\u7406\u89e3\u7ebf\u7a0b\u7684\u72b6\u6001\u8f6c\u6362\u662f\u7406\u89e3\u540e\u7eed\u591a\u7ebf\u7a0b\u95ee\u9898\u7684\u57fa\u7840\u3002<\/p>\n<h2>Java\u7ebf\u7a0b\u72b6\u6001\u8f6c\u6362\u56fe<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"639\" height=\"350\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9jhW\" alt=\"\" title=\"\"><\/p>\n<h2>Java\u7ebf\u7a0b\u6709\u54ea\u4e9b\u72b6\u6001\uff1f<\/h2>\n<p>\u5728 JVM \u8fd0\u884c\u4e2d\uff0c\u7ebf\u7a0b\u4e00\u5171\u6709 NEW\u3001RUNNABLE\u3001BLOCKED\u3001WAITING\u3001TIMED_WAITING\u3001TERMINATED \u516d\u79cd\u72b6\u6001\uff0c\u8fd9\u4e9b\u72b6\u6001\u5bf9\u5e94 <code>Thread.State<\/code> \u679a\u4e3e\u7c7b\u4e2d\u7684\u72b6\u6001\u3002<\/p>\n<p>\u63a8\u8350\u4e00\u4e2a\u5f00\u6e90\u514d\u8d39\u7684 Spring Boot \u5b9e\u6218\u9879\u76ee\uff1a<\/p>\n<blockquote><p><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=gxobh0wLjTnK5ukHeYzfxA%3D%3D.10%2F%2FPZc7xoy%2F4NvcVSoJ1eMnDTuxAqIzkH86LXqDfYjjafgOEc%2FPhcbL2Smw5fCdC4LLZRImaUnTMbpw9PYB5w%3D%3D\" rel=\"noopener noreferrer\">https:\/\/github.com\/javastacks\/spring-boot-best-practice<\/a><\/p><\/blockquote>\n<p><strong>Thread.State\u679a\u4e3e\u6e90\u7801\uff1a<\/strong><\/p>\n<p>\u4e3a\u65b9\u4fbf\u9605\u8bfb\uff0c\u5728\u6b64\u53bb\u6389\u4e86\u6587\u6863\u6ce8\u91ca<\/p>\n<pre><code>public enum State {\n NEW,\n RUNNABLE,\n BLOCKED,\n WAITING,\n TIMED_WAITING,\n TERMINATED;\n}<\/code><\/pre>\n<p>\u5728\u7ed9\u5b9a\u7684\u65f6\u95f4\u70b9\uff0c\u7ebf\u7a0b\u53ea\u80fd\u5904\u4e8e\u8fd9\u4e9b\u72b6\u6001\u4e2d\u7684\u4e00\u79cd\u72b6\u6001\u3002\u8fd9\u4e9b\u72b6\u6001\u662f\u4e0d\u53cd\u6620\u4efb\u4f55\u64cd\u4f5c\u7cfb\u7edf\u7ebf\u7a0b\u72b6\u6001\u7684\u865a\u62df\u673a\u72b6\u6001\u3002<\/p>\n<h3>NEW,TERMINATED<\/h3>\n<p>\u8fd9\u4e24\u4e2a\u72b6\u6001\u6bd4\u8f83\u597d\u7406\u89e3\uff0c\u5f53\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u540e\uff0c\u8fd8\u6ca1\u6709\u8c03\u7528start()\u65b9\u6cd5\u65f6\uff0c\u7ebf\u7a0b\u5904\u5728 NEW \u72b6\u6001\uff0c\u7ebf\u7a0b\u5b8c\u6210\u6267\u884c\uff0c\u9000\u51fa\u540e\u53d8\u4e3aTERMINATED\u7ec8\u6b62\u72b6\u6001\u3002<\/p>\n<h3>RUNNABLE<\/h3>\n<p>\u8fd0\u884c Thread \u7684 start \u65b9\u6cd5\u540e\uff0c\u7ebf\u7a0b\u8fdb\u5165 RUNNABLE \u53ef\u8fd0\u884c\u72b6\u6001<\/p>\n<pre><code>\/**\n * \u7a0b\u5e8f\u76ee\u7684\uff1a\u89c2\u5bdf\u7ebf\u7a0b\u7684\u5404\u79cd\u72b6\u6001\n * created at 2020-06-26 19:09\n * @author lerry\n *\/\nclass MyThread extends Thread {\n @Override\n public void run() {\n  System.out.printf(&quot;%s\u7ebf\u7a0b\u8fd0\u884c\/n&quot;, Thread.currentThread().getName());\n }\n}\n\n\/**\n * \u5206\u522b\u89c2\u5bdf\u521b\u5efa\u7ebf\u7a0b\u540e\u3001start()\u540e\u3001\u548c\u7ebf\u7a0b\u9000\u51fa\u540e\u7684\u7ebf\u7a0b\u72b6\u6001\u3002\n * \u5176\u4e2dThread.sleep(50);\u662f\u4e3a\u4e86\u7b49\u5f85\u7ebf\u7a0b\u6267\u884c\u5b8c\n *\/\npublic class ThreadStateDemo {\n public static void main(String[] args) throws InterruptedException {\n  MyThread myThread = new MyThread();\n  System.out.printf(&quot;\u521b\u5efa\u7ebf\u7a0b\u540e\uff0c\u7ebf\u7a0b\u7684\u72b6\u6001\u4e3a\uff1a%s\/n&quot;, myThread.getState());\n  myThread.start();\n  System.out.printf(&quot;\u8c03\u7528start()\u65b9\u6cd5\u540e\u7ebf\u7a0b\u7684\u72b6\u6001\u4e3a\uff1a%s\/n&quot;, myThread.getState());\n  \/\/ \u4f11\u772050\u6beb\u79d2\uff0c\u7b49\u5f85MyThread\u7ebf\u7a0b\u6267\u884c\u5b8c\n  Thread.sleep(50);\n  System.out.printf(&quot;\u518d\u6b21\u6253\u5370\u7ebf\u7a0b\u7684\u72b6\u6001\u4e3a\uff1a%s\/n&quot;, myThread.getState());\n\n }\n}<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre><code>\u521b\u5efa\u7ebf\u7a0b\u540e\uff0c\u7ebf\u7a0b\u7684\u72b6\u6001\u4e3a\uff1aNEW\n\u8c03\u7528start()\u65b9\u6cd5\u540e\u7ebf\u7a0b\u7684\u72b6\u6001\u4e3a\uff1aRUNNABLE\nThread-0\u7ebf\u7a0b\u8fd0\u884c\n\u518d\u6b21\u6253\u5370\u7ebf\u7a0b\u7684\u72b6\u6001\u4e3a\uff1aTERMINATED<\/code><\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u8f93\u51fa\u7ed3\u679c\u7b26\u5408\u9884\u671f\u3002<\/p>\n<ul>\n<li>\u5728\u521a\u521b\u5efa\u5b8c\u7ebf\u7a0b\u540e\uff0c\u72b6\u6001\u4e3aNEW<\/li>\n<li>\u8c03\u7528\u4e86start()\u65b9\u6cd5\u540e\u7ebf\u7a0b\u7684\u72b6\u6001\u53d8\u4e3a\uff1aRUNNABLE\u3002<\/li>\n<li>\u7136\u540e\uff0c\u6211\u4eec\u770b\u5230\u4e86run()\u65b9\u6cd5\u7684\u6267\u884c\uff0c\u8fd9\u4e2a\u6267\u884c\uff0c\u662f\u5728\u4e3b\u7ebf\u7a0bmain\u6253\u5370\u4e86\u8c03\u7528start()\u65b9\u6cd5\u540e\u7ebf\u7a0b\u7684\u72b6\u6001\u4e3a\uff1aRUNNABLE\u8f93\u51fa\u540e\u6267\u884c\u7684\u3002<\/li>\n<li>\u968f\u540e\uff0c\u6211\u4eec\u8ba9main\u7ebf\u7a0b\u4f11\u7720\u4e8650\u6beb\u79d2\uff0c\u7b49\u5f85MyThread\u7ebf\u7a0b\u9000\u51fa<\/li>\n<li>\u6700\u540e\u518d\u6253\u5370MyThread\u7ebf\u7a0b\u7684\u72b6\u6001\uff0c\u4e3aTERMINATED\u3002<\/li>\n<\/ul>\n<h3>BLOCKED<\/h3>\n<p>\u5982\u56fe\u5de6\u4fa7\u6240\u793a\uff0c\u5728\u8fd0\u884c\u6001\u4e2d\u7684\u7ebf\u7a0b\u8fdb\u5165 synchronized \u540c\u6b65\u5757\u6216\u8005\u540c\u6b65\u65b9\u6cd5\u65f6\uff0c\u5982\u679c\u83b7\u53d6\u9501\u5931\u8d25\uff0c\u5219\u4f1a\u8fdb\u5165\u5230 BLOCKED \u72b6\u6001\u3002\u5f53\u83b7\u53d6\u5230\u9501\u540e\uff0c\u4f1a\u4ece BLOCKED \u72b6\u6001\u6062\u590d\u5230\u5c31\u7eea\u72b6\u6001\u3002<\/p>\n<pre><code>import lombok.extern.slf4j.Slf4j;\n\n\/**\n * \u7a0b\u5e8f\u76ee\u7684\uff1a\u89c2\u5bdf\u7ebf\u7a0b\u7684BLOCKED\u72b6\u6001\n * created at 2020-06-26 19:09\n * @author lerry\n *\/\n@Slf4j\npublic class ThreadBlockedStateDemo {\n\n public static void main(String[] args) {\n  Thread threadA = new Thread(() -&gt; method01(), &quot;A-Thread&quot;);\n  Thread threadB = new Thread(() -&gt; method01(), &quot;B-Thread&quot;);\n\n  threadA.start();\n  threadB.start();\n\n  log.info(&quot;\u7ebf\u7a0bA\u7684\u72b6\u6001\u4e3a\uff1a{}&quot;, threadA.getState());\n  log.info(&quot;\u7ebf\u7a0bB\u7684\u72b6\u6001\u4e3a\uff1a{}&quot;, threadB.getState());\n }\n\n \/**\n  * \u505c\u987f10\u6beb\u79d2\u3001\u6a21\u62df\u65b9\u6cd5\u6267\u884c\u8017\u65f6\n  *\/\n public static synchronized void method01() {\n  log.info(&quot;[{}]:\u5f00\u59cb\u6267\u884c\u4e3b\u7ebf\u7a0b\u7684\u65b9\u6cd5&quot;, Thread.currentThread().getName());\n  try {\n   Thread.sleep(10);\n  }\n  catch (InterruptedException e) {\n   e.printStackTrace();\n  }\n  log.info(&quot;[{}]:\u4e3b\u7ebf\u7a0b\u7684\u65b9\u6cd5\u6267\u884c\u5b8c\u6bd5&quot;, Thread.currentThread().getName());\n }\n}<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre><code>2020-06-26 20:32:15.404 [A-Thread] INFO  com.hua.threadtest.state.ThreadBlockedStateDemo - [A-Thread]:\u5f00\u59cb\u6267\u884c\u4e3b\u7ebf\u7a0b\u7684\u65b9\u6cd5\n2020-06-26 20:32:15.404 [main    ] INFO  com.hua.threadtest.state.ThreadBlockedStateDemo - \u7ebf\u7a0bA\u7684\u72b6\u6001\u4e3a\uff1aRUNNABLE\n2020-06-26 20:32:15.407 [main    ] INFO  com.hua.threadtest.state.ThreadBlockedStateDemo - \u7ebf\u7a0bB\u7684\u72b6\u6001\u4e3a\uff1aBLOCKED\n2020-06-26 20:32:15.417 [A-Thread] INFO  com.hua.threadtest.state.ThreadBlockedStateDemo - [A-Thread]:\u4e3b\u7ebf\u7a0b\u7684\u65b9\u6cd5\u6267\u884c\u5b8c\u6bd5\n2020-06-26 20:32:15.418 [B-Thread] INFO  com.hua.threadtest.state.ThreadBlockedStateDemo - [B-Thread]:\u5f00\u59cb\u6267\u884c\u4e3b\u7ebf\u7a0b\u7684\u65b9\u6cd5\n2020-06-26 20:32:15.430 [B-Thread] INFO  com.hua.threadtest.state.ThreadBlockedStateDemo - [B-Thread]:\u4e3b\u7ebf\u7a0b\u7684\u65b9\u6cd5\u6267\u884c\u5b8c\u6bd5<\/code><\/pre>\n<p>A\u7ebf\u7a0b\u4f18\u5148\u83b7\u5f97\u5230\u4e86\u9501\uff0c\u72b6\u6001\u4e3aRUNNABLE,\u8fd9\u65f6\uff0cB\u7ebf\u7a0b\u5904\u4e8eBLOCKED\u72b6\u6001\u3002<\/p>\n<p>\u5f53A\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5\u540e\uff0cB\u7ebf\u7a0b\u6267\u884c\u5bf9\u5e94\u65b9\u6cd5\u3002<\/p>\n<p>\u63a8\u8350\u4e00\u4e2a\u5f00\u6e90\u514d\u8d39\u7684 Spring Boot \u5b9e\u6218\u9879\u76ee\uff1a<\/p>\n<blockquote><p><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=fz0eCxN51%2BkKfqx1gU7vsw%3D%3D.KSsZI4TVK3KqVeYf3Ot0TEYEYmiEzSsdA1jyYfuqgREaBaIluNsN4YP7%2F18%2B6UCV06tUBGkz%2FSsPLPAYasJSTA%3D%3D\" rel=\"noopener noreferrer\">https:\/\/github.com\/javastacks\/spring-boot-best-practice<\/a><\/p><\/blockquote>\n<h3>WAITING,TIMED_WAITING<\/h3>\n<p>\u5982\u56fe\u53f3\u4fa7\u6240\u793a\uff0c\u8fd0\u884c\u4e2d\u7684\u7ebf\u7a0b\u8fd8\u4f1a\u8fdb\u5165\u7b49\u5f85\u72b6\u6001\uff0c\u8fd9\u4e24\u4e2a\u7b49\u5f85\u4e00\u4e2a\u662f\u6709\u8d85\u65f6\u65f6\u95f4\u7684\u7b49\u5f85\uff0c\u4f8b\u5982\u8c03\u7528 <code>Object.wait<\/code>\u3001<code>Thread.join<\/code> \u7b49\uff1b\u53e6\u5916\u4e00\u4e2a\u662f\u65e0\u8d85\u65f6\u7684\u7b49\u5f85\uff0c\u4f8b\u5982\u8c03\u7528 <code>Thread.join<\/code> \u6216\u8005 <code>Locksupport.park<\/code>\u7b49\u3002\u8fd9\u4e24\u79cd\u7b49\u5f85\u90fd\u53ef\u4ee5\u901a\u8fc7 notify \u6216 unpark \u7ed3\u675f\u7b49\u5f85\u72b6\u6001\u5e76\u6062\u590d\u5230\u5c31\u7eea\u72b6\u6001\u3002<\/p>\n<p>\u5b98\u65b9\u6587\u6863\u8bf4\u660e\u4e3a\uff1a<\/p>\n<blockquote><p>A thread in the waiting state is waiting for another thread to perform a particular action. For example, a thread that has called Object.wait() on an object is waiting for another thread to call Object.notify() or Object.notifyAll() on that object. A thread that has called Thread.join() is waiting for a specified thread to terminate.<\/p><\/blockquote>\n<p>\u5904\u4e8e\u7b49\u5f85\u72b6\u6001\u7684\u7ebf\u7a0b\u6b63\u5728\u7b49\u5f85\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u7279\u5b9a\u7684\u64cd\u4f5c\u3002<\/p>\n<pre><code>import lombok.extern.slf4j.Slf4j;\n\n\/**\n * &lt;pre&gt;\n * \u7a0b\u5e8f\u76ee\u7684\uff1a\u89c2\u5bdf\u7ebf\u7a0b\u7684WAITING\u72b6\u6001\n * \u6a21\u62df\uff1a\u53ea\u6709\u4e00\u4e2a\u552e\u7968\u7a97\u53e3\u7684\u552e\u7968\u5385\uff0c\u6709\u4e24\u4e2a\u7c89\u4e1d\u90fd\u60f3\u4e70\u7968\u3002\n * \u5982\u679c\u6ca1\u6709\u7968\uff0c\u4ed6\u4eec\u5c31\u7ee7\u7eed\u7b49\u5f85\u3001\u5982\u679c\u6709\u7968\uff0c\u5219\u4e70\u7968\u3001\u7136\u540e\u79bb\u5f00\u552e\u7968\u5385\u3002\n * \u5176\u4e2d\uff0c\u5de5\u4f5c\u4eba\u5458\u4f1a\u8865\u7968\uff0c\u8865\u7968\u4e4b\u540e\uff0c\u7c89\u4e1d\u5c31\u53ef\u4ee5\u4e70\u5230\u7968\u4e86\u3002\n * &lt;\/pre&gt;\n * created at 2020-06-26 19:09\n * @author lerry\n *\/\n@Slf4j\npublic class ThreadWaitingStateDemo {\n\n public static void main(String[] args) throws InterruptedException {\n  Ticket ticket = new Ticket();\n  Thread threadA = new Thread(() -&gt; {\n   synchronized (ticket) {\n\n    while (ticket.getNum() == 0) {\n     try {\n      ticket.wait();\n     }\n     catch (InterruptedException e) {\n      e.printStackTrace();\n     }\n    }\n    ticket.buy();\n   }\n  }, &quot;\u7c89\u4e1dA&quot;);\n\n  Thread threadB = new Thread(() -&gt; {\n   synchronized (ticket) {\n    while (ticket.getNum() == 0) {\n     try {\n      ticket.wait();\n     }\n     catch (InterruptedException e) {\n      e.printStackTrace();\n     }\n    }\n    ticket.buy();\n   }\n  }, &quot;\u7c89\u4e1dB&quot;);\n\n  threadA.start();\n  threadB.start();\n\n  \/\/ \u786e\u4fddA\u548cB\u7ebf\u7a0b\u90fd\u8fd0\u884c\u8d77\u6765\n  Thread.sleep(10);\n  log.info(&quot;\u7c89\u4e1dA\u7ebf\u7a0b\u7684\u72b6\u6001\u4e3a\uff1a{}&quot;, threadA.getState());\n  log.info(&quot;\u7c89\u4e1dB\u7ebf\u7a0b\u7684\u72b6\u6001\u4e3a\uff1a{}&quot;, threadB.getState());\n\n  Thread employeeThread = new Thread(() -&gt; {\n   synchronized (ticket) {\n    if (ticket.getNum() == 0) {\n     ticket.addTickt();\n     ticket.notifyAll();\n    }\n   }\n  }, &quot;\u8865\u7968\u5458&quot;);\n  employeeThread.start();\n }\n\n}\n\n@Slf4j\nclass Ticket {\n\n \/**\n  * \u7968\u7684\u5f20\u6570\n  *\/\n private int num = 0;\n\n public int getNum() {\n  return num;\n }\n\n public void addTickt() {\n  try {\n   Thread.sleep(2_000);\n  }\n  catch (InterruptedException e) {\n   e.printStackTrace();\n  }\n  log.info(&quot;\u8865\u5145\u7968&quot;);\n  this.num += 2;\n }\n\n \/**\n  * \u505c\u987f10\u6beb\u79d2\u3001\u6a21\u62df\u65b9\u6cd5\u6267\u884c\u8017\u65f6\n  *\/\n public void buy() {\n  log.info(&quot;[{}]:\u8d2d\u4e70\u4e86\u4e00\u5f20\u7968&quot;, Thread.currentThread().getName());\n  log.info(&quot;[{}]:\u9000\u51fa\u552e\u7968\u5385&quot;, Thread.currentThread().getName());\n }\n}<\/code><\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre><code>2020-06-26 21:26:37.938 [main    ] INFO  com.hua.threadtest.state.ThreadWaitingStateDemo - \u7c89\u4e1dA\u7ebf\u7a0b\u7684\u72b6\u6001\u4e3a\uff1aWAITING\n2020-06-26 21:26:37.945 [main    ] INFO  com.hua.threadtest.state.ThreadWaitingStateDemo - \u7c89\u4e1dB\u7ebf\u7a0b\u7684\u72b6\u6001\u4e3a\uff1aWAITING\n2020-06-26 21:26:39.948 [\u8865\u7968\u5458     ] INFO  com.hua.threadtest.state.Ticket - \u8865\u5145\u7968\n2020-06-26 21:26:39.949 [\u7c89\u4e1dB     ] INFO  com.hua.threadtest.state.Ticket - [\u7c89\u4e1dB]:\u8d2d\u4e70\u4e86\u4e00\u5f20\u7968\n2020-06-26 21:26:39.949 [\u7c89\u4e1dB     ] INFO  com.hua.threadtest.state.Ticket - [\u7c89\u4e1dB]:\u9000\u51fa\u552e\u7968\u5385\n2020-06-26 21:26:39.949 [\u7c89\u4e1dA     ] INFO  com.hua.threadtest.state.Ticket - [\u7c89\u4e1dA]:\u8d2d\u4e70\u4e86\u4e00\u5f20\u7968\n2020-06-26 21:26:39.949 [\u7c89\u4e1dA     ] INFO  com.hua.threadtest.state.Ticket - [\u7c89\u4e1dA]:\u9000\u51fa\u552e\u7968\u5385<\/code><\/pre>\n<p>\u5f53\u4fee\u6539<code>ticket.wait();<\/code>\u4e3a<code>ticket.wait(10);<\/code>\u540e\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre><code>2020-06-26 21:27:10.704 [main    ] INFO  com.hua.threadtest.state.ThreadWaitingStateDemo - \u7c89\u4e1dA\u7ebf\u7a0b\u7684\u72b6\u6001\u4e3a\uff1aTIMED_WAITING\n2020-06-26 21:27:10.709 [main    ] INFO  com.hua.threadtest.state.ThreadWaitingStateDemo - \u7c89\u4e1dB\u7ebf\u7a0b\u7684\u72b6\u6001\u4e3a\uff1aTIMED_WAITING\n2020-06-26 21:27:12.714 [\u8865\u7968\u5458     ] INFO  com.hua.threadtest.state.Ticket - \u8865\u5145\u7968\n2020-06-26 21:27:12.714 [\u7c89\u4e1dB     ] INFO  com.hua.threadtest.state.Ticket - [\u7c89\u4e1dB]:\u8d2d\u4e70\u4e86\u4e00\u5f20\u7968\n2020-06-26 21:27:12.714 [\u7c89\u4e1dB     ] INFO  com.hua.threadtest.state.Ticket - [\u7c89\u4e1dB]:\u9000\u51fa\u552e\u7968\u5385\n2020-06-26 21:27:12.715 [\u7c89\u4e1dA     ] INFO  com.hua.threadtest.state.Ticket - [\u7c89\u4e1dA]:\u8d2d\u4e70\u4e86\u4e00\u5f20\u7968\n2020-06-26 21:27:12.715 [\u7c89\u4e1dA     ] INFO  com.hua.threadtest.state.Ticket - [\u7c89\u4e1dA]:\u9000\u51fa\u552e\u7968\u5385<\/code><\/pre>\n<h2>\u5173\u4e8ewait()\u653e\u5728while\u5faa\u73af\u7684\u7591\u95ee<\/h2>\n<p>\u4e3a\u4ec0\u4e48<code>ticket.wait();<\/code>\u8981\u653e\u5728<code>while (ticket.getNum() == 0)<\/code>\u4ee3\u7801\u5757\u4e2d\u5462\uff1f\u65e2\u7136\u8fd9\u884c\u4ee3\u7801\u65f6\u8ba9\u7ebf\u7a0b\u7b49\u5f85\u7740\uff0c\u90a3\u4f7f\u7528if\u4e0d\u5c31\u884c\u4e86\uff1f<\/p>\n<p>\u6211\u4eec\u8bbe\u60f3\u4e00\u4e0b\uff0c\u5982\u679c\u4f7f\u7528if\uff0c\u5219\u5728\u7ebf\u7a0b\u88ab\u5524\u9192\u540e\uff0c\u4f1a\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c\uff0c\u4e0d\u518d\u5224\u65ad\u6761\u4ef6\u662f\u5426\u7b26\u5408\uff0c\u8fd9\u65f6\u8fd8\u662f\u6ca1\u6709\u7968\uff0c\u7c89\u4e1d\u4e5f\u5c31\u8d2d\u4e70\u4e0d\u5230\u7968\u4e86\u3002<\/p>\n<p>\u6211\u4eec\u770b\u4e00\u4e0b<code>Object.wait()<\/code>\u7684\u5b98\u65b9doc\u8bf4\u660e\uff1a<\/p>\n<pre><code>As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:\n           synchronized (obj) {\n               while (&lt;condition does not hold&gt;)\n                   obj.wait();\n               ... \/\/ Perform action appropriate to condition\n           }<\/code><\/pre>\n<p>\u5728\u4e00\u4e2a\u53c2\u6570\u7248\u672c\u4e2d(wait\u65b9\u6cd5)\uff0c\u4e2d\u65ad\u548c\u865a\u5047\u7684\u5524\u9192\u662f\u53ef\u80fd\u7684\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5e94\u8be5\u603b\u662f\u5728\u5faa\u73af\u4e2d\u4f7f\u7528\u3002<\/p>\n<p>\u6211\u4eec\u518d\u7ee7\u7eed\u770b<code>Object.wait(long timeout)<\/code>\u7684\u6587\u6863\u8bf4\u660e\uff1a<\/p>\n<blockquote><p>A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops<\/p><\/blockquote>\n<p>\u7ebf\u7a0b\u4e5f\u53ef\u4ee5\u5728\u6ca1\u6709\u901a\u77e5\u3001\u4e2d\u65ad\u6216\u8d85\u65f6\u7684\u60c5\u51b5\u4e0b\u88ab\u5524\u9192\uff0c\u8fd9\u5c31\u662f\u6240\u8c13\u7684\u5047\u5524\u9192\u3002\u867d\u7136\u8fd9\u79cd\u60c5\u51b5\u5728\u5b9e\u8df5\u4e2d\u5f88\u5c11\u53d1\u751f\uff0c\u4f46\u5e94\u7528\u7a0b\u5e8f\u5fc5\u987b\u901a\u8fc7\u6d4b\u8bd5\u5bfc\u81f4\u7ebf\u7a0b\u88ab\u5524\u9192\u7684\u6761\u4ef6\u6765\u9632\u6b62\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\uff0c\u5982\u679c\u6761\u4ef6\u4e0d\u6ee1\u8db3\uff0c\u5219\u7ee7\u7eed\u7b49\u5f85\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u7b49\u5f85\u5e94\u8be5\u603b\u662f\u5728\u5faa\u73af\u4e2d\u53d1\u751f<\/p>\n<p>\u6240\u4ee5\uff0c\u4e3a\u4e86\u907f\u514d\u5f88\u5c11\u53d1\u751f\u7684\u5047\u5524\u9192\u51fa\u73b0\u65f6\u7a0b\u5e8f\u53d1\u751f\u4e0d\u53ef\u9884\u77e5\u7684\u9519\u8bef\uff0c\u5efa\u8bae\u628await()\u8c03\u7528\u653e\u5728\u5faa\u73af\u8bed\u53e5\u4e2d\u3002\u8fd9\u6837\u5c31\u7b97\u88ab\u5047\u5524\u9192\uff0c\u4e5f\u6709\u6761\u4ef6\u8bed\u53e5\u7684\u9650\u5236\u3002<\/p>\n<p>\u8fd9\u4e5f\u662f\u4e3a\u4f55wait\u8981\u653e\u5728\u5faa\u73af\u8bed\u53e5\u4e2d\u7684\u4e00\u4e2a\u539f\u56e0\u3002<\/p>\n<h3>BLOCKED \u548c WAITING \u72b6\u6001\u7684\u533a\u522b\u548c\u8054\u7cfb<\/h3>\n<p>\u8868\uff1a\u5904\u4e8e\u7b49\u5f85\u72b6\u6001\u7684\u5404\u79cd\u7ec6\u5206\u72b6\u6001\u5bf9\u6bd4<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"172\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9jhX\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u5904\u4e8eBLOCKED\u72b6\u6001\u7684\u7ebf\u7a0b\uff0c\u8fd8\u662f\u5728\u7ade\u4e89\u9501\u7684\uff0c\u4e00\u65e6cpu\u6709\u65f6\u95f4\uff0c\u5b83\u7ade\u4e89\u5230\u4e86\u9501\u3001\u5c31\u4f1a\u6267\u884c\u3002<\/p>\n<p>\u4f46\u662fWAITING\u72b6\u6001\u7684\u7ebf\u7a0b\u5219\u4e0d\u53bb\u7ade\u4e89\u9501\uff0c\u9700\u8981\u7b49\u5f85\u88ab\u52a8\u901a\u77e5\u3001\u6216\u8005\u81ea\u5df1\u5b9a\u7684\u95f9\u949f\uff08\u7b49\u5f85\u65f6\u95f4\uff09\u5230\u4e86\u3001\u518d\u53bb\u7ade\u4e89\u9501\u3002<\/p>\n<p>\u4e00\u56fe\u80dc\u5343\u8a00\uff0c\u5728\u6b64\u5f15\u7528\u4e00\u5f20\u56fd\u5916\u4e00\u4f4d\u5927\u725b\u753b\u7684\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"544\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9jhY\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><strong>\u8fd1\u671f\u70ed\u6587\u63a8\u8350\uff1a<\/strong><\/p>\n<p>1.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=bY1SUc5iSR4m6iDR0x54Yg%3D%3D.IHcnU70lcOnyYaEWdybj%2B3YLJ64bAdlSSJGkfIyXfIY%3D\" rel=\"noopener noreferrer\">1,000+ \u9053 Java\u9762\u8bd5\u9898\u53ca\u7b54\u6848\u6574\u7406(2022\u6700\u65b0\u7248)<\/a><\/p>\n<p>2.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=20UY6suGEWPmLgCpUARO8Q%3D%3D.yp7NJRrbRliFOijUdU26JwNmW5ZdxKyDp4tTxDIsENK3ZgFCifGdcrsrMxquobOF3NFaXjaGO6YDUTeECObjyQ%3D%3D\" rel=\"noopener noreferrer\">\u52b2\u7206\uff01Java \u534f\u7a0b\u8981\u6765\u4e86\u3002\u3002\u3002<\/a><\/p>\n<p>3.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=2q4WFvvmmmjCCHMWlUpneg%3D%3D.nBHvyl9pjVAd8jfnoCirwUaFT57WQM0vp2%2Bp1KEWuN%2F9gNs8ZWL6BXxB3b5EBNG1\" rel=\"noopener noreferrer\">Spring Boot 2.x \u6559\u7a0b\uff0c\u592a\u5168\u4e86\uff01<\/a><\/p>\n<p>4.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=N%2FlC%2BwrC8PKY5G7lt5ub%2FQ%3D%3D.i6u3ZtZ50vumefAsUNEmcUD4iVet95EAJhjhr0qBvR62pHk%2F3CDRQJaO1hAy3DQWZBQAxIsOHcNX0LhFlzKiLw%3D%3D\" rel=\"noopener noreferrer\">\u522b\u518d\u5199\u6ee1\u5c4f\u7684\u7206\u7206\u7206\u70b8\u7c7b\u4e86\uff0c\u8bd5\u8bd5\u88c5\u9970\u5668\u6a21\u5f0f\uff0c\u8fd9\u624d\u662f\u4f18\u96c5\u7684\u65b9\u5f0f\uff01\uff01<\/a><\/p>\n<p>5.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=oIrRpI7ZVTOeQgeD98igpw%3D%3D.1%2F8fWq7jLv8S93JtznLr5IYB5NbxiosF8CcCiZF1d3pgbJJuCoQYlhpV0J%2Bd1b72vV4egRbxJDaXobiU4%2F%2FMGD6%2F%2Fldc%2FBOS9cyTDZlUnLE%3D\" rel=\"noopener noreferrer\">\u300aJava\u5f00\u53d1\u624b\u518c\uff08\u5d69\u5c71\u7248\uff09\u300b\u6700\u65b0\u53d1\u5e03\uff0c\u901f\u901f\u4e0b\u8f7d\uff01<\/a><\/p>\n<p>\u89c9\u5f97\u4e0d\u9519\uff0c\u522b\u5fd8\u4e86\u968f\u624b\u70b9\u8d5e+\u8f6c\u53d1\u54e6\uff01<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044125761\">https:\/\/segmentfault.com\/a\/1190000044125761<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6765\u6e90\uff1ahttps:\/\/blog.csdn.net\/limenghua9112\/article\/details\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008111","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008111"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008111\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}