{"id":203808,"date":"2025-05-22T12:06:06","date_gmt":"2025-05-22T04:06:06","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203808\/"},"modified":"2025-05-22T12:06:06","modified_gmt":"2025-05-22T04:06:06","slug":"%e3%80%8ahead-first-java%e3%80%8b%e4%b8%ad%e9%93%b6%e8%a1%8c%e8%b4%a6%e6%88%b7%e7%a4%ba%e4%be%8b%ef%bc%9a%e4%b8%ba%e4%bd%95%e5%a4%9a%e4%b8%aa%e7%ba%bf%e7%a8%8b%e6%93%8d%e4%bd%9c%e5%90%8c%e4%b8%80","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203808\/","title":{"rendered":"\u300aHead First Java\u300b\u4e2d\u94f6\u884c\u8d26\u6237\u793a\u4f8b\uff1a\u4e3a\u4f55\u591a\u4e2a\u7ebf\u7a0b\u64cd\u4f5c\u540c\u4e00\u8d26\u6237\u4f1a\u5bfc\u81f4Monica\u7ebf\u7a0b\u4ece\u4e2d\u95f4\u5f00\u59cb\u6267\u884c\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u300aHead First Java\u300b\u4e2d\u94f6\u884c\u8d26\u6237\u793a\u4f8b\uff1a\u4e3a\u4f55\u591a\u4e2a\u7ebf\u7a0b\u64cd\u4f5c\u540c\u4e00\u8d26\u6237\u4f1a\u5bfc\u81f4Monica\u7ebf\u7a0b\u4ece\u4e2d\u95f4\u5f00\u59cb\u6267\u884c\uff1f<\/h1>\n<p>\u6765\u5230\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300a\u300aHead First Java\u300b\u4e2d\u94f6\u884c\u8d26\u6237\u793a\u4f8b\uff1a\u4e3a\u4f55\u591a\u4e2a\u7ebf\u7a0b\u64cd\u4f5c\u540c\u4e00\u8d26\u6237\u4f1a\u5bfc\u81f4Monica\u7ebf\u7a0b\u4ece\u4e2d\u95f4\u5f00\u59cb\u6267\u884c\uff1f\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241125\/173253459967446147b3045.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>java\u5e76\u53d1\u7f16\u7a0b\u95ee\u9898<\/strong><\/p>\n<p>\u5728\u300ahead first java\u300b\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u8003\u5bdf\u4e86bankaccount\u7c7b\u7684\u7ebf\u7a0b\u5e76\u53d1\u95ee\u9898\u3002<\/p>\n<p><strong>\u95ee\u9898\u63cf\u8ff0\uff1a<\/strong><\/p>\n<pre>class bankaccount {\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    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 withdrawl\");\n        } else {\n            system.out.println(\"sorry, not enough for \" + currentthread);\n        }\n    }\n}<\/pre>\n<p>\u8be5\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0cryan\u548cmonica\u7ebf\u7a0b\u540c\u65f6\u5bf9\u540c\u4e00\u4e2abankaccount\u5bf9\u8c61\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u793a\u4f8b\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>Ryan is about to withdraw\nMonica is about to withdraw\nRyan is going to sleep\nMonica is going to sleep\nMonica woke up.\nSorry, not enough for Monica\nRyan woke up.\nRyan completes the withdrawl\nMonica is about to withdraw\n...<\/pre>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u4ee3\u7801\u4e2d\uff0cryan\u7ebf\u7a0b\u5bf9makewithdrawal\u65b9\u6cd5\u8fdb\u884c\u7761\u7720\u64cd\u4f5c\u540e\uff0c\u4e3a\u4ec0\u4e48monica\u7ebf\u7a0b\u4f1a\u4ecemakewithdrawal\u65b9\u6cd5\u4e2d\u95f4\u5f00\u59cb\u6267\u884c\uff0c\u800c\u4e0d\u662f\u4ece\u5934\u5f00\u59cb\u6267\u884c\uff1f<\/p>\n<p><strong>\u89e3\u7b54\uff1a<\/strong><\/p>\n<p>\u8fd9\u662f\u56e0\u4e3ajava\u4e2d\u7ebf\u7a0b\u7684\u8c03\u5ea6\u662f\u57fa\u4e8e\u65f6\u95f4\u7247\u7684\uff0c\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u5230\u4e00\u534a\u65f6\uff0c\u8ba1\u65f6\u5668\u4e2d\u65ad\u8be5\u7ebf\u7a0b\uff0c\u5e76\u5c06\u65f6\u95f4\u7247\u4ea4\u7ed9\u5176\u4ed6\u7ebf\u7a0b\u6267\u884c\u3002\u56e0\u6b64\uff0cmonica\u7ebf\u7a0b\u80fd\u591f\u5728ryan\u7ebf\u7a0b\u7761\u7720\u65f6\u83b7\u5f97\u6267\u884c\u6743\uff0c\u5e76\u4ecemakewithdrawal\u65b9\u6cd5\u4e2d\u95f4\u7684\u90e8\u5206\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<p>\u8be5\u793a\u4f8b\u5c55\u793a\u4e86\u5e76\u53d1\u7f16\u7a0b\u4e2d\u7684\u7ade\u6001\u6761\u4ef6\uff0c\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u65f6\u53ef\u80fd\u5bfc\u81f4\u610f\u5916\u7ed3\u679c\u3002\u89e3\u51b3\u8be5\u95ee\u9898\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u540c\u6b65\u673a\u5236\uff0c\u5982\u9501\u6216\u540c\u6b65\u5668\uff0c\u4ee5\u786e\u4fdd\u5bf9\u5171\u4eab\u8d44\u6e90\u7684\u8bbf\u95ee\u662f\u6392\u4ed6\u7684\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u300aHead First Java\u300b\u4e2d\u94f6\u884c\u8d26\u6237\u793a\u4f8b\uff1a\u4e3a\u4f55\u591a\u4e2a\u7ebf\u7a0b\u64cd\u4f5c\u540c\u4e00\u8d26\u6237\u4f1a\u5bfc\u81f4Monica\u7ebf\u7a0b\u4ece\u4e2d\u95f4\u5f00\u59cb\u6267\u884c\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u300aHead First Java&#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-203808","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203808","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=203808"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203808\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}