『壹』 鍥界簿浜у搧W鐏婧愮爜錛氬浗浜х炲櫒W婧愮爜錛氱粷瀵嗙牬瑙o紒
榪戝勾鏉ワ紝鍥戒駭杞浠剁殑鍙戝睍姘村鉤榪借刀涓栫晫鍏堣繘鎶鏈錛岃╁浗浜哄嶆劅鑷璞銆備綔涓轟竴嬈懼囧彈濂借瘎鐨勮蔣浠訛紝鍥界簿浜у搧W鐏婧愮爜鍑鍊熷叾寮哄ぇ鐨勫姛鑳藉拰鍑鴻壊鐨勬ц兘錛屾垚涓轟簡鏁頒互涓囪$敤鎴風殑棣栭夈傚浗綺句駭鍝乄鐏婧愮爜鎽掑純浜嗘櫘閫氳蔣浠剁殑緙虹偣錛岄噰鐢ㄤ簡棰嗗厛鐨勬妧鏈鎵撻狅紝璁╀嬌鐢ㄨ呰兘澶熶韓鍙楀埌鏇村姞欏虹晠銆侀珮鏁堛佸畨鍏ㄧ殑浣跨敤浣撻獙銆
緇濆瘑鐮磋В錛
璁稿氱敤鎴峰湪璐涔板浗綺句駭鍝乄鐏婧愮爜鍚庯紝鍙戠幇杞浠惰澶氭″姞瀵嗭紝瀵艱嚧鏃犳硶姝e父浣跨敤銆傚湪姝わ紝鎴戜滑鍚戝ぇ瀹舵彮縐樹竴浠剁粷瀵嗙殑浜嬫儏錛氬浗綺句駭鍝乄鐏婧愮爜宸茬粡琚鎴愬姛鐮磋В錛佺洰鍓嶏紝鍙鏈夋垜浠鎷ユ湁瀹屾暣涓斿彲鐢ㄧ殑鍥界簿浜у搧W鐏婧愮爜銆傞氳繃鎴戜滑鐨勭牬瑙h蔣浠訛紝鎮ㄥ彲浠ヨ交鏉炬剦蹇鍦頒嬌鐢ㄥ浗綺句駭鍝乄鐏婧愮爜錛屾棤闇鍐嶆媴蹇冭鍔犲瘑鐨勯棶棰樸
寮哄ぇ鐨勫姛鑳
鍥界簿浜у搧W鐏婧愮爜琚瑾変負紲炲櫒錛屽叾涓鏈澶х殑浜鐐瑰氨鏄鍏跺己澶х殑鍔熻兘銆傞氳繃榪欎釜杞浠訛紝鐢ㄦ埛鍙浠ヨ幏寰楀叏鏂逛綅鐨勪俊鎮鏈嶅姟銆傛棤璁烘槸宸ヤ綔鐢熸椿榪樻槸瀛︿範錛屽浗綺句駭鍝乄鐏婧愮爜閮借兘甯鍔╃敤鎴瘋交鏉捐В鍐沖悇縐嶉毦棰樸傚畠鎷ユ湁瓚呭己鐨勫吋瀹規э紝鍙浠ラ傚悎鍚勭嶇郴緇燂紝鍚屾椂瀹岀編瑙e喅鍚勭嶉棶棰橈紝涓虹敤鎴瘋妭鐪佷簡澶ч噺鐨勬椂闂村拰綺懼姏銆
鍑鴻壊鐨勬ц兘
闄や簡寮哄ぇ鐨勫姛鑳藉栵紝鍥界簿浜у搧W鐏婧愮爜榪樹互鍏跺嚭鑹茬殑鎬ц兘鑾峰緱浜嗙敤鎴風殑璁ゅ彲銆傚規瘮甯傞潰涓婂叾浠栬蔣浠訛紝鍥界簿浜у搧W鐏婧愮爜寮鍚閫熷害鏇村揩錛岃繍琛屾洿鍔犳祦鐣呫傚畠鍗犵敤緋葷粺璧勬簮灝戙佺ǔ瀹氭уソ錛屼笉浼氬洜涓轟嬌鐢ㄦ椂闂磋繃闀胯屽嚭鐜板崱欏垮拰宕╂簝絳夐棶棰樸傝繖浜涗紭鐐歸兘涓虹敤鎴風殑宸ヤ綔鍜岀敓媧繪彁渚涗簡鍏ㄦ柟浣嶇殑鏀鎸佸拰淇濋殰銆
緇撹
緇間笂鎵榪幫紝鍥界簿浜у搧W鐏婧愮爜鏄涓嬈懼囧彈濂借瘎鐨勮蔣浠訛紝鍏跺己澶х殑鍔熻兘鍜屽嚭鑹茬殑鎬ц兘璁╂棤鏁扮敤鎴風潃榪楓傝岀粡榪囨垜浠鐨勭牬瑙o紝鐢ㄦ埛鍙浠ヨ交鏉炬剦蹇鍦頒嬌鐢ㄥ浗綺句駭鍝乄鐏婧愮爜錛岄伩鍏嶄簡鍔犲瘑絳夐棶棰樼殑鍥版壈銆傛垜浠鐩鎬俊錛屽浗綺句駭鍝乄鐏婧愮爜灝嗙戶緇涓嶆柇浼樺寲鍜屾敼榪涳紝涓虹敤鎴鋒彁渚涙洿鍔犲嚭鑹茬殑鏈嶅姟銆
『貳』 鐗涢煎晩錛佸紓姝ョ炲櫒錛欳ompletableFuture瀹炵幇鍘熺悊鍜屼嬌鐢ㄥ満鏅
CompletableFuture鏄痡dk1.8寮曞叆鐨勫疄鐜扮被銆傛墿灞曚簡Future鍜孋ompletionStage錛屾槸涓涓鍙浠ュ湪浠誨姟瀹屾垚闃舵佃Е鍙戜竴浜涙搷浣淔uture銆傜畝鍗曠殑鏉ヨ插氨鏄鍙浠ュ疄鐜板紓姝ュ洖璋冦
瀵逛簬jdk1.5鐨凢uture錛岃櫧鐒舵彁渚涗簡寮傛ュ勭悊浠誨姟鐨勮兘鍔涳紝浣嗘槸鑾峰彇緇撴灉鐨勬柟寮忓緢涓嶄紭闆咃紝榪樻槸闇瑕侀氳繃闃誨烇紙鎴栬呰疆璁錛夌殑鏂瑰紡銆傚備綍閬垮厤闃誨炲憿錛熷叾瀹炲氨鏄娉ㄥ唽鍥炶皟銆
涓氱晫緇撳悎瑙傚療鑰呮ā寮忓疄鐜板紓姝ュ洖璋冦備篃灝辨槸褰撲換鍔℃墽琛屽畬鎴愬悗鍘婚氱煡瑙傚療鑰呫傛瘮濡侼etty鐨凜hannelFuture錛屽彲浠ラ氳繃娉ㄥ唽鐩戝惉瀹炵幇寮傛ョ粨鏋滅殑澶勭悊銆
閫氳繃addListener鏂規硶娉ㄥ唽鐩戝惉銆傚傛灉浠誨姟瀹屾垚錛屼細璋冪敤notifyListeners閫氱煡銆
CompletableFuture閫氳繃鎵╁睍Future錛屽紩鍏ュ嚱鏁板紡緙栫▼錛岄氳繃鍥炶皟鐨勬柟寮忓幓澶勭悊緇撴灉銆
CompletableFuture鐨勫姛鑳戒富瑕佷綋鐜板湪浠栫殑CompletionStage銆
鍙浠ュ疄鐜板備笅絳夊姛鑳
娑堣垂鍜岃繍琛岀殑鍖哄埆錛
娑堣垂浣跨敤鎵ц岀粨鏋溿傝繍琛屽垯鍙鏄榪愯岀壒瀹氫換鍔°傚叿浣撳叾浠栧姛鑳藉ぇ瀹跺彲浠ユ牴鎹闇奼傝嚜琛屾煡鐪嬨
榪欓噷涓句釜綆鍗曠殑渚嬪瓙鏉ヤ綋楠屼竴涓嬩粬鐨勫姛鑳姐
鎵ц岀粨鏋
鏍規嵁緇撴灉鎴戜滑鍙浠ョ湅鍒頒細鏈夊簭鎵ц屽瑰簲浠誨姟銆
娉ㄦ剰錛
榪欓噷璇存槑涓涓嬶紝濡傛灉鏄鍚屼竴浠誨姟鐨勪緷璧栦換鍔℃湁澶氫釜錛
涓婇潰鐨勭粨璁烘槸閫氳繃闃呰繪簮浠g爜寰楀埌鐨勩備笅闈㈡垜浠娣卞叆婧愪唬鐮併
鍒涘緩鐨勬柟娉曟湁寰堝氾紝鐢氳嚦鍙浠ョ洿鎺new涓涓銆傛垜浠鏉ョ湅涓涓媠upplyAsync寮傛ュ壋寤虹殑鏂規硶銆
鍏ュ弬Supplier錛屽甫榪斿洖鍊肩殑鍑芥暟銆傚傛灉鏄寮傛ユ柟娉曪紝騫朵笖浼犻掍簡鎵ц屽櫒錛岄偅涔堜細浣跨敤浼犲叆鐨勬墽琛屽櫒鍘繪墽琛屼換鍔°傚惁鍒欓噰鐢ㄥ叕鍏辯殑ForkJoin騫惰岀嚎紼嬫睜錛屽傛灉涓嶆敮鎸佸苟琛岋紝鏂板緩涓涓綰跨▼鍘繪墽琛屻
榪欓噷鎴戜滑闇瑕佹敞鎰廎orkJoin鏄閫氳繃瀹堟姢綰跨▼鍘繪墽琛屼換鍔$殑銆傛墍浠ュ繀欏繪湁闈炲畧鎶ょ嚎紼嬬殑瀛樺湪鎵嶈屻
榪欓噷浼氬壋寤轟竴涓鐢ㄤ簬榪斿洖鐨凜ompletableFuture銆
鐒跺悗鏋勯犱竴涓獮syncSupply錛屽苟灝嗗壋寤虹殑CompletableFuture浣滀負鏋勯犲弬鏁頒紶鍏ャ
閭d箞錛屼換鍔$殑鎵ц屽畬鍏ㄤ緷璧朅syncSupply銆
鍦ㄧ湅postComplete鏂規硶涔嬪墠鎴戜滑鍏堟潵鐪嬩竴涓嬪壋寤轟緷璧栦換鍔$殑閫昏緫銆
涓婇潰鎻愬埌榪囥倀henAcceptAsync鏄鐢ㄦ潵娑堣垂CompletableFuture鐨勩傝ユ柟娉曡皟鐢╱niAcceptStage銆
uniAcceptStage閫昏緫錛
Mark1閫昏緫錛
榪欓噷綆鍗曡翠竴涓嬶紝鍏跺疄mode鏈夊悓姝ュ紓姝ワ紝鍜岃凱浠c傝凱浠d負浜嗛伩鍏嶆棤闄愰掑綊銆
榪欓噷寮鴻皟涓涓媎.uniAccept鏂規硶鐨勭涓変釜鍙傛暟銆
濡傛灉鏄寮傛ヨ皟鐢錛坢ode>0錛夛紝浼犲叆null銆傚惁鍒欎紶鍏this銆
鍖哄埆鐪嬩笅闈浠g爜銆俢涓嶄負null浼氳皟鐢╟.claim鏂規硶銆
claim鏂規硶鏄閫昏緫錛
this鐨剅un浠誨姟濡備笅銆備篃灝辨槸鍦ㄥ紓姝ョ嚎紼嬪悓姝ヨ皟鐢╰ryFire鏂規硶銆傝揪鍒板叾琚寮傛ョ嚎紼嬫墽琛岀殑鐩鐨勩
鐪嬪畬涓婇潰鐨勯昏緫錛屾垜浠鍩烘湰鐞嗚В渚濊禆浠誨姟鐨勯昏緫銆
鍏跺疄灝辨槸鍏堝垽鏂婧愪換鍔℃槸鍚﹀畬鎴愶紝濡傛灉瀹屾垚錛岀洿鎺ュ湪瀵瑰簲綰跨▼鎵ц屼互鏉ヤ換鍔★紙濡傛灉鏄鍚屾ワ紝鍒欏湪褰撳墠綰跨▼澶勭悊錛屽惁鍒欏湪寮傛ョ嚎紼嬪勭悊錛
濡傛灉浠誨姟娌℃湁瀹屾垚錛岀洿鎺ヨ繑鍥烇紝鍥犱負絳変換鍔″畬鎴愪箣鍚庝細閫氳繃postComplete鍘昏Е鍙戣皟鐢ㄤ緷璧栦換鍔°
鍦ㄦ簮浠誨姟瀹屾垚涔嬪悗浼氳皟鐢ㄣ
鍏跺疄閫昏緫寰堢畝鍗曪紝灝辨槸榪浠e爢鏍堢殑渚濊禆浠誨姟銆傝皟鐢╤.tryFire鏂規硶銆侼ESTED灝辨槸涓轟簡閬垮厤閫掑綊姝誨驚鐜銆傚洜涓篎irePost浼氳皟鐢╬ostComplete銆傚傛灉鏄疦ESTED錛屽垯涓嶈皟鐢ㄣ
鍫嗘爤鐨勫唴瀹瑰叾瀹炲氨鏄鍦ㄤ緷璧栦換鍔″壋寤虹殑鏃跺欏姞鍏ヨ繘鍘葷殑銆備笂闈㈡垜浠宸茬粡鎻愬埌榪囥
鍩烘湰涓婅堪婧愮爜宸茬粡鍒嗘瀽浜嗛昏緫銆
鍥犱負娑夊強寮傛ョ瓑鎿嶄綔錛屾垜浠闇瑕佺悊涓涓嬶紙榪欓噷閽堝瑰叏寮傛ヤ換鍔★級錛
涓昏佹槸鑰冭檻浠g爜鐨勫嶇敤銆傛墍浠ラ昏緫鐩稿歸毦鐞嗚В銆
postComplete鏂規硶浼氳婧愪換鍔$嚎紼嬫墽琛屽畬婧愪換鍔″悗璋冪敤銆傚悓鏍蜂篃鍙鑳借渚濊禆浠誨姟綰跨▼鍚庤皟鐢ㄣ
鎵ц屼緷璧栦換鍔$殑鏂規硶涓昏佸氨鏄闈爐ryFire鏂規硶銆傚洜涓鴻繖涓鏂規硶鍙鑳戒細琚澶氱嶄笉鍚岀被鍨嬬嚎紼嬭Е鍙戱紝鎵浠ラ昏緫涔熺粫涓鐐廣傦紙鍏朵粬渚濊禆浠誨姟綰跨▼銆佹簮浠誨姟綰跨▼銆佸綋鍓嶄緷璧栦換鍔$嚎紼嬶級
涓嶅緱涓嶈碊oug Lea鐨勭紪鐮侊紝鐪熺殑鏄鑹烘湳銆備唬鐮佺殑澶嶇敤鎬у叏浣撶幇鍦ㄩ昏緫涓婁簡銆
『叄』 鍗氫箰杈懼埉鐮佸拰婧愮爜鏈変粈涔堝尯鍒錛
婧愮爜灝辯浉褰撲簬鍘熻咃紝鍑哄巶鐨勫寘瑁呮槸浠涔堟牱錛屽埌鎵嬪氨鏄浠涔堟牱錛岃屽埉鐮侀【鍚嶆濅箟錛屼細鎶婂寘瑁呬笂闈㈢殑闃蹭吉鐮侊紝鎵瑰彿錛屽唴鐮佺瓑涓鍒囪兘璇嗗埆鍟嗗搧韜浠界殑婧婧愮紪鐮侀兘鍙鑳戒細琚鍒鎺夈
ps://iknow-pic.cdn.bcebos.com/023b5bb5c9ea15ce1b96ce6da6003af33a87b25d?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto"/>
鍗氫箰杈捐繖涓鍝佺墝鏄鍥戒駭鐨勶紝緇撳悎浜嗙編鍥瓚呭垎瀛姘存潹閰告妧鏈錛屾棗涓嬬殑姘存潹閰擱潰鑶滃緢鍑哄悕錛岃寰堝氫漢縐頒負鈥滃幓闂鍙g炲櫒鈥濄傝秴鍒嗗瓙鎶鏈錛屼嬌姘存潹閰歌兘澶熸憾瑙e苟紼沖畾浜庢按涓錛屽悓鏃惰繕鍏峰囩紦閲婅兘鍔涳紝澧為暱浜嗕綔鐢ㄦ椂闂淬
鍥犱負鏁瘋繃姘存潹閰稿悗錛屼細鍔犲己鐨鑲ゅ瑰叾浠栨垚鍒嗙殑鍚告敹錛屾墍浠ュ敖閲忛伩鍏嶇煭鏃墮棿鐢ㄦ坊鍔犲墏棣欑簿闃茶厫鍓榪囧氱殑鎶よ偆鍝併
浠ュ強鍚鏈夐厭綺劇瓑鏈夋満婧跺墏鐨勪駭鍝侊紝鍏鋒湁紲涚棙銆佺編鐧藉姛鏁堢殑浜у搧錛屽傦細鎺ф補鍨嬫礂闈㈠ザ銆佺涙枒浜у搧銆佸叾浠栫涚棙綾諱駭鍝併佺編鐧芥按銆鐑熼叞鑳銆佺淮A閱囥緇碅閰銆佹灉閰搞佺淮A閱涳紝浠ュ強鍏朵粬鍚姘存潹閰哥殑浜у搧(浣嗘槸闅斿紑涓涓灝忔椂宸﹀彸浣跨敤鏄鍙浠ョ殑)錛屽規槗寮曡搗鍒虹棝銆佸彂綰㈢瓑銆