{"id":203731,"date":"2025-05-22T13:47:10","date_gmt":"2025-05-22T05:47:10","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203731\/"},"modified":"2025-05-22T13:47:10","modified_gmt":"2025-05-22T05:47:10","slug":"head-first-java%e4%b8%ad%e5%a4%9a%e4%b8%aa%e7%ba%bf%e7%a8%8b%e5%90%8c%e6%97%b6%e5%8f%96%e6%ac%be%ef%bc%8c%e4%b8%ba%e4%bb%80%e4%b9%88%e4%bc%9a%e5%87%ba%e7%8e%b0%e4%bd%99%e9%a2%9d%e4%b8%8d%e8%b6%b3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203731\/","title":{"rendered":"Head First Java\u4e2d\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u53d6\u6b3e\uff0c\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u4f59\u989d\u4e0d\u8db3\u7684\u95ee\u9898\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Head First Java\u4e2d\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u53d6\u6b3e\uff0c\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u4f59\u989d\u4e0d\u8db3\u7684\u95ee\u9898\uff1f<\/h1>\n<p>\u600e\u4e48\u5165\u95e8\u6587\u7ae0\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300aHead First Java\u4e2d\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u53d6\u6b3e\uff0c\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u4f59\u989d\u4e0d\u8db3\u7684\u95ee\u9898\uff1f\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241120\/1732087471673d8eaff1dd0.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>head first java\u4e2d\u7684\u7ebf\u7a0b\u95ee\u9898<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5728\u4ee5\u4e0b\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u7ebf\u7a0b\u6765\u5206\u522b\u6267\u884cryanandmonicajob\u7c7b\u7684run\u65b9\u6cd5\u3002\u6bcf\u4e2a\u7ebf\u7a0b\u5bf9bankaccount\u5bf9\u8c61\u8fdb\u884c\u53d6\u6b3e\u64cd\u4f5c\uff0c\u5e76\u5728\u53d6\u6b3e\u524d\u68c0\u67e5\u8d26\u6237\u4f59\u989d\u662f\u5426\u5927\u4e8e\u7b49\u4e8e\u53d6\u6b3e\u91d1\u989d\u3002\u7136\u800c\uff0c\u8f93\u51fa\u7ed3\u679c\u5374\u4ee4\u4eba\u60ca\u8bb6\u3002<\/p>\n<p><strong>\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>class bankaccount {\n\n    private int balance = 100;\n\n    public int getbalance() {\n        return balance;\n    }\n\n    public void withdraw(int amount) {\n        balance = balance - amount;\n    }\n}\n\npublic class ryanandmonicajob implements runnable {\n\n    private bankaccount account = new bankaccount();\n\n    public static void main(string[] args) {\n        ryanandmonicajob thejob = new ryanandmonicajob();\n        thread one = new thread(thejob);\n        thread two = new thread(thejob);\n        one.setname(\"ryan\");\n        two.setname(\"monica\");\n        one.start();\n        two.start();\n    }\n\n    public void run() {\n        for (int x = 0; x &lt; 10; x++) {\n            makewithdrawal(10);\n            if (account.getbalance() &lt; 0) {\n                system.out.println(\"overdrawn!\");\n            }\n        }\n    }\n\n    private void makewithdrawal(int amount) {\n        string currentthread = thread.currentthread().getname();\n        if (account.getbalance() &gt;= amount) {\n            system.out.println(currentthread + \" is about to withdraw\");\n            try {\n                system.out.println(currentthread + \" is going to sleep\");\n                thread.sleep(500);\n            } catch (interruptedexception ex) {\n                ex.printstacktrace();\n            }\n            system.out.println(currentthread + \" woke up.\");\n            account.withdraw(amount);\n            system.out.println(currentthread + \" completes the withdrawal\");\n        } else {\n            system.out.println(\"sorry, not enough for \" + currentthread);\n        }\n    }\n}<\/pre>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>ryan is about to withdraw\nryan: sorry, not enough for ryan\nmonica: is about to withdraw\n...<\/pre>\n<p><strong>\u95ee\u9898\u539f\u56e0\uff1a<\/strong><\/p>\n<p>\u5f53ryan\u7ebf\u7a0b\u6267\u884cmakewithdrawal\u65b9\u6cd5\u65f6\uff0c\u5b83\u5148\u8f93\u51fa&#8221;ryan is about to withdraw&#8221;\uff0c\u7136\u540e\u8fdb\u5165sleep\u72b6\u6001\u3002\u7136\u800c\uff0c\u8fd9\u5e76\u4e0d\u662f\u50cf\u4f60\u60f3\u8c61\u7684\u90a3\u6837\u4ea4\u51fa\u63a7\u5236\u6743\u7ed9\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u3002\u76f8\u53cd\uff0c\u5728\u8fd9\u4e2asleep\u65f6\u95f4\u5185\uff0cmonica\u7ebf\u7a0b\u4ecd\u7136\u7ee7\u7eed\u6267\u884c\uff0c\u5e76\u4e14\u5df2\u7ecf\u68c0\u6d4b\u5230\u8d26\u6237\u4f59\u989d\u4e0d\u8db3\uff0c\u6240\u4ee5\u8f93\u51fa&#8221;ryan: sorry, not enough for ryan&#8221;\u3002<\/p>\n<p>\u4e00\u65e6ryan\u7ebf\u7a0b\u4ecesleep\u72b6\u6001\u4e2d\u82cf\u9192\uff0c\u5b83\u7ee7\u7eed\u6267\u884cmakewithdrawal\u65b9\u6cd5\u3002\u6b64\u65f6\uff0c\u8d26\u6237\u4f59\u989d\u5df2\u7ecf\u53d1\u751f\u4e86\u53d8\u5316\uff0c\u5e76\u4e14\u4e0d\u518d\u8db3\u4ee5\u652f\u6301\u53d6\u6b3e\u64cd\u4f5c\u3002\u56e0\u6b64\uff0c\u5b83\u8f93\u51fa&#8221;monica is about to withdraw&#8221;\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u4e0d\u540c\u6b65\uff0c\u6211\u4eec\u53ef\u4ee5\u5728makewithdrawal\u65b9\u6cd5\u4e2d\u4f7f\u7528synchronized\u5757\u6765\u786e\u4fdd\u8d26\u6237\u5bf9\u8c61\u7684\u8bbf\u95ee\u662f\u540c\u6b65\u7684\u3002<\/p>\n<pre>private void makeWithdrawal(int amount) {\n    synchronized (account) {\n        String currentThread = Thread.currentThread().getName();\n        if (account.getBalance() &gt;= amount) {\n            System.out.println(currentThread + \" is about to withdraw\");\n            try {\n                System.out.println(currentThread + \" is going to sleep\");\n                Thread.sleep(500);\n            } catch (InterruptedException ex) {\n                ex.printStackTrace();\n            }\n            System.out.println(currentThread + \" woke up.\");\n            account.withdraw(amount);\n            System.out.println(currentThread + \" completes the withdrawal\");\n        } else {\n            System.out.println(\"Sorry, not enough for \" + currentThread);\n        }\n    }\n}<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300aHead First Java\u4e2d\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u53d6\u6b3e\uff0c\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u4f59\u989d\u4e0d\u8db3\u7684\u95ee\u9898\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Head First Java\u4e2d&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203731","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203731","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203731"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203731\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}