JDK 11鏂扮壒鎬у涔犲叆闂ㄦ墜鍐岋細涓庢椂淇辫繘锛屽叏闈綋楠孞ava鏂扮壒鎬ц繘闃朵箣鏃?/p>涓€銆丣DK 11寮曞叆鑳屾櫙涓庢柊鐗规€ф杩?/h3>
闅忕潃Java鐢熸€佺郴缁熺殑钃媰鍙戝睍锛孞ava寮€鍙戝洟闃熶笉鏂帹鍑烘柊鐗堟湰锛屽寮鸿瑷€鍔熻兘銆佹彁鍗囨€ц兘銆佹敼鍠勫畨鍏ㄦ€т互鍙婃彁渚涙洿浣崇殑寮€鍙戣€呬綋楠屻€侸DK 11浣滀负Java 8浠ユ潵鐨勪竴涓噸瑕侀噷绋嬬锛屾爣蹇楃潃Java鍦ㄦā鍧楀寲鏀硅繘銆佸苟鍙慉PI鎻愬崌銆佸寮虹殑鏃ユ湡涓庢椂闂碅PI浠ュ強鎬ц兘涓庡畨鍏ㄦ€т紭鍖栨柟闈㈢殑鎸佺画婕旇繘銆傝鐗堟湰铻嶅悎浜嗕紬澶氬垱鏂扮壒鎬э紝鏃ㄥ湪浼樺寲鐜版湁鍔熻兘骞跺紩鍏ヤ竴绯诲垪寮€鍙戣€呮湡寰呯殑鏂板姛鑳斤紝浠ュ簲瀵规棩鏂版湀寮傜殑鎶€鏈幆澧冦€傛繁鍏ョ悊瑙fā鍧楀寲鍗囩骇銆佸苟鍙戞€ц兘鎻愬崌銆佹棩鏈熸椂闂碅PI鏀硅繘绛夋牳蹇冭鐐癸紝骞舵帰绱㈠疄鎴樺簲鐢ㄦ渚嬶紝灏嗗府鍔╁紑鍙戣€呴珮鏁堝簲鐢↗DK 11鏂扮壒鎬т簬椤圭洰涓€傚叧娉ㄦ€ц兘涓庡畨鍏ㄦ€х殑鎻愬崌锛岀‘淇濆簲鐢ㄥ湪鐜颁唬鐜涓殑绋冲畾鎬у拰鏁堢巼銆?/p>浜屻€佹ā鍧楃郴缁熷崌绾э細妯″潡鍖栨敼杩涗笌绠$悊
JDK 11杩涗竴姝ユ帹鍔ㄤ簡妯″潡绯荤粺鐨勬垚鐔熷拰浼樺寲銆傛ā鍧楀寲鑷狫DK 9璧锋垚涓烘牳蹇冨彉闈╀箣涓€锛屾棬鍦ㄨВ鍐冲ぇ鍨嬮」鐩腑鐨勪緷璧栫鐞嗐€佺被璺緞绠$悊鍜屼唬鐮佺粍缁囬毦棰樸€傚湪JDK 11涓紝妯″潡鍖栨敼杩涗富瑕佷綋鐜板湪妯″潡瀵煎叆涓庡鍑虹殑鏇存槑纭€с€佹ā鍧楄矾寰勭殑绠€鍖栦娇鐢ㄧ瓑鏂归潰銆傞€氳繃module-info.java鏂囦欢锛屽紑鍙戣€呰兘鏇存竻鏅板湴瀹氫箟妯″潡鐨勫鍑烘帴鍙e拰渚濊禆锛屼娇妯″潡闂寸殑渚濊禆鍏崇郴鏇村姞娓呮櫚鍙帶銆?/p>
鍒涘缓妯″潡鐨勬楠ら潪甯哥畝鍗曟槑浜嗐€備緥濡傦紝鍦ㄦā鍧楃殑鐩綍涓嬪垱寤簃odule-info.java鏂囦欢锛屽0鏄庢ā鍧椾緷璧栧拰瀵煎嚭鍖呯瓑淇℃伅銆傝繖鏍风殑澹版槑浣垮緱妯″潡涔嬮棿鐨勫紩鐢ㄦ洿鍔犱究鎹风洿瑙傘€?/p>涓夈€佸苟鍙慉PI鎻愬崌锛氬苟鍙戞€ц兘鐨勪紭鍖栦笌鏂扮殑骞跺彂宸ュ叿
JDK 11瀵瑰苟鍙慉PI杩涜浜嗛噸澶ф敼杩涳紝鎻愪緵浜嗘洿楂樻晥銆佹洿瀹夊叏鐨勫苟鍙戝伐鍏峰拰鏂规硶銆俲ava.util.concurrent鍖呬腑鐨勬柊API浠ュ強CompletableFuture鐨勫寮哄姛鑳斤紝浣垮緱寮傛缂栫▼鏇村姞鐩磋楂樻晥銆侸DK 11杩樺紩鍏ヤ簡濡侴uava搴撲腑鐨凢utures绫荤瓑鏂扮殑骞跺彂宸ュ叿锛屼负寮€鍙戣€呮彁渚涗簡鏇寸畝娲併€佹洿寮哄ぇ鐨勫苟鍙戝鐞嗘柟寮忋€傝繖浜涙柊宸ュ叿鍜屾柟娉曟樉钁楁彁鍗囦簡骞跺彂缂栫▼鐨勬晥鐜囧拰瀹夊叏鎬э紝涓哄紑鍙戣€呮彁渚涗簡鏇翠赴瀵岀殑API鏀寔澶氱嚎绋嬪簲鐢ㄥ紑鍙戙€?/p>鍥涖€佸寮虹殑鏃ユ湡涓庢椂闂碅PI锛氱洿瑙傛槗鐢ㄧ殑鏀硅繘
JDK 11鍦ㄦ棩鏈熸椂闂碅PI鏂归潰杩涜浜嗘樉钁楀寮猴紝浣垮緱鏃ユ湡鏃堕棿鎿嶄綔鏇村姞鐩磋鍜屾槗鐢ㄣ€傝繖浜涙敼杩涘噺灏戜簡閿欒骞舵彁楂樹簡浠g爜鐨勫彲璇绘€э紝浣垮紑鍙戣€呰兘鏇磋交鏉惧湴澶勭悊鏃ユ湡鍜屾椂闂寸浉鍏崇殑浠诲姟銆?/p>
浜斻€佹€ц兘涓庡畨鍏ㄦ€ф彁鍗囷細鍏ㄩ潰浼樺寲纭繚搴旂敤绋冲畾鎬т笌鏁堢巼
闄や簡涓婅堪鏍稿績鏀硅繘澶栵紝JDK 11杩樺湪鎬ц兘浼樺寲鍜屽畨鍏ㄦ€ф柟闈㈣繘琛屼簡澶氶」鎻愬崌銆傚寘鎷瀮鍦惧洖鏀剁畻娉曠殑浼樺寲銆佸唴瀛樺畨鍏ㄦ€х殑澧炲己浠ュ強瀵硅€佹棫API鐨勫簾寮冪瓑锛屾棬鍦ㄦ彁楂樺簲鐢ㄧ殑绋冲畾鎬у拰鏁堢巼銆傝繖浜涙敼杩涗负寮€鍙戣€呮彁渚涗簡鏇村己澶с€佹洿瀹夊叏鐨勫簲鐢ㄨ繍琛岀幆澧冦€?/p>
---
瀹炴垬鎺㈢锛氬苟鍙戞€ц兘椋欏崌鐨勭瀵嗘鍣?/p>
璁╂垜浠€氳繃涓€涓緥瀛愶紝鏉ラ鐣ヤ竴涓嬪浣曚娇鐢–ompletableFuture鏉ョ畝鍖栧紓姝ョ紪绋嬬殑榄呭姏锛?/p>
寮曞叆java.util.concurrent鍖呬腑鐨凜ompletableFuture锛屼綘灏嗗紑鍚苟鍙戠紪绋嬬殑鏂扮邯鍏冦€備互涓嬫槸涓€涓娇鐢–ompletableFuture鐨勭ず渚嬶細
```java
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class CompletableFutureExample {
public static void main(String[] args) {
// 寮傛鑾峰彇鐢ㄦ埛鏁版嵁
CompletableFuture
// 妯℃嫙浠庢暟鎹簱鑾峰彇鐢ㄦ埛淇℃伅鐨勫紓姝ユ搷浣?/p>
return "User data fetched";
});
// 寮傛鑾峰彇鍒楄〃鏁版嵁
CompletableFuture
// 妯℃嫙浠庢暟鎹簱鑾峰彇鍒楄〃鐨勫紓姝ユ搷浣?/p>
return "List fetched";
});
try {
// 鑾峰彇缁撴灉骞舵墦鍗?/p>
String userData = fetchUser.get();
String listData = fetchList.get();
System.out.println("User data (async magic): " + userData);
System.out.println("List data (async wonder): " + listData);
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace(); // 澶勭悊寮傚父
}
}
}
```
鐬э紝杩欏氨鏄娇鐢–ompletableFuture绠€鍖栧紓姝ョ紪绋嬬殑榄斿姏鎵€鍦紒瀹冭浣犵殑浠g爜鏇寸畝娲併€佹洿楂樻晥銆傞偅涔堬紝浣犲噯澶囧ソ鎺㈢储JDK 11鐨勬柊澶ч檰浜嗗悧锛熻鎴戜滑涓€璧锋繁鍏ヤ簡瑙DK 11甯︽潵鐨勬€ц兘涓庡畨鍏ㄦ€ч璺冿紒 馃殌鉁? JDK 11鎬ц兘涓庡畨鍏ㄦ€ч璺冩帰绉? 鎬ц兘璺冨崌鐨勭瀵嗘鍣↗DK 11鍦ㄥ瀮鍦惧洖鏀剁畻娉曚笂鏂藉睍浜嗕竴绯诲垪绮惧缁濅鸡鐨勪紭鍖栧姛澶€傚畠寮哄寲浜咷1鍨冨溇鍥炴敹鍣ㄧ殑骞惰鍒嗕唬鏀堕泦锛屾彁鍗囦簡GC鐨勫搷搴旈€熷害鍜屽悶鍚愰噺锛岃浣犵殑搴旂敤绋嬪簭濡傞奔寰楁按锛岀晠娓稿湪楂樻晥鑳界殑娴峰煙銆傪煍モ湪瀹夊叏鎬у崌绾ч噸纾呮潵琚璊DK 11绛戣捣浜嗕竴閬撳潥鍥虹殑瀹夊叏闃茬嚎锛屽鎶楃幇浠e畨鍏ㄥ▉鑳併€傚畠鎻愪緵浜嗘洿涓ユ牸鐨凥TTPS鏀寔锛屾窐姹颁簡鑰佹棫鐨凙PI浠ュ噺灏戝畨鍏ㄦ紡娲烇紝骞舵洿鏂颁簡TLS鍗忚鐗堟湰锛岀‘淇濅綘鐨勫簲鐢ㄧ▼搴忓湪涓€涓潥涓嶅彲鎽х殑瀹夊叏鐜涓繍琛屻€傪煍掟煕★笍 瀹炶返妗堜緥鐤戦毦瑙g瓟 濡備綍宸у搴旂敤JDK 11鏂扮壒鎬э紵鍦ㄥ皢椤圭洰杩佺Щ鍒癑DK 11鏃讹紝浣犻渶瑕佸阀濡欐柦灞曚互涓嬬粷鎶€锛氭鏌ヤ緷璧栵細纭繚鎵€鏈変緷璧栭」閮借兘涓嶫DK 11鎼烘墜鍏辫垶锛屽吋瀹规ā鍧楀寲锛涙ā鍧楀寲杩佺Щ锛氬鐜版湁椤圭洰杩涜妯″潡鍖栨敼閫狅紝閬靛惊JDK 11鐨勬ā鍧楀寲瑙勮寖锛涙€ц兘涓庡畨鍏ㄥ璁★細璇勪及鎬ц兘鍙樺寲鍜屽畨鍏ㄩ闄╋紝閽堝鎬у湴杩涜浼樺寲璋冩暣銆傞亣鍒伴棶棰樻椂锛屼綘鍙互鍒╃敤JDK 11鎻愪緵鐨勯敊璇棩蹇楀拰璋冭瘯宸ュ叿杩涜闂瀹氫綅锛屽悓鏃跺姹傜ぞ鍖鸿祫婧愮殑甯姪锛屽java-developers璁哄潧銆傚弬鑰冨畼鏂笿DK 11鐨勮縼绉诲拰鏇存柊鎸囧崡锛岃幏寰楄缁嗙殑姝ラ鍜屽缓璁€傚浜庢复鏈涙繁鍏ヤ簡瑙DK 11鏂扮壒鎬х殑寮€鍙戣€咃紝鎱曡缃戞彁渚涗簡涓板瘜鐨凧ava瀛︿範璧勬簮銆傝繖閲屾眹鑱氫簡瑙嗛鏁欑▼銆佸疄鎴樻渚嬪拰缂栫▼鎸戞垬锛屽府鍔╁紑鍙戣€呭揩閫熸帉鎻ava鍩虹涓庤繘闃剁煡璇嗐€傞€氳繃閬靛惊杩欎簺鎸囧崡鍜屽疄璺垫渚嬶紝寮€鍙戣€呭皢鑳芥洿濂藉湴鐞嗚В鍜屽簲鐢↗DK 11鐨勬柊鐗规€э紝浠庤€屾彁鍗囧簲鐢ㄧ殑鎬ц兘銆佸畨鍏ㄦ€у拰鍙淮鎶ゆ€с€傝鎴戜滑涓€璧烽┚椹璊DK 11鐨勯瓟鍔涳紝寮€鍚紪绋嬫柊绾厓锛侌煂燄煔€
文章来自《钓虾网小编|www.jnqjk.cn》整理于网络,文章内容不代表本站立场,转载请注明出处。