{"id":35836,"date":"2024-11-26T12:00:50","date_gmt":"2024-11-26T04:00:50","guid":{"rendered":"https:\/\/fwq.ai\/blog\/35836\/"},"modified":"2024-11-26T12:00:50","modified_gmt":"2024-11-26T04:00:50","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:\/\/fwq.ai\/blog\/35836\/","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><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173077550257164.jpg\" class=\"aligncenter\" title=\"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\u63d2\u56fe\" alt=\"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\u63d2\u56fe\" \/><\/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<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fHead 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\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>head first java\u4e2d\u7684\u7ebf\u7a0b\u95ee\u9898 \u95ee\u9898\uff1a \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 \u4ee3\u7801\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b class bankaccount { private int balance = 100; public int getbalance() { return balance; } public void withdraw(int amount) { balance = balance &#8211; amount; } } public class ryanandmonicajob implements runnable { private bankaccount account = new bankaccount(); public static void main(string[] args) { ryanandmonicajob thejob = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-35836","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35836","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=35836"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35836\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=35836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=35836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=35836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}