{"id":1532,"date":"2017-07-03T23:11:38","date_gmt":"2017-07-03T15:11:38","guid":{"rendered":"http:\/\/coderbee.net\/?p=1532"},"modified":"2017-07-03T23:11:38","modified_gmt":"2017-07-03T15:11:38","slug":"thread-runnable-callable","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/java\/20170703\/1532","title":{"rendered":"Thread, Runnable, Callable"},"content":{"rendered":"<p>\u6700\u8fd1\u89c2\u5bdf\u540c\u4e8b\u9762\u8bd5\uff0c\u53d1\u73b0\u4ed6\u5bf9\u95ee\u9898\u7684\u7406\u89e3\u672c\u8eab\u5c31\u6709\u4e0d\u5bf9\uff0c\u8fd9\u6837\u5373\u4f7f\u9762\u8bd5\u8005\u5b8c\u5168\u638c\u63e1\u4e86\u90a3\u4e2a\u95ee\u9898\uff0c\u4e5f\u53ef\u80fd\u7ed9\u4e0d\u4e86\u4ed6\u60f3\u8981\u7684\u56de\u7b54\u3002<\/p>\n<p>\u4ed6\u7684\u95ee\u9898\u662f\uff1aJava \u91cc\u6709\u54ea\u4e9b\u521b\u5efa\u7ebf\u7a0b\u7684\u65b9\u5f0f\uff1f\u4ed6\u671f\u671b\u7684\u7b54\u6848\u662f Thread, Runnable, Callable, Future \u8fd9\u4e9b\u3002<\/p>\n<p>\u8fd9\u4e2a\u7406\u89e3\u662f\u9519\u8bef\u7684\uff0c\u4ed6\u4e5f\u662f\u7f51\u4e0a\u770b\u7684\u3002<\/p>\n<h1>\u4e00\u3001\u7ebf\u7a0b\u4e0e\u4efb\u52a1<\/h1>\n<p>\u7ebf\u7a0b\u53ef\u4ee5\u7406\u89e3\u4e3a <strong>\u53ef\u4ee5\u6267\u884c\u7a0b\u5e8f\u6307\u4ee4\u7684\u673a\u5668<\/strong>\uff0c\u800c\u4efb\u52a1\u5219\u662f<strong>\u8981\u6267\u884c\u7684\u4e00\u6bb5\u6307\u4ee4<\/strong>\u3002<\/p>\n<p>\u5728 Java \u91cc\u7528 <code>java.lang.Thread<\/code> \u7c7b\u6765\u8868\u793a\u7ebf\u7a0b\u3002\u7528 <code>java.lang.Runnable<\/code> \u63a5\u53e3\u8868\u793a\u4efb\u52a1\u3002<\/p>\n<p>\u4efb\u52a1\u521b\u5efa\u51fa\u6765\u540e\u662f\u9700\u8981\u653e\u5230\u7ebf\u7a0b\u4e0a\u53bb\u6267\u884c\u7684\uff0c\u6240\u8c13<em>\u7ebf\u7a0b\u9a71\u52a8\u4efb\u52a1\u6267\u884c<\/em>\u3002<\/p>\n<h3>\u521b\u5efa\u3001\u542f\u52a8\u7ebf\u7a0b<\/h3>\n<p>\u521b\u5efa\u7ebf\u7a0b\u4e00\u822c\u662f\u521b\u5efa\u4e00\u4e2a <code>Thread<\/code> \u7c7b\u7684\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u5728\u521b\u5efa\u65f6\u6307\u5b9a\u4efb\u52a1\uff0c\u4e5f\u53ef\u4ee5\u8986\u5199 <code>void run()<\/code> \u65b9\u6cd5\u6765\u5b9e\u73b0\u4efb\u52a1\u7684\u903b\u8f91\u3002<\/p>\n<p><code>Thread<\/code> \u7c7b\u5b9e\u4f8b\u5316\u540e\u53ea\u662f\u5728 Java \u5806\u91cc\u521b\u5efa\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u5bf9\u8c61\uff0c\u5e76\u6ca1\u6709\u8ddf\u64cd\u4f5c\u7cfb\u7edf\u7684\u7ebf\u7a0b\u5173\u8054\u8d77\u6765\uff0c\u8fd8\u4e0d\u80fd\u771f\u6b63\u6267\u884c\u4efb\u52a1\u3002\u9700\u8981\u8c03\u7528 <code>native void start()<\/code> \u65b9\u6cd5\uff08\u8fd9\u4e2a\u65b9\u6cd5\u5728\u4e0d\u540c\u7684 JDK \u7248\u672c\u91cc\u6709\u6240\u4e0d\u540c\uff09\u3002\u6240\u4ee5\u5982\u679c\u662f\u7ee7\u627f\u4e86 <code>Thread<\/code> \u7c7b\uff0c\u662f\u4e0d\u80fd\u8986\u5199 <code>start<\/code> \u65b9\u6cd5\u7684\uff0c\u5426\u5219\u662f\u6ca1\u6cd5\u542f\u52a8\u7ebf\u7a0b\u7684\u3002<\/p>\n<p><code>start<\/code> \u65b9\u6cd5\u8c03\u7528\u540e\uff0c\u7ebf\u7a0b\u4f1a\u81ea\u52a8\u6267\u884c <code>run<\/code> \u65b9\u6cd5\uff0c <code>run<\/code> \u65b9\u6cd5\u6267\u884c\u5b8c\u6210\u540e\u7ebf\u7a0b\u5c31\u4f1a\u9500\u6bc1\u3002<\/p>\n<h1>\u4e8c\u3001Callable, Future<\/h1>\n<p><code>java.util.concurrent.Callable<\/code> \u8868\u793a\u4e00\u4e2a\u5e26\u8fd4\u56de\u503c\u7684\u4efb\u52a1\uff0c\u662f\u4e00\u79cd\u7279\u6b8a\u70b9\u4efb\u52a1\u3002<\/p>\n<p><code>java.util.concurrent.Future<\/code> \u8868\u793a\u4e00\u4e2a\u53ef\u5728\u672a\u6765\u83b7\u5f97\u8c03\u7528\u7ed3\u679c\u7684\u5b58\u6839\u3002<\/p>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><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>\u6700\u8fd1\u89c2\u5bdf\u540c\u4e8b\u9762\u8bd5\uff0c\u53d1\u73b0\u4ed6\u5bf9\u95ee\u9898\u7684\u7406\u89e3\u672c\u8eab\u5c31\u6709\u4e0d\u5bf9\uff0c\u8fd9\u6837\u5373\u4f7f\u9762\u8bd5\u8005\u5b8c\u5168\u638c\u63e1\u4e86\u90a3\u4e2a\u95ee &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/java\/20170703\/1532\">\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":[276,158,275,274],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1532"}],"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=1532"}],"version-history":[{"count":4,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1532\/revisions"}],"predecessor-version":[{"id":1536,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1532\/revisions\/1536"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}