锘??xml version="1.0" encoding="utf-8" standalone="yes"?>BlogJava-John Jianghttp://www.8634070.com/jiangshachina/a cup of Java, cheers!<br> https://github.com/johnshajiang/blogzh-cnTue, 26 Mar 2019 20:15:08 GMTTue, 26 Mar 2019 20:15:08 GMT60鎺储HTTP/2: 娴佺殑鐘舵?鍘?http://www.8634070.com/jiangshachina/archive/2016/10/08/431871.htmlJohn JiangJohn JiangSat, 08 Oct 2016 13:17:00 GMThttp://www.8634070.com/jiangshachina/archive/2016/10/08/431871.htmlhttp://www.8634070.com/jiangshachina/comments/431871.htmlhttp://www.8634070.com/jiangshachina/archive/2016/10/08/431871.html#Feedback0http://www.8634070.com/jiangshachina/comments/commentRss/431871.htmlhttp://www.8634070.com/jiangshachina/services/trackbacks/431871.html
鎺储HTTP/2: 娴佺殑鐘舵?/span>
鎺储HTTP/2绯诲垪?#27537;绗?#37733;涚瘒鏂囩珷锛岃璇讳簡HTTP/2娴佺殑鐘舵?#38171;?#28000;強鐘舵佷箣闂寸殑杞寲銆?2016.10.09鏈鍚庢洿鏂?

1. 姒傝堪
    HTTP/2?#27537;娴?Stream)鏄?#37832;?#37912;舵佺殑銆傚綋瀹埛绔垨鏈嶅姟鍣?#32468;?#37734;娇鐢煇涓?#23092;?#37720;诲彂閫佹垨鎺敹鐗?#28729;?#29999;?Frame)?#22440;?#23512;鍚?#37911;?#28729;氭?#22249;(Flag)?#27537;甯?#37827;?#38171;?#28028;氬紩璧?#23092;佺殑鐘舵佺殑杞寲銆?a href="https://tools.ietf.org/html/rfc7540">HTTP 2鍗忚瀹氫箟?#27537;娴佺?#33333;?#38171;?#28641;?#28051;?#37813;绀?#38171;?/span>
                         +--------+
                 send PP |        | recv PP
                
,--------|  idle  |--------.
               /         |        |         \
              v          +--------+          v
       +----------+          |           +----------+
       |          |          | send H /  |          |
,------| reserved |          | recv H    | reserved |------.
|      | (local)  |          |           | (remote) |      |
|      +----------+          v           +----------+      |
|          |             +--------+             |          |
|          |     recv ES |        | send ES     |          |
|   send H |     ,-------|  open  |-------.     | recv H   |
|          |    /        |        |        \    |          |
|          v   v         +--------+         v   v          |
|      +----------+          |           +----------+      |
|      |   half   |          |           |   half   |      |
|      |  closed  |          | send R /  |  closed  |      |
|      | (remote) |          | recv R    | (local)  |      |
|      +----------+          |           +----------+      |
|           |                |                 |           |
|           | send ES /      |       recv ES / |           |
|           | send R /       v        send R / |           |
|           | recv R     +--------+   recv R   |           |
| send R /  `----------->|        |<-----------'  send R / |
| recv R                 | closed |               recv R   |
`----------------------->|        |<----------------------'
                         +--------+

   send:   endpoint sends this frame
   recv:   endpoint receives this frame

   H:  HEADERS frame (with implied CONTINUATIONs)
   PP: PUSH_PROMISE frame (with implied CONTINUATIONs)
   ES: END_STREAM flag
   R:  RST_STREAM frame
    鎬荤殑璇?#38171;孒TTP/2涓?#23092;佺殑?#26275;涓?#37922;?#37723;?#37723;?#37832;?#28729;氫箟?#31777;7绉?#37912;舵?#38171;歩dle锛宺eserved (local)锛宺eserved (remote)锛宱pen锛宧alf closed (local)锛宧alf closed (remote)鍜宑losed銆傚綋涓绔彂閫佹垨鎺敹澶撮儴鍧?鐢?#28051;涓狧EADERS/PUSH_PROMISE甯?#37724;?#32497;殢瀹冪殑闆跺埌澶?#28051;狢ONTINUATION甯?#32513;勬?#24878;殑闆?#37722;??#22440;RST_STREAM甯?#38171;屾垨?#23512;鍚?#37832;塃ND_STREAM鏍囩?#21095;殑甯?HEADERS鍜孌ATA)?#31651;鍚?#38171;屽皢鏀瑰彉娴佺殑鐘舵?#37510;?/span>
    娴佺殑鐘舵佸熀浜?#37722;勭?#22172;宸辩殑瑙?#29785;?#37510;?#37922;变簬甯殑浼?#26440;?#28028;?#37832;夌綉缁滃欢杩?#38171;?#37734;?#37722;?#28051;鏃跺埢锛?#28051;?#37722;?#32468;?#29825;?#28051;虹殑娴佺殑鐘舵佸彲鑳?#37828;?#28051;?#37722;岀殑銆傛瘮濡?#38171;屽綋?#24386;閫佺?#23047;鐢?#28051;涓?#28598;?#27996;巌dle鐘舵佺殑娴佸彂閫佷竴涓?#28051;嶅寘鍚END_STREAM鏍囩?#21095;殑HEADERS甯箣鍚?#28028;?#32468;?#37719;?#29825;?#28051;?#29831;?#23092;?#28598;?#27996;open鐘舵?#38171;屼?#21976;鏃?#37818;敹绔?#28751;?#37832;?#23536;楀埌璇HEADERS甯?#38171;?#37813;浠?#37734;?#38317;?#28051;鏃跺埢锛?#37818;敹绔?#28186;濈劧璁?#28051;?#29831;?#23092;佺殑鐘舵?#37828;idle銆?/span>

2. idle
    鎵鏈夌殑娴?#37734;?#37714;涘缓?#31651;鍒?#38318;藉浜巌dle鐘舵?#37510;傚浜巌dle鐘舵佺殑娴?#38171;屽彧?#21393;璁歌鐢?#27996;庡彂閫丠EADERS甯?#38171;屼?#21975;彲浠?#29722;?#37922;?#27996;?#37818;敹HEADERS鍜孭RIORITY甯?#37510;傚湪涓绔娇鐢?#29831;?#37912;舵佺殑娴佸彂閫佹垨鎺敹HEADERS甯箣鍚?#38171;岃绔?#28028;?#29825;?#28051;烘娴佺殑鐘舵?#26462;彉涓open銆?#37818;敹PRIORITY甯?#28051;?#28028;氭?#29808;彉娴佺殑鐘舵?#37510;?/span>
    涓涓猧dle鐘舵佺殑娴佸彲琚?#37721;?#28051;涓?#23092;?#38315;?#26473;囧彂閫?鎺敹PUSH_PROMISE甯?#28103;濈?#27409;潃锛屼娇鍏?#37734;皢?#28533;琚?#37922;?#27996;?#37832;嶅姟鍣?#32468;?#37818;?#38315;?#37510;傝淇濈?#27409;殑娴佺殑鐘舵佸垯浠巌dle?#24393;涓reserved (local/remote)銆?/span>

3. open
    澶?#27996;open鐘舵佺殑娴佸彲琚?#37922;?#27996;庡彂閫?#28000;讳?#26336;?#35826;?#23340;殑甯?#37510;備娇鐢?#29831;?#37912;舵佺殑娴?#37720;诲彂閫?鎺敹?#23512;鍚?#37832;塃ND_STREAM鏍囩?#21095;殑甯?HEADERS鍜孌ATA)?#31651;鍚?#38171;?#28028;氫娇璇?#23092;佺殑鐘舵佸彉鎴恏alf closed (local/remote)銆備娇鐢open鐘舵佺殑娴佸彂閫佹垨鎺敹RST_STREAM甯箣鍚?#38171;屽垯浼氫娇瀹冪殑鐘舵?#26462;彉涓篶losed銆?/span>

4. half closed (local/remote)
    鐘舵乭alf closed (local)涓巋alf closed (remote)涓殑local涓巖emote?#27537;鍖?#37714;?#38171;?#28729;屽叏鏄熀浜?#37722;勭?#22172;宸辩殑瑙?#29785;?#37510;傚浜?#37722;?#28051;涓?#23092;佺殑涓?#32468;?#38171;?#28641;傛灉涓绔?#29825;?#28051;?#26473;?#28051;?#23092;佺殑鐘舵?#37828;half closed (local)锛?#38317;?#28052;?#37721;?#28051;绔?#37721;?#38003;?#29825;?#28051;?#26473;?#28051;?#23092;佺殑鐘舵?#37828;half closed (remote)銆?/span>
    澶?#27996;巋alf closed (local)鐘舵佺殑娴佸彧鑳?#29722;?#37922;?#27996;庡彂閫乄INDOW_UPDATE锛孭RIORITY鍜孯ST_STREAM甯?#38171;屼?#21975;彲浠?#29722;?#37922;?#27996;?#37818;敹浠讳?#26336;?#35826;?#23340;殑甯?#37510;傜浉瀵瑰簲鍦?#38171;?#28598;?#27996;巋alf closed (remote)鐘舵佺殑娴佸彧鑳?#29722;?#37922;?#27996;?#37818;敹WINDOW_UPDATE锛孭RIORITY鍜孯ST_STREAM甯?#38171;屼?#21975;彲浠?#29722;?#37922;?#27996;庡彂閫?#28000;讳?#26336;?#35826;?#23340;殑甯?#37510;?/span>

5. reserved (local/remote)
    涓巋alf closed (local/remote)鐘舵佺浉浼?#38171;宺eserved (local/remote)鐘舵佷腑?#27537;local涓巖emote涔?#37828;熀浜?#23092;佷袱绔?#37722;勮嚜?#27537;瑙?#29785;?#37510;傛洿鍏蜂綋?#27537;鏄?#38171;?#37832;嶅姟鍣?#32468;彂閫丳USH_PROMISE?#30370;涓涓猧dle鐘舵佺殑娴佷?#28616;?#27409;潃浠?#37922;?#27996;?#37832;潵?#27537;鎺?#38315;?#38171;屽苟瑙嗚繖涓?#29722;?#28103;濈?#27409;殑娴佺殑鐘舵佷负reserved (local)锛岃?#28729;埛绔?#37714;?#29785;嗚繖涓?#23092;佺殑鐘舵佷负reserved (remote)銆?/span>
    鏈嶅姟鍣?#32468;娇鐢reserved (local)鐘舵佺殑娴?#37722;?#28729;埛绔彂閫丠EADERS甯?#37510;?#29831;HEADERS甯?#28751;?#37828;?#37832;嶅姟鍣?#32468;?#37818;?#38315;?Server Push)涓?#29722;?#37818;?#38315;佺殑鍝嶅簲?#27537;澶撮儴銆傚綋?#24386;閫佷簡HEADERS甯箣鍚?#38171;?#37832;嶅姟鍣?#32468;皢瑙?#29831;?#23092;佺殑鐘舵佷负half closed (remote)銆?/span>
?#27977;?#31794;鍦?#38171;?#28729;埛绔?#38315;?#26473;reserved (remote)鐘舵佺殑娴?#37818;敹?#22476;鏈嶅姟鍣?#32468;?#37818;?#38315;佺殑鍝嶅簲?#27537;澶撮儴锛岀劧鍚?#28028;?#29785;?#29831;?#23092;佺殑鐘舵佷负half closed (local)銆?/span>
    鎵睍涓涓?#38171;?#37832;嶅姟鍣?#32468;?#37818;?#38315;佷腑琚?#28103;濈?#27409;殑娴佺殑鐘舵?#37734;彉涓篽alf closed(local/remote)?#31651;鍚庢?#23941;彲鑳?#29722;?#37922;?#27996;?#37818;敹/?#24386;閫佽鎺?#38315;佺殑鍝嶅簲?#27537;?#32139;?#20788;锛?#28052;熷氨鏄疍ATA甯?#37510;?/span>

6. closed
    ?#32139;涓绔娇鐢?#28051;涓?#23092;佸彂閫佹垨鎺敹?#22476;RST_STREAM甯?#38171;屾垨閫?#26473;?#37912;舵佷负half closed (local/remote)?#27537;娴?#37818;敹/?#24386;閫佸寘鍚?#37832;塃ND_STREAM鏍囩?#21095;殑甯箣鍚?#38171;?#38318;?#28028;?#29785;嗚繖涓?#23092;佺殑鐘舵佷负closed銆?/span>
    closed鐘舵侀?#21229;?#34425;潃娴佺殑缁?#32513;?#38171;?#28598;?#27996;?#29831;?#37912;舵佺殑娴佸?#21975;彧鑳藉彂閫佹垨鎺敹PRIORITY甯?#37510;備絾鏈?#28051;涓?#37911;?#28186;?#37510;傚嵆锛?#28641;傛灉閫?#26473;囦娇鐢half closed (local/remote)鐘舵佺殑娴?#37720;?#37818;?#37824;舵垨?#24386;閫佸寘鍚?#37832;塃ND_STREAM鏍囩?#21095;殑甯?HEADERS?#22440;DATA)锛?#28000;娇璇?#23092;佺殑鐘舵佸彉涓篶losed锛?#38317;?#28052;?#37734;?#23005;箣鍚庣殑杈冪煭鏃?#38338;?#37712;?#38171;?#28000;嶇?#36346;彲浠?#37818;敹WINDOW_UPDATE?#22440;RST_STREAM甯?#37510;?/span>


John Jiang 2016-10-08 21:17 ?#24386;琛?#29831;?#29825;?/a>
]]>
鎺储HTTP/2: HPACK鍗忚绠?#22570;(鍘?http://www.8634070.com/jiangshachina/archive/2016/09/24/431837.htmlJohn JiangJohn JiangSat, 24 Sep 2016 12:29:00 GMThttp://www.8634070.com/jiangshachina/archive/2016/09/24/431837.htmlhttp://www.8634070.com/jiangshachina/comments/431837.htmlhttp://www.8634070.com/jiangshachina/archive/2016/09/24/431837.html#Feedback0http://www.8634070.com/jiangshachina/comments/commentRss/431837.htmlhttp://www.8634070.com/jiangshachina/services/trackbacks/431837.html
鎺储HTTP/2: HPACK鍗忚绠?#22570;
鎺储HTTP/2绯诲垪?#27537;绗?#28051;?#30226;鏂囩珷宸?#32513;?#28000;?#32513;嶄簡HTTP 2鍗忚锛?#37832;?#37826;?#37714;欏皢绠?#22570;鐢?#27996;嶩TTP/2澶撮儴鍘嬬缉?#27537;HPACK鍗忚銆?2016.10.01鏈鍚庢洿鏂?

1. ?#29056;鏈?#37720;熺悊
    HPACK澶撮儴鍘嬬缉?#27537;?#29056;鏈?#37720;熺?#21975;氨鏄娇鐢储?#32041;琛?#37724;?a href="https://en.wikipedia.org/wiki/Huffman_coding">Huffman缂栫爜銆傚湪鍘嬬缉(缂栫爜)涓?#29785;?#37720;?瑙爜)杩囩锛屽彲?#30370;?#23514;瀹氱殑澶撮儴瀛楁(?#23512;鍚?#28699;楁鍚?#28051;庡?#26945;鍊?瀛樺偍鍦储?#32041;琛?#28051;?#37510;傜储?#32041;琛?#28051;殑姣?#28051;涓?#37833;?#37929;?#37922;辩储?#32041;(涓涓暣鏁?锛屽?#26945;鍚嶅拰瀛楁鍊?#32513;勬垚銆傚浜庡瓨鍦储?#32041;琛?#28051;殑澶撮儴瀛楁锛?#37734;?#32514;栫?#20345;?#36346;彲浠?#28000;呬娇鐢储?#32041;浣?#28051;?#29831;?#28699;楁?#30005;殑浠?#29723;?#38171;?#37734;?#29785;?#37934;佹椂閫?#26473;?#29831;储?#32041;浠庤涓?#37836;?#37813;?#37713;?#28725;瑰簲?#27537;瀛楁銆傚浜庡叾瀹冪殑瀛楃涓?#38171;屽垯?#24434;浠娇鐢Huffman缂栫爜杩?#29723;?#37720;嬬缉銆?br /> 1.1 ?#20648;?#32041;琛?/strong>
    ?#20648;?#32041;琛?#37922;?#38344;?#37804;佽涓庡姩鎬佽缁勬垚銆?#38344;?#37804;佽鐢盚PACK鍗忚棰?#28729;氫箟?#27537;61涓?#29999;?#37922;殑澶撮儴瀛楁缁勬垚锛屽叾涓?#28598;儴鍒嗗?#26945;?#30005;殑鍊?#28051;虹銆?#38344;?#37804;佽鏄?#37721;?#29831;荤殑锛屽叾涓殑鏉?#37929;強鍏?#28003;嶇疆鍧?#28051;嶅彲?#27967;鏀?#37510;侶PACK鍗忚涓殑闄勫綍A?#22442;鍑轰簡鍏儴?#27537;闈?#37804;佽鏉?#37929;?#37510;傚姩鎬佽涔?#37922;?#28051;绯诲垪澶撮儴瀛楁缁勬垚锛屼?#21975;叾涓殑鍏?#32497;?#28051;?#37733;?#28729;?#38171;?#37734;?#28729;?#38340;?#37823;?#28003;?#28051;彲浠彃鍏?#37826;扮殑鏉?#37929;?#38171;?#28052;熷厑璁?#37714;?#38340;?#23480;?#37832;夌殑鏉?#37929;?#37510;?br />     HPACK鍗忚瑕佹眰闈?#37804;佽涓庡姩鎬佽鍚堝苟鍦?#37722;?#28051;涓?#28699;樺偍绌?#38338;?#28051;?#38171;屽叾涓?#38344;?#37804;佽?#30086;浜庡?#23945;儴锛屽姩鎬佽绱殢鍏?#37722;?#37510;?#38317;?#28052;?#37734;暣涓储?#32041;琛?#32460;?#38338;?#28051;?#38171;屽姩鎬佽?#27537;绗?#28051;涓?#37833;?#37929;殑?#20648;?#32041;?#30370;鏄?2銆傚姩鎬佽?#27537;缁?#37814;?#37720;熷垯鏄?#37711;?#26473;涘?#22557;嚭(FIFO)銆傚綋鍚?#37716;?#37804;佽涓?#28583;?#37716;?#37833;?#37929;?#37827;?#38171;屽皢鎬?#37828;?#28000;庣62浣嶆彃鍏?#38171;?#37720;?#37832;夌殑鏉?#37929;?#28751;嗗叏?#20788;鍚?#37721;?#32457;诲姩涓涓?#28003;嶇疆銆傚綋浠庡姩鎬佽涓?#37714;?#38340;?#37833;?#37929;?#37827;?#38171;屽皢鎬?#37828;?#28000;?#37832;鍚?#28051;浣?#26473;?#29723;屽垹闄?#37510;?br />     铏?#29831;?#38171;?#37719;忚瑕佹?#20634;皢闈?#37804;佽涓庡姩鎬佽鍚堝苟鍦?#28051;璧?#38171;屼?#21978;繖鍙?#37828;?#38315;?#26440;?#28051;婄殑瑕佹眰銆傚彧瑕佸姩鎬佽?#27537;?#20648;?#32041;鏄?#28000;?2寮濮?#38171;?#38317;?#28052;?#37722;?#28051;?#28729;炵幇?#24434;浠?#37837;规嵁?#22172;宸辩殑鍠滃?#20511;嚜鐢?#37734;颁娇鐢?#28699;樺偍鏁版嵁缁?#37835;?#37510;傛瘮濡?#38171;屽彲浠皢闈?#37804;佽鍗曠嫭鏀?#37734;?#28051;涓?#28051;嶅彲?#24393;?#27537;鏁扮粍涓?#38171;岃屽姩鎬佽鐢?#37721;?#28051;涓摼琛?#26473;?#29723;屽?#27194;偍锛岃繖鏍峰彲鑳?#28028;氫究浜庢彃鍏?#37724;屽垹闄?#37833;?#37929;?#37510;傚彧涓?#26473;?#38171;岃繖涓摼琛?#28051;?#37711;?#32497;犵殑涓嬫爣涓庡姩鎬佽涓?#37833;?#37929;殑?#20648;?#32041;?#31651;闂寸浉宸?2銆?br />     (鍔?#37804;??#20648;?#32041;琛?#28051;殑鏉?#37929;厑璁搁?#23941;銆?br /> 1.2 Huffman缂栫爜
    Huffman缂栫爜鏄?#28051;绉?#37922;?#27996;庢?#29363;?#29112;?#29256;嵁鍘嬬缉?#27537;鏉冭矾寰勭?#26667;?#20346;?#26945;硶銆傚湪?#23047;鐢?#29831;?#32480;楁硶鏃?#38171;岄渶瑕佷竴?#32054;鎵鏈?#29722;?#32514;栫?#20344;瓧绗殑鏉冮噸(鍑虹幇棰戠巼)浠?#37934;佽銆傚湪瀵瑰閲忕殑HTTP澶撮儴鏍?#37832;?#26473;?#29723;?#32513;?#29825;箣鍚?#38171;屽?#26944;?#36720;簡涓浠?#38315;?#37922;?#27996;嶩PACK?#27537;Huffman浠?#37934;佽锛?#37922;?#37719;忚涓殑闄勫綍B?#22442;鍑?#37510;?br />
    蹇呴?#32472;敞鎰忕殑鏄?#38171;孒PACK鍗忚骞?#28051;?#29781;佹眰璇?#37719;忚?#27537;瀹炵幇涓瀹氳?#20343;娇鐢储?#32041;琛?#38171;?#37719;?#28186;挎煇涓?#28699;楁宸?#32513;?#28699;?#37734;?#27996;庣储?#32041;琛?#28051;簡銆傝?#28051;?#28052;?#28051;?#29781;佹眰涓瀹氳?#20344;?#29808;瓧绗?#28051;?#28729;?#37826;紿uffman鍘嬬缉銆備?#29111;氨鏄?#29831;?#38171;岀?#21978;涓?#38171;?#37734;?#32514;栫?#20345;?#36346;彲浠?#28051;嶅?#29808;?#25774;儴瀛楁杩?#29723;?#28000;讳綍褰?#23534;忕殑鍘嬬缉锛岃屽彧?#28214;?#30370;鎵鏈夌殑瀛楃杞寲鎴?#28699;?#38010;傚舰寮?#37510;?br />
2. ?#29056;鏈?#37825;版嵁绫诲瀷琛?#32448;烘硶
    HPACK鍗忚?#23047;鐢殑?#29056;鏈?#37825;版嵁绫诲?#23338;彧鏈?#28051;?#32457;?#38171;氭暣鏁?#38171;涘瓧绗?#28051;?#37510;?#29831;?#37719;忚?#23047;鐢暣鏁?#37720;?#29723;?#32448;虹储?#32041;鍜屽瓧绗?#28051;茬殑闀垮害銆傚?#25774;儴瀛楁鍚嶅拰鍊?#28051;?#37713;虹?#25198;殑鏁?#28699;?#38171;屽彧浼氳?#32139;浣滃瓧绗?#28051;?#26473;?#29723;?#28598;勭悊銆?br /> 2.1 ?#26275;鏁?#29723;?#32448;烘硶
    HPACK鍦?#29723;?#32448;烘暣鏁?#37827;跺苟涓?#37828;?#37814;?#28729;冪畝鍗曠殑杞?#37817;?#37812;?#27996;岃繘鍒跺舰寮?#37510;?#37733;?#28051;篐PACK甯?#37832;?#23011;?#28051;涓暣鏁扮殑琛?#32448;?#38003;藉浠庢煇涓??#30254;鐗?#28003;嶅瓧鑺?octet锛?#28051;?#37826;?#28751;嗗?#21057;畝鍐?#28051;?瀛?#38010;?)涓殑浠讳綍涓涓瘮鐗?#28003;嶅紑濮?#38171;屼絾鎬?#37828;?#29781;?#37734;煇涓?#28699;?#38010;傜殑鏈鍚?#28051;涓瘮鐗?#28003;嶇粨鏉?#37510;傛瘮濡傝绀?27锛岃瀹?#28000;庡瓧鑺傜殑绗?#28051;涓瘮鐗?#28003;嶅紑濮嬪鍏?#38171;岃偗瀹?#28028;?#37734;?#37832;鍚?#28051;涓瘮鐗?#28003;嶇粨鏉?#38171;?#28641;?#28051;?#37733;?#37813;绀?#38171;?br />
  0   1   2   3   4   5   6   7
+---+---+---+---+---+---+---+---+
| 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
+---+---+---+---+---+---+---+---+
濡傛灉绗?#28051;涓瘮鐗?#28003;?#29722;?#37711;?#28729;?#37706;?#37719;?#37922;?鐢??"浠?#29723;?锛屽彧鑳?#28000;庣浜?#28051;瘮鐗?#28003;嶅紑濮嬪鍏?#37723;?#38171;?#32513;撴灉渚濈?#36346;彧?#28214;瑕佷竴涓?#28699;?#38010;?#38171;?#28641;?#28051;?#37813;绀?#38171;?br />
  0   1   2   3   4   5   6   7
+---+---+---+---+---+---+---+---+
| ? | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
+---+---+---+---+---+---+---+---+
浣?#28641;傛灉鏄?#28000;庣涓?#28051;瘮鐗?#28003;嶅紑濮嬪鍏?#37723;?#38171;?#26473;?#37827;?#28028;氬彂?#24135;涓涓?#28699;?#38010;傚凡缁?#28051;嶅?#29110;簡锛屽?#21620;瑕佺浜?#28051;?#28699;?#38010;?#37510;備?#21978;兘鍚?#29723;?#32448;?#37812;?#28641;?#28051;嬪舰寮?#37723;?#38171;?br />
  0   1   2   3   4   5   6   7
+---+---+---+---+---+---+---+---+
| ? | ? | 1 | 1 | 1 | 1 | 1 | 1 |
+---+---+---+-------------------+
| 1 | ? | ? | ? | ? | ? | ? | ? |
+---+---+---+---+---+---+---+---+
杩?#37828;剧劧涓嶇鍚HPACK鍗忚?#27537;瑕佹眰锛?#37733;?#28051;?#28729;?#29999;?#37832;?#38003;藉鍦煇涓?#28699;?#38010;傜殑鏈鍚?#28051;涓瘮鐗?#28003;嶇粨鏉?#26473;?#28051;?#29723;?#32448;?#37510;?#28051;?#26440;惧埌杩?#28051;鐩殑锛孒PACK鍗忚璁?#29825;?#37713;轰簡涓绉嶅涓?#37733;?#37813;绀虹殑琛?#32448;烘硶锛?br />
  0   1   2   3   4   5   6   7
+---+---+---+---+---+---+---+---+
| ? | ? | 1 | 1   1   1   1   1 |
+---+---+---+-------------------+
| 1 |    Value-(2^N-1) LSB      |
+---+---------------------------+
               ...
+---+---------------------------+
| 0 |    Value-(2^N-1) MSB      |
+---+---------------------------+
绗?#28051;涓?#28699;?#38010;?#28051;?#38003;藉琚?#37922;潵濉?#37711;呮暣鏁?#29723;?#32448;?#28003;嶇殑?#30254;鐗?#28003;?#37825;?涓?#37733;?#28051;殑涓?)琚?#32457;?#28051;簆refix銆?#28051;?#38344;?#37828;?#29831;?#29723;?#32448;烘?#26336;殑Java璇?#29815;瀹炵幇锛?br />
public void encodeInteger(int value, int prefix) {
    
if (value >> prefix <= 0) {
        printBinary(value);
    } 
else {
        
int number = (1 << prefix) - 1;
        printBinary(number);
        
for (value -= number; value >= 128; value /= 128) {
            printBinary(value 
% 128 + 128);
        }
        printBinary(value);
    }
}

private void printBinary(int value) {
    System.out.println(String.format(
"%8s", Integer.toBinaryString(value)).replace(" ""0"));
}
鏍规嵁涓婅堪绠楁?#26334;彲?#29025;锛屽綋prefix涓?鏃?#38171;?27?#27537;琛?#32448;烘硶濡?#28051;?#37733;?#37813;绀?#38171;?br />
  0   1   2   3   4   5   6   7
+---+---+---+---+---+---+---+---+
| ? | ? | 1 | 1 | 1 | 1 | 1 | 1 |
+---+---+---+-------------------+
| 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+-------------------+
2.2 瀛楃涓?#29723;?#32448;烘硶
  0   1   2   3   4   5   6   7
+---+---+---+---+---+---+---+---+
| H |    String Length (7+)     |
+---+---------------------------+
|  String Data (Length octets)  |
+-------------------------------+
HPACK鍗忚?#23047;鐢?#28051;?#37733;惧睍绀虹殑琛?#32448;烘硶锛?#28729;?#37922;?#28051;夐儴鍒?#32513;勬垚锛?br /> [1]Huffman?#29219;蹇?#38171;岃绀?#29831;?#28699;楃涓?#37828;?#37722;?#28051;篐uffman缂栫爜锛?#37719;?#37922;?#28051;涓瘮鐗?#28003;?#37510;?br /> [2]瀛楃涓?#38336;垮害锛?#28051;涓娇鐢?.1鑺?#37813;?#22570;鏂规?#26337;绀虹殑?#26275;鏁?#38171;屽叾涓璸refix涓?銆?br /> [3]瀛楃涓?#37706;?#37510;傝嫢Huffman?#29219;蹇?#28051;?锛岃鍊?#28751;?#37828;?#37720;熷?#23338;瓧绗?#28051;茬殑瀛?#38010;?#38171;?#37722;?#37714;?#29831;?#37706;?#37828;?#32513;廐uffman缂栫爜杩囩殑鏁版嵁銆?#37922;变簬缁廐uffman缂栫爜杩囩殑鏁版嵁骞?#28051;?#37804;?#37828;?#38003;?#37734;?#28051;涓?#28699;?#38010;傜殑鏈鍚?#28051;涓瘮鐗?#28003;嶅?#21229;粨鏉?#38171;?#37813;浠彲鑳?#28028;氫娇鐢EOS(end-of-string)绗?#37721;?#26473;?#29723;屽鍏?#37510;?br />
3. 澶撮儴瀛楁琛?#32448;烘硶
    鏈変簡绗?鑺?#28000;?#32513;嶇殑?#29056;鏈?#37825;版嵁绫诲?#23340;殑琛?#32448;烘硶浣?#28051;哄熀纭锛岀幇鍦?#28751;卞彲浠?#38339;愯堪澶撮儴瀛楁?#30005;殑琛?#32448;烘?#26330;簡銆侶PACK鍗忚灏嗗?#26945;琛?#32448;烘硶鍒?#37812;?绉嶇?#35826;瀷銆傚湪琛?#32448;烘硶寮澶?#37832;?#28051;涓垨?#23266;骞?#28051;瘮鐗?#28003;?#37922;?#27996;庤绀?#32491;诲瀷銆?br /> 3.1 宸?#37734;储?#32041;琛殑澶撮儴瀛楁
    绫诲瀷?#29219;璇?#37719;?#37922;?涓瘮鐗?#28003;?#38171;屽?#28051;?銆傜储?#32041;?#23047;鐢prefix涓??#27537;?#26275;鏁?#29723;?#32448;烘硶銆傚湪瑙?#37934;佹椂锛?#28051;?#28028;氭洿鏂?#37716;?#37804;佽銆?br />
  0   1   2   3   4   5   6   7
+---+---+---+---+---+---+---+---+
| 1 |        Index (7+)         |
+---+---------------------------+
3.2 ?#30370;?#30086;鍏储?#32041;琛殑澶撮儴瀛楁
    绫诲瀷?#29219;璇?#37719;?#37922;?涓瘮鐗?#28003;?#38171;屽?#28051;?1銆傚湪瑙?#37934;佹椂锛?#28028;?#37722;?#37716;?#37804;佽鍐呮彃鍏?#37826;?#37833;?#37929;?#37510;傝繖绉嶇?#35826;?#23338;張琚?#37714;?#37812;?#28051;?#32457;嶆?#21613;喌锛?br /> [1]澶撮儴瀛楁鍚嶅凡鍦储?#32041;琛?#28051;?#38171;屽?#26945;鍚嶇储?#32041;?#23047;鐢prefix涓??#27537;?#26275;鏁?#29723;?#32448;烘硶锛岃屽?#26945;鍊间娇鐢?#28699;楃涓?#29723;?#32448;烘硶銆?br />
  0   1   2   3   4   5   6   7
+---+---+---+---+---+---+---+---+
| 0 | 1 |      Index (6+)       |
+---+---+-----------------------+
| H |     Value Length (7+)     |
+---+---------------------------+
| Value String (Length octets)  |
+-------------------------------+
[2]澶撮儴瀛楁鍚?#28051;嶅湪?#20648;?#32041;琛?#28051;?#38171;屽?#26945;鍚嶅拰瀛楁鍊?#37735;囦娇鐢?#28699;楃涓?#29723;?#32448;烘硶锛岃岀涓涓?#28699;?#38010;傜殑鍚?涓瘮鐗?#28003;嶅?#22246;娇鐢?濉?#37711;?#37510;?br />
  0   1   2   3   4   5   6   7
+---+---+---+---+---+---+---+---+
| 0 | 1 |           0           |
+---+---+-----------------------+
| H |     Name Length (7+)      |
+---+---------------------------+
|  Name String (Length octets)  |
+---+---------------------------+
| H |     Value Length (7+)     |
+---+---------------------------+
| Value String (Length octets)  |
+-------------------------------+
3.2 鏆?#28051;嶇疆鍏储?#32041;琛殑澶撮儴瀛楁
    绫诲瀷?#29219;璇?#37719;?#37922;?涓瘮鐗?#28003;?#38171;屽?#28051;?000銆傚湪瑙?#37934;佹椂锛?#28051;?#37722;?#37716;?#37804;佽鍐呮彃鍏?#37826;?#37833;?#37929;?#37510;傝繖绉嶇?#35826;?#23338;張琚?#37714;?#37812;?#28051;?#32457;嶆?#21613;喌锛?br /> [1]澶撮儴瀛楁鍚嶅凡鍦储?#32041;琛?#28051;?#38171;屽?#26945;鍚嶇储?#32041;?#23047;鐢prefix涓??#27537;?#26275;鏁?#29723;?#32448;烘硶锛岃屽?#26945;鍊间娇鐢?#28699;楃涓?#29723;?#32448;烘硶銆?br />
  0   1   2   3   4   5   6   7
+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 |  Index (4+)   |
+---+---+-----------------------+
| H |     Value Length (7+)     |
+---+---------------------------+
| Value String (Length octets)  |
+-------------------------------+
[2]澶撮儴瀛楁鍚?#28051;嶅湪?#20648;?#32041;琛?#28051;?#38171;屽?#26945;鍚嶅拰瀛楁鍊?#37735;囦娇鐢?#28699;楃涓?#29723;?#32448;烘硶锛岃岀涓涓?#28699;?#38010;傜殑鍚?涓瘮鐗?#28003;嶅?#22246;娇鐢?濉?#37711;?#37510;?br />
  0   1   2   3   4   5   6   7
+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 |       0       |
+---+---+-----------------------+
| H |     Name Length (7+)      |
+---+---------------------------+
|  Name String (Length octets)  |
+---+---------------------------+
| H |     Value Length (7+)     |
+---+---------------------------+
| Value String (Length octets)  |
+---+---------------------------+
3.3 姘?#28051;嶇疆鍏储?#32041;琛殑澶撮儴瀛楁
    绫诲瀷?#29219;璇?#37719;?#37922;?涓瘮鐗?#28003;?#38171;屽?#28051;?001銆傚湪瑙?#37934;佹椂锛?#28051;?#37722;?#37716;?#37804;佽鍐呮彃鍏?#37826;?#37833;?#37929;?#37510;傝繖绉嶇?#35826;?#23338;張琚?#37714;?#37812;?#28051;?#32457;嶆?#21613;喌锛?br /> [1]澶撮儴瀛楁鍚嶅凡鍦储?#32041;琛?#28051;?#38171;屽?#26945;鍚嶇储?#32041;?#23047;鐢prefix涓??#27537;?#26275;鏁?#29723;?#32448;烘硶锛岃屽?#26945;鍊间娇鐢?#28699;楃涓?#29723;?#32448;烘硶銆?br />
  0   1   2   3   4   5   6   7
+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 1 |  Index (4+)   |
+---+---+-----------------------+
| H |     Value Length (7+)     |
+---+---------------------------+
| Value String (Length octets)  |
+-------------------------------+
[2]澶撮儴瀛楁鍚?#28051;嶅湪?#20648;?#32041;琛?#28051;?#38171;屽?#26945;鍚嶅拰瀛楁鍊?#37735;囦娇鐢?#28699;楃涓?#29723;?#32448;烘硶锛岃岀涓涓?#28699;?#38010;傜殑鍚?涓瘮鐗?#28003;嶅?#22246;娇鐢?濉?#37711;?#37510;?nbsp;  
  0   1   2   3   4   5   6   7
+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 1 |       0       |
+---+---+-----------------------+
| H |     Name Length (7+)      |
+---+---------------------------+
|  Name String (Length octets)  |
+---+---------------------------+
| H |     Value Length (7+)     |
+---+---------------------------+
| Value String (Length octets)  |
+-------------------------------+
    ?#24434;浠彂?#24135;锛?.2鑺?#28051;?.3鑺?#28051;殑琛?#32448;烘硶闄簡绫诲瀷?#29219;璇?#28051;?#37722;屼箣澶?#38171;屽叾瀹冪殑閮?#28729;屽叏?#27977;鍚?#37510;?#38317;?#28052;?#28729;?#28000;殑鍖?#37714;?#37828;?#28000;涔?#37723;?#38171;?#32491;诲瀷0000琛?#32448;虹殑瀛楁鍦?#32513;忚繃澶氭瑙?#37934;佷?#24227;?#26667;?#20345;椂锛屽彲鑳?#28028;氳?#28999;涓?#28051;?#28000;?#38000;呯疆鍏储?#32041;琛?#28051;?#37510;傝岀?#35826;瀷0001琛?#32448;烘硶寮鸿?#20903;簡璇?#28699;楁鏃?#29825;?#37734;?#28000;讳綍鏃跺?#38318;?#28051;嶅彲?#30086;鍏储?#32041;琛?#37510;傜?#35826;瀷0001?#24434;鐢?#27996;庤绀哄寘鍚?#37832;?#37825;忔劅淇伅锛?#28641;傚?#21977;爜锛岀殑瀛楁鍊?#38171;?#28000;伩鍏嶅杩?#27996;涘?#26473;?#29723;?#37720;嬬缉鏃?#27996;?#37922;熺殑椋?#38340;?#37510;?br />
4. 鍔?#37804;佽?#27537;绠悊
    鍔?#37804;佽涓殑鏉?#37929;?#29722;?#29825;?#28051;?#37828;?#37832;?#28751;?#28725;哥殑锛屽叾璁?#32480;楀叕寮?#28051;?#38171;氬?#26945;鍚嶇殑瀛?#38010;傞?#22446;害+瀛楁鍊肩殑瀛?#38010;傞?#22446;害+32銆傚?#26945;鍚?鍊肩殑闀垮害鏄寚瀹?#28000;殑鍘熷?#23338;瓧鑺傜殑闀垮害锛岃岄潪缁忚繃Huffman缂栫爜鍚庣殑瀛?#38010;傜殑闀垮害銆?br />     鍔?#37804;佽?#27537;灏?#28725;?#28751;?#37828;?#37711;?#28051;?#37813;鏈?#37833;?#37929;殑灏?#28725;镐箣鍜?#37510;傚姩鎬佽?#27537;鏈澶?#28751;?#28725;?#37828;?#37832;?#38340;愮殑锛屽彲浠?#38315;?#26473;?#28051;?#38344;殑?#26275;鏁?#29723;?#32448;烘?#26335;潵閫氱煡鍗忚?#27537;?#24135;瀹?#37720;?#37824;瑰彉鍔?#37804;佽?#27537;鏈澶?#28751;?#28725;?#37510;?br />
  0   1   2   3   4   5   6   7
+---+---+---+---+---+---+---+---+
| 0 | 0 | 1 |   Max size (5+)   |
+---+---------------------------+
    ?#32139;?#24387;鍏?#37826;扮殑鏉?#37929;垨鏀瑰彉鍔?#37804;佽?#27537;鏈澶?#28751;?#28725;?#37827;?#38171;屽彲鑳藉鑷?#23480;?#37832;夌殑涓涓垨澶?#28051;?#37833;?#37929;?#29722;?#38315;?#37713;?#38171;?#37922;氳?#34411;竻绌烘暣涓?#37716;?#37804;佽銆傚?#21975;姩鎬佽?#27537;鏈澶?#28751;?#28725;?#29825;剧疆涓?鏄?#37722;?#23049;曠殑锛?#28729;?#38340;?#28051;?#38171;岃繖鏄?#28051;绉嶅父鐢殑?#31483;绌?#37716;?#37804;佽?#27537;閫?#23536;?#37510;?/div>

John Jiang 2016-09-24 20:29 ?#24386;琛?#29831;?#29825;?/a>
]]>鎺储HTTP/2: 鍒?#29831;HTTP/2(鍘?http://www.8634070.com/jiangshachina/archive/2016/09/20/431814.htmlJohn JiangJohn JiangTue, 20 Sep 2016 08:42:00 GMThttp://www.8634070.com/jiangshachina/archive/2016/09/20/431814.htmlhttp://www.8634070.com/jiangshachina/comments/431814.htmlhttp://www.8634070.com/jiangshachina/archive/2016/09/20/431814.html#Feedback1http://www.8634070.com/jiangshachina/comments/commentRss/431814.htmlhttp://www.8634070.com/jiangshachina/services/trackbacks/431814.html
鎺储HTTP/2: 鍒?#29831;HTTP/2
鐩?#37715;?#37824;?#37816;丠TTP/2?#27537;鏈嶅姟鍣?#32468;?#28051;?#28729;埛绔?#28729;炵幇宸?#37832;?#28051;嶅皯锛?a href="http://www.8634070.com/jiangshachina/category/55120.html">鎺储HTTP/2绯诲垪
?#27537;绗?#27996;岀瘒灏?#37714;嗗埆浠Jetty鍜宑url浣?#28051;?#37832;嶅姟鍣?#32468;?#37724;?#28729;埛绔?#38171;屾?#24538;堪?#31777;HTTP/2娴?#29831;曠幆澧冪殑?#24780;?#32531;杩囩銆?#37832;?#37826;?#26473;樺皢?#23047;鐢?#26473;?#28051;?#23092;?#29831;曠幆澧?#37720;诲睍绀篔etty鍦?#28729;炵幇HTTP/2鏃剁殑涓涓?#28766;闄?#37724;?#28051;涓狟ug銆?2016.09.22鏈鍚庢洿鏂?

1. HTTP/2?#27537;瀹炵幇
    鐩?#37715;嶅凡缁?#37832;?#28028;?#28598;氱殑鏈嶅姟鍣?#32468;?#37724;?#28729;埛绔?#28729;炵幇?#31777;瀵笻TTP/2?#27537;鏀寔銆傚湪鏈嶅姟鍣?#32468;?#38171;岃憲鍚嶇殑Apache httpd浠?.4.17鐗?#38171;孨ginx浠?.9.5鐗?#38171;屽紑濮?#37824;?#37816;丠TTP/2銆傚湪瀹埛绔?#38171;?#28051;?#23092;佺殑娴忚鍣?#38171;?#28641;Chrome锛孎ireFox鍜孖E锛岀殑鏈鏂?#37911;?#37735;?#37824;?#37816;丠TTP/2锛屼絾瀹?#28000;?#38318;藉彧鏀寔杩?#29723;?#37734;TLS涓婄殑HTTP/2(鍗砲2)銆備娇鐢Java璇?#29815;瀹炵?#25198;殑锛屽垯鏈塉etty鍜孨etty锛?#28729;?#28000;?#38318;?#28729;炵幇?#31777;鏈嶅姟鍣?#32468;?#37724;?#28729;埛绔?#37510;傛澶?#37832;?#28051;浠紿TTP/2瀹炵?#25198;殑?#22442;琛?#38171;?a href="https://github.com/http2/http2-spec/wiki/Implementations">https://github.com/http2/http2-spec/wiki/Implementations
    鍙?#28598;?#38171;岃繕鏈?#28051;浜?#23480;?#37711;锋敮鎸佸?#31547;TTP/2?#27537;鍒嗘瀽涓庤皟璇?#38171;?#28641;俢url鍜學ireShark銆傝繖閲?#28052;?#37832;?#28051;浠芥绫诲伐鍏风殑?#22442;琛?#38171;?a href="https://github.com/http2/http2-spec/wiki/Tools">https://github.com/http2/http2-spec/wiki/Tools

2. 鏈嶅姟鍣?#32468;?/span>
    浣?#28051;篔ava绋嬪簭鍛?#38171;?#38315;?#37922;?#28051;娆句娇鐢Java璇?#29815;缂栧?#27409;殑寮婧怘TTP/2鏈嶅姟鍣?#32468;?#28729;炵幇浼?#28052;庢槸寰堣嚜鐒剁殑缁撴灉銆傚疄闄?#28051;?#38171;?#37734;?#37827;?#37722;庣殑鐮旂涓?#38171;?#37812;?#28000;?#28052;熼渶瑕佹煡鐪?#37832;嶅姟鍣?#32468;殑婧?#28000;爜銆傝繖瀵?#27996;庢繁鍏?#37734;扮悊瑙HTTP/2锛屽?#36346;彂?#24135;瀹炵幇涓彲鑳界殑闂?#26864;?#38171;屽叿鏈夌幇瀹炴?#24526;箟銆?/span>
    鏈?#37826;?#38315;夋嫨Jetty?#27537;鏈鏂?#37911;?#37832;?.3.11浣?#28051;?#37832;嶅姟鍣?#32468;?#37510;侸etty鏄?#28051;涓?#37812;愮?#29114;殑Servlet瀹?#37731;?#38171;岃繖涓?#23534;?#24386;Web?#31794;鐢?#32459;嬪?#24532;彁渚涗簡鏋?#28598;?#28186;?#37714;?#37510;傝?#37832;?#37826;囩1鑺?#28051;?#37819;愬埌?#27537;Netty鏄?#28051;涓?#28028;?#26440;撳?#20635;?#21976;灦锛?#28729;?#28051;撴敞浜庣綉缁?#32459;嬪簭銆傚彲浠娇鐢Netty鍘诲紑?#24386;涓涓猄ervlet瀹?#37731;?#38171;屼?#21978;繖鏄剧劧涓嶅?#20636;洿鎺娇鐢Jetty鏂?#28186;?#37510;?/span>
    瀹夎鍜岄?#23943;疆Jetty鏄?#28051;浠跺緢瀹?#37828;撶殑浜嬫儏锛屽?#34562;綋杩囩濡?#28051;?#37813;绀?#37510;?/span>
    鍋?#29825;?#23005;?#37827;跺凡缁?#28051;?#26462;藉苟瑙?#37720;嬪?#25106;簡Jetty 9.3.11?#27537;鍘嬬缉鏂?#28000;?#38171;岀洰褰?#37722;?#28051;簀etty-9.3.11銆傚湪鍏?#28051;?#37714;涘缓涓涓猼est-base瀛愮洰褰?#38171;屼綔涓哄皢瑕佸?#28056;缓?#27537;Jetty Base?#27537;鐩?#35120;?#37510;?/span>
$ cd jetty-9.3.11
$ mkdir test-base
$ cd test-base
鍦?#37714;涘缓Base鏃?#38171;屽姞鍏?#37824;?#37816;乭ttp锛宧ttps锛宧ttp2(h2)锛宧ttp2c(h2c)鍜宒eploy?#27537;妯?#37735;?#37510;?/span>
$ java -jar ../start.jar --add-to-startd=http,https,http2,http2c,deploy

ALERT: There are enabled module(s) with licenses.
The following 1 module(s):
 + contains software not provided by the Eclipse Foundation!
 + contains software not covered by the Eclipse Public License!
 + has not been audited for compliance with its license

 Module: alpn
  + ALPN is a hosted at github under the GPL v2 with ClassPath Exception.
  + ALPN replaces/modifies OpenJDK classes in the java.sun.security.ssl package.
  + http://github.com/jetty-project/jetty-alpn
  + http://openjdk.java.net/legal/gplv2+ce.html

Proceed (y/N)? y
INFO: server          initialised (transitively) in ${jetty.base}\start.d\server.ini
INFO: http            initialised in ${jetty.base}\start.d\http.ini
INFO: ssl             initialised (transitively) in ${jetty.base}\start.d\ssl.ini
INFO: alpn            initialised (transitively) in ${jetty.base}\start.d\alpn.ini
INFO: http2c          initialised in ${jetty.base}\start.d\http2c.ini
INFO: https           initialised in ${jetty.base}\start.d\https.ini
INFO: deploy          initialised in ${jetty.base}\start.d\deploy.ini
INFO: http2           initialised in ${jetty.base}\start.d\http2.ini
DOWNLOAD: http://central.maven.org/maven2/org/mortbay/jetty/alpn/alpn-boot/8.1.5.v20150921/alpn-boot-8.1.5.v20150921.jar to ${jetty.base}\lib\alpn\alpn-boot-8.1.5.v20150921.jar
DOWNLOAD: https://raw.githubusercontent.com/eclipse/jetty.project/master/jetty-server/src/test/config/etc/keystore?id=master to ${jetty.base}\etc\keystore
MKDIR: ${jetty.base}\webapps
INFO: Base directory was modified
    娉剰锛?#37734;?#28051;婅堪杩囩涓?#38171;?#28028;氭?#35268;嵁?#32139;鍓嶇幆澧冨彉閲?#28051;娇鐢殑Java鐗?#37832;?姝?#28598;?#28051;?.8.0_60)鍘讳笅杞?#28051;涓?#28725;瑰簲?#27537;TLS-ALPN瀹炵幇jar鏂?#28000;?姝?#28598;?#28051;篴lpn-boot-8.1.5.v20150921.jar)锛岃jar浼?#37922;?#27996;庡h2?#27537;鏀寔銆傚綋鍚?#37716;Jetty鏃?#38171;岃jar浼氳Java?#27537;Bootstrap class loader鍔?#26462;藉埌?#34987;?#30718;寰?#28051;?#37510;?/span>
    鍒涘缓涓涓?#37832;绠鍗曠殑Web?#31794;鐢?#38171;屼娇瀹?#37734;?#37837;?#37929;?#35120;?#28051;嬪寘鍚?#28051;涓?#37826;?#37832;?#37826;?#28000;秈ndex锛屽唴瀹?#28051;?HTTP/2 Test"銆?/span>鍐嶅寘鍚?#28051;涓?#32480;鍗曠殑Servlet锛?#28000;爜濡?#28051;?#38171;?/span>
package test;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {

    
private static final long serialVersionUID = 5222793251610509039L;

    @Override
    
public void doGet(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException {
        response.getWriter().println("Test");
    }

    @Override
    
public void doPost(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException {
        doGet(request, response);
    }
}
web.xml涓?#29781;?#37828;?#28729;氫箟?#31777;涓涓猄ervlet锛屽?#34562;綋鍐?#28729;瑰涓?#38171;?/span>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    metadata-complete="false" version="3.1">

    
<welcome-file-list>
        
<welcome-file>index</welcome-file>
    
</welcome-file-list>

    
<servlet>
        
<servlet-name>test</servlet-name>
        
<servlet-class>test.TestServlet</servlet-class>
    
</servlet>
    
<servlet-mapping>
        
<servlet-name>test</servlet-name>
        
<url-pattern>/test/*</url-pattern>
    
</servlet-mapping>
</web-app>
璇簲鐢殑?#20788;缃茶矾寰?#28051;簀etty-9.3.11/test-base/webapps/test.war銆傚湪璇WAR鏂?#28000;舵墍鍦殑鐩?#35120;?#28051;?#38171;屽?#28056;缓涓涓猼est.xml锛屽?#36346;唴瀹瑰涓?#37813;绀?#38171;?/span>
<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  
<Set name="contextPath">/</Set>
  
<Set name="war"><SystemProperty name="jetty.base" default="."/>/webapps/test.war</Set>
</Configure>
鍚?#37716;Jetty鏈嶅姟鍣?#38171;屼娇鐢?#27035;?#29825;殑HTTP鍜孒TTPS绔?#37721;?#38171;屽?#21975;埆涓?080鍜?443銆?/span>
$ java -jar ../start.jar
2016-09-15 21:15:51.190:INFO:oejs.Server:main: jetty-9.3.11.v20160721
2016-09-15 21:15:51.237:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:///D:/http2/jetty/jetty-9.3.11/test-base/webapps/] at interval 1
2016-09-15 21:15:52.251:INFO:oejw.StandardDescriptorProcessor:main: NO JSP Support for /test.war, did not find org.eclipse.jetty.jsp.JettyJspServlet
2016-09-15 21:15:52.313:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@4520ebad{/test.war,file:///D:/http2/jetty/jetty-9.3.11/test-base/webapps/test.war/,AVAILABLE}{D:\http2\jetty\jetty-9.3.11\test-base\webapps\test.war}
2016-09-15 21:15:52.391:INFO:oejw.StandardDescriptorProcessor:main: NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet
2016-09-15 21:15:52.391:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@711f39f9{/,file:///D:/http2/jetty/jetty-9.3.11/test-base/webapps/test.war/,AVAILABLE}{/test.war}
2016-09-15 21:15:52.532:INFO:oejs.AbstractConnector:main: Started ServerConnector@1b68ddbd{HTTP/1.1,[http/1.1, h2c, h2c-17, h2c-16, h2c-15, h2c-14]}{0.0.0.0:8080}
2016-09-15 21:15:52.735:INFO:oejus.SslContextFactory:main: x509=X509@e320068(jetty,h=[jetty.eclipse.org],w=[]) for SslContextFactory@1f57539(file:///D:/http2/jetty/jetty-9.3.11/test-base/etc/keystore,file:///D:/http2/jetty/jetty-9.3.11/test-base/etc/keystore)
2016-09-15 21:15:52.735:INFO:oejus.SslContextFactory:main: x509=X509@76f2b07d(mykey,h=[],w=[]) for SslContextFactory@1f57539(file:///D:/http2/jetty/jetty-9.3.11/test-base/etc/keystore,file:///D:/http2/jetty/jetty-9.3.11/test-base/etc/keystore)
2016-09-15 21:15:53.234:INFO:oejs.AbstractConnector:main: Started ServerConnector@4b168fa9{SSL,[ssl, alpn, h2, h2-17, h2-16, h2-15, h2-14, http/1.1]}{0.0.0.0:8443}
2016-09-15 21:15:53.249:INFO:oejs.Server:main: Started @3940ms
    鏍规嵁涓婅堪鏃?#36423;楀彲?#29025;锛孞etty鍚?#37922;簡Web?#31794;鐢test.war锛岃繕鍚?#37716;簡涓?#28051;猄erverConnector锛?#28051;涓?#37824;?#37816;乭2c锛屽彟涓涓?#37824;?#37816;乭2銆傚?#23536;楁敞鎰忕殑鏄?#38171;岃繖涓?#28051;猄erverConnector杩?#37714;嗗埆鏀?#37816;乭2c-17, h2c-16, h2c-15, h2c-14鍜宧2-17, h2-16, h2-15, h2-14銆傝繖鏄?#37733;?#28051;?#38171;孒TTP/2鍦?#23005;?#23534;忓彂甯冧箣鍓?#38171;屽厛鍚庡彂甯冧簡18涓?#38013;夋锛屽?#21057;?#26663;彿涓?0-17銆?#37813;浠?#38171;岃繖閲岀殑h2c-XX鍜宧2-XX鎸囩殑灏?#37828;?#32471;琗X鍙疯?#22795;銆?/span>

3. 瀹埛绔?/span>
    鍏?#28729;?#37832;鏂?#28186;跨殑瀹埛绔?#28751;?#37828;?#23092;忚鍣簡銆傚彧瑕佷娇鐢殑FireFox?#22440;Chrome鐗?#37832;?#28051;?#37828;?#28598;?#38000;?#38171;岃偗瀹?#38318;藉凡缁?#37824;?#37816;佷簡HTTP/2锛岃?#28051;?#26473;?#28051;鍔?#38003;?#37828;?#27035;?#29825;?#37813;撳紑?#27537;銆備?#29111;氨鏄?#29831;?#38171;屽綋?#23047;鐢FireFox鍘?#29825;?#38338;?#37715;?#38344;?#37813;?#20788;缃茬殑Web?#31794;鐢?#37827;?#38171;屽氨鏄?#37734;娇鐢HTTP/2锛屼絾浣?#28051;?#28028;氭劅瑙夊埌杩?#32457;嶅彉?#23538;銆備娇鐢FireFox鎻?#28186;涚殑Developer Tools涓殑Network宸?#37711;锋煡鐪?#37832;嶅姟鍣?#32468;殑鍝嶅簲锛?#28028;氬彂?#24135;HTTP鐗?#37832;?#28051;篐TTP/2.0銆備?#21976;澶?#29999;?#37832;?#26473;?#28051;?#28729;埛绔?#38003;藉?#29112;彁渚涙洿涓?#28051;?#28725;岀殑涓?#37832;嶅姟鍣?#32468;?#26473;?#29723;?#27996;?#27996;掔殑鍔?#38003;?#38171;?#38317;?#28052;?#23092;忚鍣?#28751;?#39582;?#28051;?#37722;?#38315;備簡銆?br />    Jetty涔?#28729;炵幇?#31777;鏀?#37816;丠TTP/2?#27537;瀹埛绔?#38171;屼?#21978;繖涓?#28729;埛绔?#37828;?#28051;涓狝PI锛岄渶瑕佺?#26663;?#27409;?#23338;簭鍘?#29825;?#38338;瓾TTP/2鏈嶅姟鍣?#32468;?#37510;傝?#28051;?#38171;岀洰鍓?#29831;API?#27537;璁?#29825;?#37814;借薄灞傛杈?#28003;?#38171;岄渶瑕佸簲鐢?#32459;嬪簭鍛?#28725;笻TTP/2鍗忚锛屾瘮濡?#37722;勭?#23941;抚锛?#37832;?#26440;冩繁鍏殑?#31777;瑙?#37510;傝繖瀵?#27996;庡垵?#31225;HTTP/2?#27537;寮?#24386;鑰呮潵璇?#38171;?#37828;?#37906;跺緢涓?#37722;?#38315;?#37510;?#37832;?#37826;?#38315;夋嫨?#23047;鐢C璇?#29815;缂栧?#27409;殑涓涓?#23480;?#37711;?#38171;屽叾瀹?#28052;?#37828;疕TTP/2?#27537;瀹埛绔?#28729;炵幇?#31651;涓锛宑url銆?/span>
    curl鍦?#37824;?#37816;丠TTP/2鏃?#38171;?#28729;?#38340;?#28051;?#37828;娇鐢簡nghttp2?#27537;C搴?#38171;?#37813;浠渶瑕佸厛瀹夎nghttp2銆傚彟澶?#38171;?#28051;轰簡璁curl鏀?#37816;乭2锛屽氨蹇呴瑕?#37832;TLS-ALPN?#27537;鏀寔銆?#38317;?#28052;?#38171;?#28051;鑸?#37734;?#26473;橀渶瑕?#28729;夎?#21291;penSSL 1.0.2+銆?/span>
    ?#32137;缁?#28051;?#37711;?#27996;?#37734;Linux涓?#28729;夎鏀?#37816;丠TTP/2?#27537;curl?#27537;璧勬簮鏈?#23536;?#28598;?#38171;岃?#22249;?#23338;苟涓嶉毦锛屼絾鏈夌?#29808;?#36328;箒锛岃瀹夎?#21615;殑渚?#29863;栨瘮杈?#28598;?#38171;?#37832;?#37826;?#28751;?#28051;?#29863;樿堪?#31777;銆傚?#20635;灉鏄娇鐢Windows锛岀瑪鑰呮瘮杈?#37818;?#38013;?#38315;?#26473;Cygwin?#28533;瀹夎鍜屼娇鐢curl銆傚湪Windows涓?#28729;夎?#21268;ygwin闈?#29999;?#32480;鍗?#38171;?#37734;Cygwin涓?#37813;?#29723;?#37722;勭?#23941;懡浠?#37827;?#38171;屾劅瑙?#28051;?#28751;?#28641;?#37722;?#37734;娇鐢Linux锛屽敖绠?#28729;?#39582;?#28051;?#37828;?#28051;涓?#38095;氭嫙鏈?#37510;?#38315;?#26473;Cygwin瀹夎curl锛?#28729;?#28028;氳嚜鍔?#37734;?#28729;夎鎵?#28214;?#27537;鍚勭渚?#29863;栫?#23338;簭鍜屽簱銆?/span>
    鍦?#32471;?#38000;呯殑鏈?#37731;?#28051;?#38171;?#38315;?#26473;囨煡鐪媍url?#27537;鐗?#37832;?#28028;氬?#34425;幇濡?#28051;?#28103;伅锛?/span>
curl 7.50.2 (x86_64-unknown-cygwin) libcurl/7.50.2 OpenSSL/1.0.2h zlib/1.2.8 libidn/1.29 libpsl/0.14.0 (+libidn/1.29) libssh2/1.7.0 nghttp2/1.14.0
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp
Features: Debug IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets Metalink PSL
鐢?#28051;婂彲?#29025;锛岀瑪鑰呬娇鐢殑curl鐗?#37832;?#37828;?.50.2锛宯ghttp2鐗?#37832;?#37828;?.14.0锛岃孫penSSL鐗?#37832;?#37828;?.0.2h銆?/span>

4. 绗?#28051;娆?#28751;?#29831;?/span>
    鍦?#32471;?#28051;娆?#28751;?#29831;?#28051;?#38171;屽彧?#28214;瑕佺畝鍗?#37734;?#29825;?#38338;?#32471;?鑺?#28051;儴缃茬殑Web?#31794;鐢?#28051;殑闈?#37804;?#37826;?#37832;?#37826;?#28000;秈ndex锛?#28000;?#37808;熷彈涓媓2c锛?#28729;屾暣鍛?#28000;?#28641;?#28051;?#38171;?/span>
$ curl -v --http2 http://localhost:8080/index
鍦?#26440;撳嚭涓寘鍚?#37832;?#28641;?#28051;嬬殑鍐?#28729;?#38171;?/span>
...
> GET /index HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.50.2
> Accept: */*
> Connection: Upgrade, HTTP2-Settings
> Upgrade: h2c
> HTTP2-Settings: AAMAAABkAAQAAP__
>
...
< HTTP/1.1 101 Switching Protocols
* Received 101
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
...
< HTTP/2 200
< server: Jetty(9.3.11.v20160721)
< last-modified: Wed, 14 Sep 2016 12:52:32 GMT
< content-length: 11
< accept-ranges: bytes
<
...
HTTP/2 Test
">"鏄?#28729;埛绔彂閫佺殑璇锋眰锛?<"鏄?#37832;嶅姟鍣?#32468;彂閫佺殑鍝嶅簲锛岃?*"鏄痗url瀵瑰綋鍓?#26473;囩?#23340;殑璇?#37828;?#37510;?/span>缁?#37722;?#37832;?#32495;诲垪绗?#28051;?#30226;鏂囩珷涓?#37813;绠?#22570;?#27537;HTTP 2鍗忚锛屽彲浠?#37832;?#28000;?#28051;嬬殑?#29056;鏈悊瑙?#37510;?/span>
[1]瀹埛绔彂璧蜂簡涓涓狧TTP/1.1?#27537;璇锋眰锛屽叾涓?#37820;?#29999;?#37832;塙pgrade澶撮儴锛岃?#20345;眰鏈嶅姟鍣?#32468;?#37719;囩骇?#22476;HTTP/2(h2c)銆?/span>
> GET /index HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.50.2
> Accept: */*
> Connection: Upgrade, HTTP2-Settings
> Upgrade: h2c
> HTTP2-Settings: AAMAAABkAAQAAP__
>
[2]鏈嶅姟鍣?#32468;?#37722;屾剰鍗囩骇锛岃繑鍥?#37725;嶅簲"101 Switching Protocols"锛岀劧鍚?#28729;埛绔敹?#22476;?#31777;101鍝嶅簲锛孒TTP/2杩?#37818;?#26473;?#29723;岀璁?#37510;?/span>
< HTTP/1.1 101 Switching Protocols
* Received 101
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
[3]鏈嶅姟鍣?#32468;?#37725;嶅簲鏈缁?#32513;撴灉銆?#37912;舵佽涓?#37713;虹?#25198;殑HTTP鐗?#37832;?#28051;篐TTP/2锛?#37912;舵?#28000;?#37934;佷负200锛?#28051;?#37722;?#38344;?#23052;?#37832;夎?#29114;潃"OK"銆?#37832;鍚?#26440;撳?#36720;簡index鏂?#28000;剁殑鍐?#28729;?HTTP/2 Test"銆?/span>
< HTTP/2 200
< server: Jetty(9.3.11.v20160721)
< last-modified: Wed, 14 Sep 2016 12:52:32 GMT
< content-length: 11
< accept-ranges: bytes
<
...
HTTP/2 Test

5. 涓涓?#28766;闄?/span>
    杩欐锛?#37734;彂璧风殑璇锋眰涓寘鍚綋?#20788;锛?#37723;?#28000;?#28641;?#28051;?#38171;?/span>
$ curl -v --http2 -d "body" http://localhost:8080/index
鍦?#26440;撳嚭涓寘鍚?#37832;?#28641;?#28051;嬬殑鍐?#28729;?#38171;?/span>
...
> POST /index HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.50.2
> Accept: */*
> Connection: Upgrade, HTTP2-Settings
> Upgrade: h2c
> HTTP2-Settings: AAMAAABkAAQAAP__
> Content-Length: 4
> Content-Type: application/x-www-form-urlencoded
>
...
< HTTP/1.1 200 OK
< Last-Modified: Wed, 14 Sep 2016 12:52:32 GMT
< Accept-Ranges: bytes
< Content-Length: 11
...
HTTP/2 Test
    鍜岀4鑺?#28051;殑杈撳嚭杩?#29723;屾瘮杈?#38171;?#28028;氬彂鐜扮己灏戜簡"101 Switching Protocols"閭?#28051;娈?#38171;岃?#28051;?#37832;缁堝?#23941;簲鐘舵佽涓?#37713;虹?#25198;殑HTTP鐗?#37832;?#37828;疕TTP/1.1銆傝繖灏?#29831;?#37828;?#37832;嶅姟鍣?#32468;?#28051;?#37722;屾剰鍗囩骇锛?#37722;?#38344;?#32513;?#32513;娇鐢HTTP/1.1銆傚?#27692;垰?#20788;缃茬殑Jetty鏈?#37707;?#28000;讳綍鏀瑰彉鎬?#28052;?#28028;氱?#20346;劧涓?#37824;?#37816;丠TTP/2?#31777;鍛?#38171;熸垨鑰?#26473;?#37828;痗url?#27537;闂?#26864;?#38171;熷叾瀹?#38171;?/span>杩?#37828;?#37733;?#28051;篔etty鏈嶅姟鍣?#32468;?#37734;?#28729;炵幇h2c鏃?#28051;?#37824;寔璇锋眰涓寘鍚綋?#20788;銆傚彟澶?#38171;孉pache httpd涔?#37832;?#37722;?#37837;风殑闂?#26864;?#37510;傚?#20635;灉鏄娇鐢h2锛屽垯娌?#37832;?#26473;?#28051;?#38340;?#37714;?#37510;傝繖鑳?#37722;庣殑鍘?#37733;犺?#21613;?#36720;簡鏈?#37826;囩殑鑼冪暣锛?#28051;?#28003;?#29723;堪銆?/span>

6. 涓涓狟ug
    鍦?#26473;欐灏?#29831;?#28051;?#38171;?#23092;?#29831;?#28051;涓?#28051;?#32468;?#28725;?00-continue?#27537;鏀寔銆傚?#20635;灉璇锋眰涓娇鐢簡澶撮儴"Expect: 100-continue"锛?#38317;?#28052;?#23005;?#29999;?#37734;?#29831;?#29831;锋?#20637;鏈変綋?#20788;銆備絾鐢变簬鍦?#32471;?鑺?#28051;?#28000;?#32513;嶇殑闂?#26864;?#38171;屾鏃?#28051;?#38003;藉?#23940;娇鐢h2c锛岃屽彧鑳戒娇鐢h2銆傚彟澶?#38171;岃繖娆?#28051;?#29825;?#38338;?#38344;?#37804;?#37826;?#28000;?#38171;岃?#37828;?#29825;?#38338;Servlet(姝?#28598;?#28051;?test)銆傚?#23678;暣鍛?#28000;?#28641;?#28051;?#38171;?/span>
$ curl -vk --http2 -H "Expect: 100-continue" -d "body" https://localhost:8443/test
鍦?#26440;撳?#34425;殑鏈鍚庡?#34425;幇?#31777;濡?#28051;?#28103;伅锛?/span>
curl: (92) HTTP/2 stream 1 was not closed cleanly: CANCEL (err 8)
杩?#37711;?#28729;?#37828;疛etty?#27537;涓涓?a href="https://github.com/eclipse/jetty.project/issues/902">Bug锛屾鍦?#23534;?#24386;涓殑9.3.12宸?#32513;?#28103;?#28598;嶄簡瀹?#37510;?/span>

7. 灏?#32513;?/span>
    HTTP/2渚濈?#21057;畻鏄?#37826;版疆?#27537;鎶鏈?#38171;屽鍚?#28729;剁殑瀹炵幇锛?#37827;?#29825;?#37828;?#37832;嶅姟鍣?#32468;?#38171;?#28729;埛绔?#38171;岃繕鏄?#37714;嗘瀽宸?#37711;?#38171;?#38318;借?#20345;寔鏈?#28051;浠?#37804;鐤?#37804;佸害銆傝繖浜?#28729;炵幇鍜屽伐鍏?#38318;?#37828;?#32459;嬪簭锛?#38318;?#37832;夊彲鑳藉瓨鍦bug銆傝?#28051;?#37719;忚瀵?#29825;?#28598;?#32513;嗚?#20635;病鏈変?#28355;嚭瑙?#28729;?#38171;?#37722;?#28729;?#38318;?#28028;氬彂鎸嚜宸辩殑鎯?#37709;?#37716;?#37510;傛瘮濡?#38171;孉pache httpd鍜孞etty鍦?#28729;炵幇鏈嶅姟鍣?#32468;?#37818;?#38315;佹椂锛屽?#33333;?#29808;紡灏?#28051;嶅?#30028;浉鍚?#37510;?br />    鍦?#23534;?#24386;?#22172;宸辩殑HTTP/2瀹炵幇?#22440;?#31794;鐢殑鏃跺?#38171;岄渶瑕?#37722;?#37827;朵娇鐢?#23480;?#37832;夌殑涓?#37722;?#37832;嶅姟鍣?#32468;?#37724;?#28729;埛绔?#37720;婚儴缃?#28598;氬娴?#29831;曠幆澧?#26473;?#29723;屽?#35268;瘮鍒嗘瀽銆?br />


John Jiang 2016-09-20 16:42 ?#24386;琛?#29831;?#29825;?/a>
]]>鎺储HTTP/2: HTTP 2鍗忚绠?#22570;(鍘?http://www.8634070.com/jiangshachina/archive/2016/09/19/431811.htmlJohn JiangJohn JiangMon, 19 Sep 2016 03:36:00 GMThttp://www.8634070.com/jiangshachina/archive/2016/09/19/431811.htmlhttp://www.8634070.com/jiangshachina/comments/431811.htmlhttp://www.8634070.com/jiangshachina/archive/2016/09/19/431811.html#Feedback0http://www.8634070.com/jiangshachina/comments/commentRss/431811.htmlhttp://www.8634070.com/jiangshachina/services/trackbacks/431811.html
鎺储HTTP/2: HTTP/2鍗忚绠?#22570;
HTTP/2?#27537;鍗忚?#23512;鍚潃涓?#28051;RFC锛欻ypertext Transfer Protocol Version 2 (
RFC7540)锛?#37719;HTTP/2锛汬PACK: Header Compression for HTTP/2 (RFC7541)锛?#37719;HPACK銆俁FC7540鎻忚堪?#31777;HTTP/2?#27537;璇箟锛孯FC7541鍒欐?#24538;堪?#31777;鐢?#27996;嶩TTP/2?#27537;澶撮儴鍘嬬缉?#27537;鏍?#23534;?#37510;?#37832;?#37826;?#37721;?#23057;夊強HTTP/2鍗忚锛?#37832;?#32495;诲垪?#27537;鍚?#32513;?#37826;囩珷?#30370;浼氭?#22794;強HPACK鍗忚銆?2016.10.13鏈鍚庢洿鏂?

1. HTTP/2瑕?#29785;?#37712;崇殑闂?#26864;?/span>
     HTTP/1.0鍙厑璁?#37734;?#28051;涓猅CP杩?#37818;?#28051;?#37713;虹幇涓涓?#29831;锋眰銆?#37722;庢潵?#27537;HTTP/1.1铏?#37906;跺紩鍏簡璇锋眰娴佹?#23544;嚎锛?#28000;厑璁?#37734;?#28051;涓?#26473;?#37818;?#28051;彂閫?#28598;?#28051;?#29831;锋眰锛屼?#21978;繖鍙?#37828;儴鍒?#37734;?#29785;?#37712;充簡璇锋?#20634;?#36346;彂?#27537;闂?#26864;?#37510;?#37832;嶅姟鍣?#32468;?#37734;?#26473;?#37733;?#37725;嶅簲鏃?#38171;岃繕鏄?#36423;呴瑕佹?#22796;収瀹?#37818;敹?#22476;?#27537;璇锋?#20636;殑椤哄?#24538;繘琛岃繑鍥?#37510;傚?#20635;灉鎺?#37734;?#37715;?#38344;殑鍝嶅簲瑕佹?#22563;?#26440;?#38336;跨殑鏃?#38338;?#38171;?#38317;?#28186;濈劧浼氬鍚?#38344;殑鍝嶅簲?#27537;閫?#37812;?#38339;诲锛?#27996;?#37719;?#32510;?#28598;撮?#35826;(Head-of-line blocking)銆?#37813;浠?#38171;?#28729;埛绔?#36423;呴瑕佷娇鐢?#28598;氭潯杩?#37818;?#37720;诲彂璧?#28598;?#28051;殑璇锋眰浠?#28729;炵幇骞跺彂锛屽苟杩涜屽噺灏忓欢杩?#37510;傛洿澶殑骞跺彂浼氬澶?#37832;嶅姟鍣殑?#30987;杞?#38171;?#28052;?#28028;氬崰鐢洿澶殑?#32137;缁?#29999;?#28729;?#37510;傚彟澶?#38171;?#28598;撮儴閫?#29999;?#28028;氬寘鍚?#37832;?#28598;?#38322;忕殑淇伅锛?#28641;俢ookie锛岃岃繖涔?#28028;氬鍔犵綉缁?#28028;?#26440;撶殑寮閿銆?/span>
     HTTP/2?#21393;璁?#37734;?#37722;?#28051;涓猅CP杩?#37818;?#28051;?#27996;?#38335;?#37734;?#37713;虹幇澶?#28051;?#29831;锋眰涓庡?#23941;簲锛?#27996;?#37719;?#28598;氬伐(Multiplex)銆?#37722;?#37827;?#38171;?#28729;冧娇鐢簡涓涓?#26946;樻?#22562;殑缂栫爜鏂规硶瀵瑰?#25774;儴杩?#29723;?#37720;嬬缉銆侶TTP/2杩樺厑璁?#28725;?#29831;锋?#20637;繘琛?#28028;?#37711;堢骇鎺?#25652;?#38171;?#28000;?#28186;?#29825;洿涓洪噸瑕佺殑璇锋?#20634;緱浠洿蹇殑瀹?#37812;?#38171;岃繖浼?#26473;?#28051;姝?#37819;愰珮鎬?#38003;?#37510;侶TTP/2杩?#37824;瑰彉?#31777;鏈嶅姟鍣?#32468;?#37721;?#38003;?#29722;?#37716;?#37734;?#37722;?#28729;埛绔?#26473;?#37733;?#37725;嶅簲?#27537;瀹氬紡锛屽厑璁?#37832;嶅姟鍣?#32468;?#28051;诲姩鍦?#37722;?#28729;埛绔?#37818;?#38315;佹?#29256;嵁锛岃繖灏卞彲浠?#37713;?#28751;?#28729;埛绔彂璧?#29831;锋?#20636;殑鏁?#38322;?#37510;?/span>
     鎬讳箣锛孒TTP/2涓?#29781;?#37828;?#29785;?#37712;?#37804;?#38003;?#38338;?#26864;?#37510;?/span>

2. ?#24386;璧HTTP/2
     HTTP/2浼氫娇鐢?#28051;嶩TTP/1?#27977;鍚岀殑URI scheme锛?#37719;砲ttp鍜宧ttps銆傝?#28051;?#28729;炵幇HTTP/2?#27537;鏈嶅姟鍣?#32468;?#28052;?#28051;?#28028;氫娇鐢?#28051;?#37722;岀殑绔?#37721;?#37720;诲?#21975;埆鏀?#37816;丠TTP/1鍜孒TTP/2銆傝繖鏍?#37832;?#37714;?#27996;庡?#34411;粦鍦?#28000;嶩TTP/1鍗囩骇?#22476;HTTP/2銆傛瘯绔?#37929;?#37715;嶅凡?#20788;缃茬殑缁?#28598;儴鍒嗙綉缁?#37832;嶅姟閮藉彧鏀?#37816;丠TTP/1锛屽綋鏈潵瀹?#28000;?#37719;囩骇?#22476;HTTP/2鏃?#38171;?#28641;傛灉鎹?#37922;簡涓?#37722;孶RI scheme?#22440;绔?#37721;?#38171;?#38317;?#28052;堣偗瀹?#28028;氬瀹埛绔?#27996;?#37922;熸瀬澶殑褰?#37725;?#37510;備絾鏄疕TTP/2鍗忚涓?#26473;?#29723;?#37734;http鍜宧ttps涓婄殑HTTP/2鍒嗗埆瀹氫箟?#31777;涓?#28051;?#28051;?#37722;岀殑?#29219;璇嗙锛h2c鍜宧2銆俬2c涓殑"c"鎸囩殑鏄痗leartext锛?#37719;?#37828;?#37826;?#37510;?#37832;?#37826;?#37722;?#38344;?#28028;氫娇鐢h2c?#23514;浠?#26473;?#29723;?#37734;http2涓??#27967;鎺娇鐢TCP)?#27537;HTTP/2锛岃?#37922;h2?#23514;浠?#26473;?#29723;?#37734;https涓??#23047;鐢TLS)?#27537;HTTP/2銆?/span>
     閭?#28052;?#38171;?#37824;?#37816;丠TTP/2?#27537;瀹埛绔?#28641;備?#26336;煡?#20158;瀹?#37813;杩?#37818;殑鏈嶅姟鍣?#32468;?#37828;?#37722;?#28052;?#37824;?#37816;丠TTP/2鍛?#38171;?/span>
     瀵?#27996;巋2c锛?#37824;?#37816;丠TTP/2?#27537;瀹埛绔彲浠?#37734;彂璧风殑璇锋眰涓娇鐢HTTP/1.1?#27537;Upgrade澶撮儴鍘诲皾璇曡?#20345;眰鏈嶅姟鍣?#37719;囩骇?#22476;HTTP/2銆?#29831;?#29831;锋?#20636;殑鏍?#23534;?#28641;?#28051;?#38171;?/span>
GET / HTTP/1.1
Host: server.example.com
Connection: Upgrade, HTTP2-Settings
Upgrade: h2c
HTTP2-Settings: <base64url encoding of HTTP/2 SETTINGS payload>
HTTP2-Settings鏄?#28051;涓?#32513;?#37922;盉ASE64缂栫爜杩囩殑瀛楃涓?#38171;屽叾鍘熷?#23338;唴瀹?#37828;?#28729;埛绔皢瑕佸彂閫佺殑SETTINGS甯殑杞借嵎锛?#37719;?#28051;浜涢?#23943;疆鍙傛暟銆?/span>
     濡傛灉鏈嶅姟鍣?#32468;?#37824;?#37816;丠TTP/2锛?#28729;?#28751;?#37725;嶅簲"101 Switching Protocols"锛岃绀哄彲浠?#26473;?#29723;?#37719;囩骇銆?#29831;?#37725;嶅簲?#27537;鏍?#23534;?#28641;?#28051;?#38171;?/span>
HTTP/1.1 101 Switching Protocols
Connection: Upgrade
Upgrade: h2c
     濡傛灉鏈嶅姟鍣?#32468;?#28051;?#37824;?#37816;丠TTP/2锛屽垯浼氬拷鐣Upgrade璇锋?#20634;?#25774;儴锛?#37722;?#32513;?#28186;濈劧?#23047;鐢HTTP/1.1銆?/span>
     瀵?#27996;巋2锛?#28028;氫娇鐢埌鍗忚?#29964;ransport Layer Security (TLS) Application-Layer Protocol Negotiation Extension (RFC7301)锛?#37719;TLS-ALPN銆?#29831;?#37719;忚?#21393;璁?#28729;埛绔?#37724;?#37832;嶅姟鍣?#32468;?#28751;变娇鐢?#28003;?#32457;?#37911;?#37832;殑HTTP杩?#29723;?#37719;忓晢銆傚?#20635;灉TLS-ALPN鍦幇瀹?#28051;?#26473;?#29723;岃壇濂界殑璇?#38171;?#28052;?#29825;告煇澶?#26473;?#28028;氫娇鐢?#29831;?#37826;规硶鍘诲崗鍟嗕娇鐢?#37714;殑鍗忚銆?/span>
     ?#32139;瀹埛绔?#28051;?#37832;嶅姟鍣?#32468;?#38318;?#37722;屾?#24526;娇鐢HTTP/2鏃?#38171;屽鏂?#38318;介渶瑕?#37722;勮嚜?#24386;鍑?#28051;涓?#26473;?#37818;?#25652;忚(Connection Preface)浠?#26473;?#29723;?#37832;鍚庣殑纭?#29825;?#37510;?/span>
     瀹埛绔?#37734;?#37818;敹?#22476;鏈嶅姟鍣?#32468;殑"101 Switching Protocols"鍝嶅簲(閽堝h2c)?#22440;TLS杩?#37818;殑绗?#28051;涓簲鐢?#37825;版嵁瀛?#38010;?閽堝h2)?#31651;鍚?#28028;?#32468;?#37719;冲彂鍑?#26473;?#37818;?#25652;忚銆?#29831;?#25652;忚?#27537;寮澶?#37828;?PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"(鍏?#37719;佸杩?#37714;跺舰寮?#28051;?0x505249202a20485454502f322e300d0a0d0a534d0d0a0d0a")(1)锛?#37722;?#38344;?#36423;呴?#35826;?#23944;涓涓猄ETTINGS甯?#38171;屽摢鎬?#26473;?#28051;?#29999;?#37828;?#32460;虹殑銆?/span>
     鏈嶅姟鍣?#32468;殑杩?#37818;?#25652;忚鍒?#37922;?#28051;涓猄ETTINGS甯?#37835;勬垚锛岃甯?#36423;呴鏄?#37832;嶅姟鍣?#32468;?#37734;HTTP/2杩?#37818;?#28051;彂閫佺殑绗?#28051;涓?#29999;?#37510;傝繖涓猄ETTINGS甯彲浠?#28051;虹锛?#28052;熷彲浠寘鍚?#28051;浜?#29999;?#37832;?#28729;埛绔?#28641;備綍涓庤嚜宸?#26473;?#29723;?#38315;氫俊?#27537;蹇?#29781;侀?#23943;疆淇伅銆?/span>

3. 甯?Frame)
     HTTP/2娑堟伅?#23047;鐢?#27996;岃繘鍒舵牸寮?瀹?#38340;?#32514;栫?#20345;?#26421;娇鐢?#37719;佸杩?#37714;?#28052;?#37712;?锛岀浉?#30254;浜?#37826;?#37832;?#37837;?#23534;?#38171;岃繖鏍峰彲浠?#37819;愰?#27195;?#22559;伅澶勭?#21977;殑鏁堢巼銆侶TTP/2娑堟伅?#27537;鏈灏?#37719;?#37711;?#28051;?#29999;?#38171;?#28729;?#37922;?#28598;撮儴涓?#26462;借嵎(Payload)缁勬垚銆傛瘡涓?#29999;殑闀垮害蹇呴鏄?#28051;涓垨澶?#28051;??#30254;鐗?#28003;嶅瓧鑺?octet锛?#28051;?#37826;?#28751;嗗?#21057;畝鍐?#28051;?瀛?#38010;?)銆?/span>
     甯?#28598;撮儴渚?#23046;寘鍚?#37832;?#28641;?#28051;嬬殑5涓?#28699;楁锛?/span>
     闀垮害(Length)锛?#29831;?#28699;楁鍗?#37922;?4涓瘮鐗?#28003;?#38171;?#28000;?#29723;?#29999;?#26462;借?#39118;殑闀垮害銆?#29831;?#38336;垮害鏄?#28051;涓?4浣嶇殑鏃?#32471;?#37721;锋暣鏁?#37510;?/span>
     绫诲瀷(Type)锛?#29831;?#28699;楁鍗?#37922;?涓瘮鐗?#28003;?#38171;?#28000;?#29723;?#29999;殑绫诲瀷銆?/span>
     ?#29219;蹇?Flags)锛?#29831;?#28699;楁鍗?#37922;?涓瘮鐗?#28003;?#38171;?#28000;?#29723;?#29999;?#37813;瀹氫箟?#27537;涓涓垨澶?#28051;爣蹇?#37510;傚苟涓?#37828;?#37813;鏈夌殑甯?#38318;?#28729;氫箟?#31777;?#29219;蹇?#37510;?/span>
     淇濈暀浣?R)锛?#29831;?#28699;楁鍗?#37922;?涓瘮鐗?#28003;?#38171;屽叾璇箟灏?#37832;?#29722;?#28729;氫箟銆傚湪璇诲彇甯?#37827;?#38171;岃浣嶉渶瑕佽蹇?#37923;?#38171;?#28003;?#37734;彂閫?#29999;?#37827;?#38171;岃浣嶉渶瑕佷繚鎸佷负0(0x0)銆?/span>
     娴佹爣璇嗙(Stream Identifier)锛?#29831;?#28699;楁鍗?#37922;?1涓瘮鐗?#28003;?#38171;?#28000;?#29723;?#29831;?#29999;?#37813;鍦?#23092;佺殑?#29219;璇嗙銆?/span>
     鍦?#28598;撮儴?#31651;鍚?#38171;?#32497;?#37818;潃?#27537;灏?#37828;?#26462;借嵎銆?#26462;借?#39118;殑缁?#37835;?#28051;庡唴瀹?#28729;屽叏鐢?#29999;殑绫诲?#23338;喅瀹?#38171;?#28729;冪殑闀垮害涔?#37828;?#28051;?#28729;氱殑銆?br />
     HTTP/2瀹氫箟?#31777;濡?#28051;?0绉?#28051;?#37722;岀?#35826;?#23340;殑甯?#37510;?/span>
     DATA锛?#37922;?#27996;庢惡甯?#28051;?#31885;闀垮害涓?#28729;氱殑瀛?#38010;?#37510;?#28051;涓垨澶?#28051;狣ATA?#24434;浣?#28051;?#29831;锋?#20635;垨鍝嶅簲?#27537;杞借嵎銆?/span>
     HEADERS锛?#37922;?#27996;庡紑鍚?#28051;涓?#23092;?#38171;屽?#36346;彲鎼?#29999;?#28051;涓?#28598;撮儴鍧?#37911;?#37826;?#37510;傚?#25774;儴鍧楁?#22249;敱涓涓狧EADERS/PUSH_PROMISE甯?#37724;?#32497;殢瀹冪殑闆跺埌澶?#28051;狢ONTINUATION甯?#32513;勬?#24878;殑闆?#37722;?#38171;?#37733;?#28051;?#37721;?#37832;?#28729;?#28000;?#37813;嶅彲鑳芥惡甯?#28598;撮儴淇伅銆傝繖涓?#38342;?#37722;堝彲琚?#37714;嗗壊鎴?#28051;涓垨涓缁勫瓧鑺?#38171;岃繖鏍风殑瀛?#38010;傝绉?#28051;?#28598;撮儴鍧?#37911;?#37826;?#37510;傚?#25774;儴鍧?#28051;?#37722;?#28051;?#37911;?#28729;?#32491;诲?#23340;殑甯?#36423;呴?#33636;揣绱浉閭?#38171;?#28051;?#38003;藉?#34425;幇鍏?#28729;冪?#35826;?#23340;殑甯?#37510;?/span>
     PRIORITY锛?#37922;?#27996;庢寚瀹氬彂閫佺?#32531;璁殑娴佷紭鍏堢骇銆?/span>
     RST_STREAM锛?#37922;?#27996;?#32468;?#37719;?#32513;?#23005;?#23092;?#37510;傚綋甯?#37832;涘?#26664;秷涓涓?#23092;佹垨?#24386;鐢?#38335;?#29831;?#37827;?#38171;屽氨?#24434;?#24386;閫丷ST_STREAM甯?#37510;?/span>
     SETTINGS锛?#37922;?#27996;庢惡甯彲浠?#35120;?#37725;?#28051;?#32468;箣闂?#38315;氫俊鏂瑰?#24533;殑閰嶇疆鍙傛暟銆係ETTINGS甯?#28729;氫箟?#31777;涓涓狝CK?#29219;蹇?#38171;?#37922;?#27996;庢?#22249;璇?#29999;?#37813;璁剧疆?#27537;鍙傛暟鏄?#37722;?#23480;?#29722;?#37818;敹绔?#38014;风煡銆傚綋?#25977;?#22476;涓涓猄ETTINGS涓?#37711;?#28051;殑ACK?#29219;蹇?#28051;?鏃?#38171;?#37818;敹绔?#36423;呴?#35826;?#34249;彲鑳藉揩?#27537;?#31794;鐢?#37711;?#28051;?#23480;?#29722;洿鏂扮殑鍙傛暟銆?/span>
     PUSH_PROMISE锛?#37922;?#27996;?#37722;?#37818;敹绔?#38315;氱煡?#24386;閫佺?#30370;瑕佸?#28056;缓?#27537;娴?#37510;傚綋鎺敹绔?#37818;敹?#22476;璇?#29999;?#37827;?#38171;?#37826;扮殑娴佸皻鏈?#29722;彂閫佺鍒涘缓锛屼?#21975;彂閫佺鎵?#29831;?#28028;氬?#28056;缓璇?#23092;?#37510;?#29831;?#29999;?#37922;?#27996;?#28729;炵幇HTTP/2?#27537;?#22136;瑕佺壒鎬?鏈嶅姟鍣?#32468;?#37818;?#38315;?Server Push)"銆?/span>
     PING锛?#37922;?#27996;?#23092;嬮?#24531;彂閫佺涓?#37818;敹绔箣闂寸殑鏈灏?#23536;杩?#37827;?#38338;?#37510;傝繖涓庝娇鐢?#28028;?#37813;鍛煡?#27537;ping鍛?#28000;殑鐩殑?#27977;浼?#38171;?#37828;?#28051;轰簡娴?#29831;曟煇涓?#32460;?#38338;茬殑杩?#37818;?#37828;?#37722;?#26473;樺彲鐢?#37510;?/span>
     GOAWAY锛?#37922;?#27996;庡彂璧?#28725;?#26473;?#37818;殑鍏?#38338;?#38171;屾垨瑙彂涓?#38322;嶇殑閿?#29831;?#37833;?#28000;?#37510;?#29831;?#29999;厑璁?#28051;绔?#38171;?#37734;?#28729;?#37812;?#28725;逛箣鍓嶅凡鍒涘缓?#27537;娴佺殑澶勭?#21977;殑鍚?#37827;?#38171;?#28028;?#38342;?#37734;板仠姝?#37818;?#37824;舵?#25198;殑娴?#37510;?#28051;绔?#37734;?#37714;涘缓鏂扮殑娴?#38171;屽彟涓绔?#37734;彂閫丟OAWAY锛岃繖涓?#38000;呬箣闂?#28598;?#37906;跺瓨鍦潃绔?#27996;?#37711;?#32495;?#37510;?#28051;轰簡灏?#28725;?#26473;?#32457;嶆?#21613;喌锛屽彂閫佺鍦彂閫丟OAWAY鏃?#28028;?#29825;?#28729;冩惡甯?#28051;?璇彂閫佺鎵?#29025;?#27325;?#27537;)鎺敹绔?#37832;鍚庡?#28056;缓?#27537;娴佺殑?#29219;璇嗙锛屽綋璇GOAWAY琚彂閫佷箣鍚?#38171;屽彂閫佺?#30370;浼氬拷瑙?#37922;?#37818;敹绔?#37714;涘缓?#27537;浠讳綍涓涓爣璇嗙?#30254;璇爣璇嗙澶殑娴?#37510;?/span>
     WINDOW_UPDATE锛?#37922;?#27996;?#23092;侀噺鎺?#37714;?#37510;?#29831;?#29999;殑杞借嵎鐢?#28051;涓?#37719;曟瘮鐗?#28103;濈暀浣嶅拰涓涓?1?#30254;鐗?#28003;嶇殑鏃?#32471;?#37721;锋暣鏁?#32513;勬垚銆?#29831;暣鏁?#37722;?#29831;?#29999;殑鎺敹绔?#37816;囩?#36720;簡鍏?#37722;戝綋鍓?#23092;侀噺鎺?#37714;剁?#26944;彛鎵鑳藉鍔?#28028;?#26440;?#38322;忕殑鍊?#37510;?/span>
     CONTINUATION锛?#37922;?#27996;?#32513;?#32513;彂閫?#28598;撮儴鍧?#37911;?#37826;?#37510;傚彧瑕?#37722;?#28051;涓?#23092;佷腑鍓?#38344;殑甯?#37828;疕EADERS锛孭USH_PROMISE?#22440;CONTINUATION锛屽苟涓?#29831;?#29999;?#23052;?#37832;?#29825;剧疆END_HEADERS?#29219;蹇?#38171;?#38317;?#28052;堝彲鏃?#38340;?#38322;?#37734;板彂閫丆ONTINUATION甯?#37510;?/span>
     ?#20788;鍒?#29999;?#38171;孌ATA锛孒EADERS鍜孭USH_PROMISE锛岀殑杞借嵎涓彲鑳藉寘鍚?#28617;?#37927;?Padding)銆傚鐧?#37734;?#28051;氬姟涓婃病鏈?#28729;?#38340;呯殑鐢?#28598;?#38171;?#28729;冪殑鍑虹幇鏄熀浜?#28729;?#37711;?#37929;殑銆傛瘮濡?#38171;屽彲浠?#37922;?#28729;冩潵鎵?#28052;?#28729;?#38340;?#37825;版嵁?#27537;闀垮害锛?#28000;?#37713;?#26462;?#37911;?#28729;氱殑HTTP鏀诲嚮銆?br />
     ?#24386;閫佺?#24386;閫佺殑甯殑鏈澶?#38336;垮害瑕佸?#23111;噸鎺敹绔?#29825;?#28729;氱殑SETTINGS_MAX_FRAME_SIZE?#27537;鍊?#37510;備絾璇?#37706;肩殑?#23510;鍥?#29781;?#28000;?#27996;?^14鑷?^24-1涓?#28699;?#38010;備箣闂?#37510;?/span>

4. 娴?Stream)
     娴?#37828;?#37922;?#27996;?#37734;?#28729;埛绔?#28051;?#37832;嶅姟鍣?#32468;箣闂?#26473;?#29723;?#29999;?#28028;?#38315;佺殑閫氶亾锛?#37722;?#28051;涓猅CP杩?#37818;?#28051;彲浠?#37722;?#37827;舵湁澶?#28051;?#23092;?#38171;?#28641;?#28051;?#37733;?#37813;绀?#38171;?br />
┌────────┐          Connection           ┌────────┐
│        │ ============================= │        │
│        │    --------------------- <-- Stream    │
│        │    ┌─────┐┌─────────┐┌─┐      │        │
│        │    └─────┘└─────────┘└─┘ <-- Frame     │
│        │    ---------------------      │        │
│ Client │                               │ Server │
│        │    ----------                 │        │
│        │    ┌──┐┌────┐                 │        │
│        │    └──┘└────┘                 │        │
│        │    ----------                 │        │
│        │ ============================= │        │
└────────┘                               └────────┘
鏈嶅姟鍣?#32468;?#37724;?#28729;埛绔彲浠?#27996;?#38335;?#37734;?#37722;?#37722;?#28051;涓?#26473;?#37818;?#28051;殑涓?#37722;?#23092;佷腑浼?#38315;?#29999;?#37510;傚彲浠?#37814;?#28051;涓?#23092;佺湅浣HTTP/1涓殑涓涓?#26473;?#37818;?#37510;傚?#22491;绔?#28051;?#37832;嶅姟鍣?#32468;?#37734;?#37722;?#28051;涓?#23092;佷腑?#27537;浜?#27996;?#28186;濈劧閬?#23536;彂閫?#29831;锋眰-绛?#23536;呭?#23941;簲妯?#23534;?#37510;?#28051;?#32468;?#38318;藉彲浠?#37714;涘缓鏂扮殑娴?#38171;屽?#21464;韩瀵?#37826;瑰?#28056;缓?#27537;娴?#38171;?#28052;熷彲浠?#37711;?#38338;?#28725;?#37826;瑰?#28056;缓?#27537;娴?#37510;傚抚鍦?#23092;佷腑?#27537;椤哄簭鏄?#37832;夋?#24526;箟?#27537;锛?#37818;敹绔?#28028;?#28000;?#37818;敹?#22476;?#27537;椤哄簭鍘诲?#21229;悊甯?#37510;?br />      姣?#28051;?#23092;?#38318;?#37832;?#28051;涓爣璇嗙锛?#37828;?#28051;涓?1?#30254;鐗?#28003;嶇殑鏃?#32471;?#37722;堟暣鏁?#37510;傚湪鍚?#28051;涓?#26473;?#37818;?#28051;?#38171;?#23092;佹爣璇嗙鏄敮涓?#27537;銆?#37922;?#28729;埛绔?#37714;涘缓?#27537;娴佺殑?#29219;璇嗙涓哄?#22248;暟锛?#37922;?#37832;嶅姟鍣?#37714;涘缓?#27537;娴佺殑?#29219;璇嗙涓哄?#33333;暟銆備?#21976;爣璇嗙涓??#27537;娴佸彲鐪?#28003;?#26473;?#37818;?#38171;?#37922;?#27996;?#26473;?#37818;?#37818;?#37714;?#28103;伅锛屽?#28056;缓鏂扮殑娴佹椂涓嶅彲?#23047;鐢?#29831;爣璇嗙銆?#37722;?#28051;涓?#26473;?#37818;?#28051;殑浠讳綍涓涓?#23092;佺殑?#29219;璇嗙閮?#28051;嶅彲?#22136;鐢?#38171;?#37719;?#28186;?#26473;?#28051;?#23092;佸凡琚?#37711;?#38338;簡銆傚浜?#38336;?#37827;?#38338;存病鏈?#28051;?#37826;殑杩?#37818;?#38171;屽彲鑳?#28028;氬?#34425;幇?#29219;璇嗙涓嶅鐢殑鎯呭喌锛?#38317;?#37827;跺氨蹇呴?#35826;己鍒跺?#28056;缓涓涓?#37826;扮殑杩?#37818;?#37510;?br />      HTTP/2鍗忚涓?#23092;佺殑鐢?#37723;?#37723;?#37832;?#28729;氫箟?#31777;7绉?#37912;舵?sup>(2)锛歩dle锛宺eserved(local)锛宺eserved(remote)锛宱pen锛宧alf closed(local)锛宧alf closed(remote)鍜宑losed銆傚綋涓绔?#37818;?#37824;舵垨?#24386;閫?#28598;撮儴鍧楁垨(甯DATA鍜孒EADERS?#27537;)?#29219;蹇RST_STREAM鍚庡彲?#23047;娴佺殑鐘舵佸彂鐢?#26462;彉銆?/span>
     ?#23047;鐢?#23092;佹潵瀹炵幇澶氬伐灏?#28028;氬紩璧?#28725;筎CP杩?#37818;娇鐢殑绔?#27996;?#38171;岃繖浼?#38315;?#37812;?#23092;佺殑闃诲銆傚熀浜?#29999;WINDOW_UPDATE?#27537;娴侀噺鎺?#37714;舵?#35268;?#22557;彲浠?#32429;?#28103;濈浉鍚岃繛鎺?#28051;殑娴佺浉浜掍箣闂?#28051;?#28028;氫骇鐢熺牬鍧?#37804;?#39582;?#37813;?#37510;?#23092;侀噺鎺?#37714;跺彲浠?#28003;?#37922;?#27996;?#28051;?#28051;眰闈?#38171;?#37719;?#37719;?#28051;?#23092;佹垨?#26275;涓?#26473;?#37818;?#37510;傚彧鏈?#29999;DATA?#28214;瑕侀伒瀹?#23092;侀噺鎺?#37714;?#38171;?#37813;鏈?#37711;?#28729;冪殑甯?#37813;鏈夋?#22563;楃殑绌?#38338;?#37735;?#28051;?#28028;氬崰鐢?#23092;侀噺鎺?#37714;剁?#26944;彛銆侶TTP/2鍗忚鍙?#37828;?#28729;氫箟?#31777;WINDOW_UPDATE甯殑缁?#37835;勫?#23683;?#31647;锛?#37719;忚?#27537;瀹炵幇?#24434;浠?#38315;夋嫨浠讳綍閫?#37922;嚜宸辩殑娴侀噺鎺?#37714;剁?#26945;硶銆?/span>
     娴佸彲浠?#37832;?#28028;?#37711;堢骇銆傚?#22491;绔?#37734;?#37714;涘缓涓涓?#37826;扮殑娴佹椂锛屽彲鍦HEADERS涓寚瀹?#28028;?#37711;堢骇鏉冮噸銆傚湪鍚?#32513;?#28000;讳綍鏃?#38338;?#38171;?#38315;?#26473;PRIORITY?#24434;浠?#37824;瑰彉娴佺殑浼?#37711;堢骇鏉冮噸銆傚湪骞跺彂鑳藉姏鏈?#38340;愮殑鎯呭喌涓?#38171;岄?#27195;潈?#22136;娴佺殑甯?#28028;氳浼?#37711;?#28028;?#38315;?#37510;傛潈閲嶇殑鍊?#36423;呴?#35763;粙浜?鑷?56?#31651;闂?#38171;岄粯璁?#37833;冮噸涓?6銆?/span>娴佷笌娴佷箣闂?#26473;樺彲浠?#37832;変緷璧栧叧绯?#38171;岃繖绉嶅叧绯讳細缁勬垚涓妫?#28186;?#29863;栧叧绯?#37837;?#37510;?#28051;涓?#23092;?#38003;藉?#29112;寚瀹氳嚜宸?#37812;?#28051;?#37721;?#28051;涓?#23092;佺殑瀛?#23092;?#37510;傝繖涓杩囩锛屽彲浠?#37828;?#38344;?#37818;?#28000;栫殑锛?#28052;熷彲浠?#37828;?#37818;?#28000;栫殑銆?#38344;?#37818;?#28000;?#37804;?#28186;?#29863;?#38171;?#37828;寚涓涓?#23092;?#37734;皢?#22172;宸卞彉鎴?#37721;?#28051;涓?#23092;佺殑瀛?#23092;佺殑杩囩涓?#38171;屽厑璁?#37721;?#28051;涓?#23092;?#26473;?#37832;?#37714;殑瀛?#23092;?#38171;?#37719;冲厑璁?#37832;夎嚜宸辩殑鍏勫紵娴佸瓨鍦?#37510;?#37818;?#28000;?#37804;?#28186;?#29863;?#38171;屾寚鍦?#37715;嶈堪杩囩涓?#38171;?#28051;嶅厑璁?#37721;?#28051;涓?#23092;?#26473;?#37832;?#37714;殑瀛?#23092;?#37510;傚?#20635;?#28355;彟涓涓?#23092;佸凡缁?#37832;?#28699;?#23092;佷簡锛?#38317;?#28052;?#29831;?#23092;佷?#27693;妸鎵鏈夋綔鍦殑鍏勫紵娴佸?#22557;彉鎴愯嚜宸辩殑瀛?#23092;?#38171;岀劧鍚庡?#23940;娇?#22172;宸?#37812;?#28051;?#37721;?#28051;涓?#23092;佺殑?#25966;涓瀛?#23092;?#37510;傚叾瀹?#38171;?#37818;?#28000;?#37804;?#28186;?#29863;栫殑浣?#37922;?#28751;?#37828;?#28051;轰簡鑳藉鎵?#37934;?#23480;?#37832;夌殑鍏?#32495;?#37837;?#38171;?#37734;?#37827;?#37812;愮殑鐖跺瓙鑺傜偣涓彃鍏?#37826;扮殑鑺傜偣銆?#37722;?#37714;?#38171;屽彧鑳?#28051;?#23480;?#37832;夎?#20636;?#35268;?#35826;?#29362;瓙鑺傜偣锛?#38317;?#28052;?#37711;?#32495;?#37837;戝皢涓嶅彲鑳?#26473;?#29723;岄噸鏋?#37510;?#37813;鏈夌殑娴?#37734;?#29722;?#37714;涘缓鏃?#38171;岄粯璁?#37812;?#28051;烘爣璇嗙涓?x0?#27537;娴佺殑瀛?#23092;?#37510;傚湪"鏈嶅姟鍣?#32468;?#37818;?#38315;?涓?#37922;?#37812;愮殑"鎺?#38315;?娴佸皢?#22172;鍔?#37734;?#37812;?#28051;?#37922;?#37812;?#29831;?#37818;?#38315;?#23092;佺殑娴佺殑瀛?#23092;?#38171;屽?#22549;粯璁?#37833;冮噸涔?#28051;?6銆?/span>

5. 娑堟伅浜?#37817;?/span>
5.1 璇锋眰/鍝嶅簲浜?#37817;?/span>
     HTTP/2娌?#29722;簡HTTP/1?#27537;璇箟锛?#37719;?#37813;鏈夌殑璇锋眰涓庡?#23941;簲璇箟鍧?#23536;楀埌?#31777;淇濈暀锛屽敖绠?#28028;?#38315;?#26473;?#27996;?#29831;箟?#27537;璇?#23049;?#23480;?#32513;?#37824;瑰彉?#31777;銆?/span>
     涓涓狧TTP/2娑堟伅鐢?#28641;?#28051;嬪嚑涓儴鍒?#32513;勬垚锛?/span>
     [1]浠呭浜庡?#23941;簲娑堟伅锛屽彲浠寘鍚?#28051;涓?#37820;?#29999;?#37832;?xx鍝嶅簲澶撮儴?#27537;澶撮儴鍧?#37510;?#29831;?#28598;撮儴鍧?#37922;?#28051;涓狧EADERS甯?#37724;?#32497;殢瀹冪殑闆跺埌澶?#28051;狢ONTINUATION甯?#32513;勬垚銆?/span>
     [2]涓涓?#28598;撮儴鍧?#37510;?#29831;?#28598;撮儴鍧?#37922;?#28051;涓狧EADERS甯?#37724;?#32497;殢瀹冪殑闆跺埌澶?#28051;狢ONTINUATION甯?#32513;勬垚銆?/span>
     [3]闆跺埌澶?#28051;?#37820;?#29999;?#37832;変綋?#20788;(Body)娑堟伅?#27537;DATA甯?#37510;侶TTP/1涓娇鐢殑"鍒?#37735;?chunked)"?#32139;?#20788;?#30370;涓?#38315;?#37922;?#27996;嶩TTP/2銆?#37733;?#28051;?#28051;涓綋?#20788;?#24434;鐢?#28598;?#28051;狣ATA甯?#32513;勬垚锛?#37813;浠HTTP/2?#27537;?#32139;?#20788;澶?#37906;跺氨鏄彲鍒?#37735;楃殑銆?/span>
     [4]涓涓彲鑳藉瓨鍦殑?#23512;鍚潃灏鹃儴娑堟伅?#27537;澶撮儴鍧?#37510;?#29831;?#28598;撮儴鍧?#37922;?#28051;涓狧EADERS甯?#37724;?#32497;殢瀹冪殑闆跺埌澶?#28051;狢ONTINUATION甯?#32513;勬垚銆?br />
     HTTP/2浠嶇?#33333;部鐢HTTP/1涓殑澶撮儴瀛楁锛屼?#21975;?#26945;鍚?#32457;?#28051;殑瀛楁?#23941;?#21620;?#35826;叏?#20788;涓?#28751;?#37712;?#37510;傚彟澶?#38171;岃?#27194;皢HTTP/1娑堟伅寮濮?#29723;?璇锋眰涓殑璇锋?#20637;涓庡?#23941;簲涓殑鐘舵佽)涓殑娑堟伅锛屽垎瑙?#37812;愪簡?#23266;骞?#28028;?#28598;撮儴瀛楁锛屾绫诲?#26945;鍧?#28000;?#37712;?#37721;?:)寮澶?#37510;?/span>
     HTTP/1璇锋?#20637;鏍?#23534;?#28051;?method request-target HTTP-version"锛屽?#29808;簲?#27537;HTTP/2浼?#28598;撮儴瀛楁鏈?method=method鍜?path=request-target锛屼絾HTTP-version鏃?#28725;瑰簲瀛楁锛岄粯璁?#28051;篐TTP/2銆?/span>
     HTTP/1鐘舵佽鏍?#23534;?#28051;?HTTP-version status-code reason-phrase"锛屽?#29808;簲?#27537;HTTP/2浼?#28598;撮儴瀛楁鏈?status=status-code銆備絾HTTP-version鏃?#28725;瑰簲瀛楁锛岄粯璁?#28051;篐TTP/2锛況eason-phrase涔?#37827;?#28725;瑰簲瀛楁锛?#37733;?#28051;哄彲浠?#38315;?#26473;?#37912;舵?#28000;?#37934;佹煡鎵惧埌鍏跺?#29808;簲?#27537;reason-phrase銆侶TTP/2鍗忚鏄?#37734;?#28751;?#38322;?#37713;?#28751;?#37712;?#28003;欐?#22559;伅銆?/span>
     HTTP/2鍗忚杩?#28051;?#29831;锋?#20634;?#25774;儴瀹氫箟?#31777;鍙?#28598;?#28051;?#28051;?#28028;?#28699;楁锛?/span>
     :scheme锛歎RI涓殑scheme?#20788;鍒?#37510;傚畠?#24434;浠?#28051;?#28000;?#28000;?#37828;http?#22440;https锛?#37733;?#28051;?#37832;?#37827;跺欏彲鑳?#28028;?#28051;?#38344;HTTP鏈嶅姟杩?#29723;?#27996;?#27996;?#37510;?/span>
     :authority锛歎RI涓殑鎺?#37833;冮儴鍒?#37510;傚嵆锛宻cheme://user:password@host:port/path?query#fragment涓殑"user:password@host:port"銆?/span>
     HTTP/2鍗忚8.1.3鑺?#28051;?#32513;?#37713;?#28051;浜涚畝鍗?#32448;?#28186;?#38171;屽睍绀轰簡濡備綍?#30370;HTTP/1娑堟伅瀵瑰簲?#22476;HTTP/2娑堟伅銆?/span>
5.2 鏈嶅姟鍣?#32468;?#37818;?#38315;?/span>
     HTTP/2?#27537;鏈嶅姟鍣?#32468;?#37818;?#38315;?#37828;?#28028;?#32513;熺殑璇锋眰/鍝嶅簲妯?#23534;忕殑涓绉?#37911;?#23048;?#35120;?#23534;?#37510;?#37832;嶅姟鍣?#32468;?#37734;敹?#22476;瀹埛绔殑璇锋眰(涓?#29831;锋眰)?#31651;鍚?#38171;?#28051;轰簡涓诲姩鍚?#28729;埛绔?#37818;?#38315;佹洿澶氱殑鍐?#28729;?#38171;?#28028;氳嚜鍔?#37734;?#37922;?#37812;愯嫢骞?#37826;扮殑璇锋眰(鎺?#38315;?#29831;锋眰)銆?#37832;嶅姟鍣?#37722;?#28729;埛绔彂閫佺殑鍝嶅簲涓?#38171;?#28051;?#28000;呭寘鍚?#28725;?#28051;?#29831;锋?#20636;殑鍝嶅簲(涓诲?#23941;簲)锛岃?#27194;寘鍚?#28725;?#37818;?#38315;?#29831;锋?#20636;殑鍝嶅簲(鎺?#38315;佸?#23941;簲)銆?/span>
     瀹埛绔彲浠?#38315;?#26473;囧彂閫佸寘鍚?#37832;SETTINGS_MAX_CONCURRENT_STREAMS鍙傛?#25198;殑SETTINGS甯?#37720;荤?#20346;敤鏈嶅姟鍣?#32468;?#37818;?#38315;?#38171;?#28052;熷彲浠?#38315;?#26473;囧彂閫丷ST_STREAM甯?#37720;诲?#26664;秷宸?#32513;忓彂璧风殑鏈嶅姟鍣?#32468;?#37818;?#38315;?#38171;屼絾涓?#38003;藉彂閫佸寘鍚?#37832;塃ND_STREAM?#29219;蹇楃殑甯?#37510;?br />
(1)"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"涓殑"PRI"涓?SM"鍚?#29863;锋潵灏?#37828;?RRISM(妫?#38336;?"銆傚懙鍛?#38171;孒TTPbis宸?#28003;滅粍杩?#37828;?#37807;?#29723;?#26440;?#28000;涔堟剰鎬?#37723;?;-)
(2)鏈?#32495;诲垪?#27537;鍚?#32513;?#37826;囩珷瑙?#29831;讳簡娴佺殑鐘舵?#37510;?br />


John Jiang 2016-09-19 11:36 ?#24386;琛?#29831;?#29825;?/a>
]]>Play OpenJDK: ?#21393;璁?#28003;犵殑?#23512;鍚?#28000;?java."寮澶?鍘?http://www.8634070.com/jiangshachina/archive/2015/11/01/428010.htmlJohn JiangJohn JiangSun, 01 Nov 2015 12:06:00 GMThttp://www.8634070.com/jiangshachina/archive/2015/11/01/428010.htmlhttp://www.8634070.com/jiangshachina/comments/428010.htmlhttp://www.8634070.com/jiangshachina/archive/2015/11/01/428010.html#Feedback0http://www.8634070.com/jiangshachina/comments/commentRss/428010.htmlhttp://www.8634070.com/jiangshachina/services/trackbacks/428010.html
Play OpenJDK: ?#21393;璁?#28003;犵殑?#23512;鍚?#28000;?java."寮澶?/span>

鏈?#37826;?#37828;疨lay OpenJDK?#27537;绗?#27996;岀瘒锛?#28000;?#32513;嶄簡濡備?#26336;?#20346;牬JDK涓嶅厑璁歌嚜瀹氫箟?#27537;?#23512;鍚?#28000;?java."寮澶?#26473;?#28051;闄?#37714;?#37510;傝繖涓鎶宸?#28725;?#27996;庡熀浜庡凡鏈夌殑JDK鍚慾ava.*涓?#23075;诲姞鏂扮被杩?#37828;?#37832;?#37813;甯?#37716;殑銆?2015.11.02鏈鍚庢洿鏂?

鏃?#29825;?#37828;?#32513;?#26976;?#28051;?#28725;岀殑Java绋嬪簭鍛?#38171;岃繕鏄疛ava?#27537;鍒?#28699;?#38000;?#38171;?#37804;讳細鏈?#28051;浜?#27996;烘垨鏈夋剰?#22440;鏃犳剰鍦?#37714;涘缓涓涓寘鍚?#28051;?java"?#27537;?#34987;銆備?#21975;嚭浜?#28729;?#37711;?#37826;?#38344;殑鑰?#38095;?#38171;孞DK涓嶅厑璁稿簲鐢?#32459;嬪簭绫荤殑?#23512;鍚?#28000;?java"寮澶?#38171;?#37719;?#28051;嶅厑璁java锛宩ava.foo杩?#37837;风殑?#23512;鍚?#37510;備絾javax锛宩avaex杩?#37837;风殑?#23512;鍚?#37828;厑璁哥殑銆?br />
1. 渚嬪瓙
?#30254;濡?#38171;?#28000;OpenJDK 8涓哄熀纭锛岃噯閫?#26473;?#37837;?#28051;涓?#28186;嬪瓙銆傜瑪鑰呮兂鍚慜penJDK璐尞涓涓?#37722;屾?#27537;HashMap锛?#37719;崇被SynchronizedHashMap锛岃岃绫荤殑?#23512;鍚嶅氨涓簀ava.util銆係ynchronizedHashMap鏄疕ashMap?#27537;鍚屾浠悊锛?#37922;变簬杩?#28051;?#28051;被鏄?#37734;?#37722;?#28051;?#23512;鍐?#38171;孲ynchronizedHashMap涓?#28000;呭彲浠?#29825;?#38338;瓾ashMap?#27537;public鏂规硶涓庡彉閲?#38171;岃?#27194;彲浠?#29825;?#38338;瓾ashMap?#27537;protected鍜宒efault鏂规硶涓庡彉閲?#37510;係ynchronizedHashMap鐪?#29863;锋潵?#24434;鑳藉儚涓?#38344;?#26473;?#37837;?#38171;?br />
package java.util;

public class SynchronizedHashMap<K, V> {

    
private HashMap<K, V> hashMap = null;

    
public SynchronizedHashMap(HashMap<K, V> hashMap) {
        
this.hashMap = hashMap;
    }

    
public SynchronizedHashMap() {
        
this(new HashMap<>());
    }

    
public synchronized V put(K key, V value) {
        
return hashMap.put(key, value);
    }

    
public synchronized V get(K key) {
        
return hashMap.get(key);
    }

    
public synchronized V remove(K key) {
        
return hashMap.remove(key);
    }

    
public synchronized int size() {
        
return hashMap.size; // ?#27967;鎺?#29835;?#37922;HashMap.size?#24393;閲?#38171;岃岄潪HashMap.size()鏂规硶
    }
}

2. ClassLoader?#27537;闄?#37714;?/span>
?#23047;鐢javac鍘荤紪璇?#23143;?#37826;?#28000;SynchronizedHashMap.java骞舵病鏈?#38338;?#26864;?#38171;屼絾鍦娇鐢?#32514;?#29831;?#37722;庣殑SynchronizedHashMap.class鏃?#38171;孞DK?#27537;ClassLoader鍒?#28028;氭缁?#37716;?#26462;java.util.SynchronizedHashMap銆?br />璁炬兂鏈?#28641;?#28051;嬬殑?#31794;鐢?#32459;嬪簭锛?br />
import java.util.SynchronizedHashMap;

public class SyncMapTest {

    
public static void main(String[] args) {
        SynchronizedHashMap
<String, String> syncMap = new SynchronizedHashMap<>();
        syncMap.put(
"Key""Value");
        System.out.println(syncMap.get(
"Key"));
    }
}
?#23047;鐢java鍛?#28000;?#37720;?#26473;?#29723;岃?#31794;鐢?#37827;?#38171;?#28028;氭姤濡?#28051;嬮敊璇?#38171;?br />
Exception in thread "main" java.lang.SecurityException: Prohibited package name: java.util
    at java.lang.ClassLoader.preDefineClass(ClassLoader.java:
659)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:
758)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:
142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:
467)
    at java.net.URLClassLoader.access$
100(URLClassLoader.java:73)
    at java.net.URLClassLoader$
1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$
1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:
361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:
424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:
331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:
357)
    at SyncMapTest.main(SyncMapTest.java:
6)
鏂规硶ClassLoader.preDefineClass()?#27537;婧?#28000;爜濡?#28051;?#38171;?br />
private ProtectionDomain preDefineClass(String name,
        ProtectionDomain pd)
{
    
if (!checkName(name))
        
throw new NoClassDefFoundError("IllegalName: " + name);

    
if ((name != null&& name.startsWith("java.")) {
        
throw new SecurityException
            (
"Prohibited package name: " +
            name.substring(
0, name.lastIndexOf('.')));
    }
    
if (pd == null) {
        pd 
= defaultDomain;
        }

    
if (name != null) checkCerts(name, pd.getCodeSource());

    
return pd;
}
寰?#23059;呮鍦?#38171;岃鏂规硶浼氬?#22559;鏌?#23536;呭姞杞界殑绫诲叏鍚?鍗冲寘鍚?绫诲悕)鏄?#37722;?#28000;?java."寮澶?#38171;?#28641;?#37828;?#38171;屽垯鎶涘嚭SecurityException銆?#38317;?#28052;堝彲浠?#28751;?#29831;?#28103;?#37824;?#29831;?#37826;规?#26336;殑婧?#28000;爜锛?#28000;?#32464;佺牬杩?#28051;闄?#37714;?#37510;?br />浠嶫DK涓殑src.zip涓?#37815;垮嚭java/lang/ClassLoader.java鏂?#28000;?#38171;屼慨鏀瑰叾涓殑preDefineClass鏂规硶浠?#37720;?#38340;浉鍏?#38340;?#37714;?#37510;傞噸鏂扮紪璇慍lassLoader.java锛屽皢鐢?#37812;愮殑ClassLoader.class锛孋lassLoader$1.class锛孋lassLoader$2.class锛孋lassLoader$3.class锛孋lassLoader$NativeLibrary.class锛孋lassLoader$ParallelLoaders.class鍜孲ystemClassLoaderAction.class鍘绘浛鎹JDK/jre/lib/rt.jar涓?#28725;瑰簲?#27537;?#34987;銆?br />鍐嶆杩?#29723;孲yncMapTest锛?#37719;?#28000;嶇劧浼氭?#28056;?#34425;浉鍚岀殑SecurityException锛?#28641;?#28051;?#37813;绀?#38171;?br />
Exception in thread "main" java.lang.SecurityException: Prohibited package name: java.util
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:
760)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:
142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:
467)
    at java.net.URLClassLoader.access$
100(URLClassLoader.java:73)
    at java.net.URLClassLoader$
1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$
1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:
361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:
424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:
331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:
357)
    at SyncMapTest.main(SyncMapTest.java:
6)
姝?#37827;舵槸鐢?#37826;规硶ClassLoader.defineClass1()鎶涘?#34425;殑SecurityException銆備?#21978;繖鏄?#28051;涓猲ative鏂规硶锛?#38317;?#28052;?#28000;?#38315;?#26473;?#28103;?#37824;笿ava浠爜鏄?#37827;犳硶瑙?#37712;?#26473;?#28051;?#38338;?#26864;樼殑(JDK鐪?#37828;?#28766;傚?#20637;闃插)銆?#37720;熸潵鍦Hotspot?#27537;C++婧?#37826;?#28000;秇otspot/src/share/vm/classfile/systemDictionary.cpp涓?#37832;?#28641;?#28051;?#29831;?#37721;?#38171;?br />
const char* pkg = "java/";
if (!HAS_PENDING_EXCEPTION &&
    !class_loader.is_null() &&
    parsed_name !
= NULL &&
    !strncmp((const char*)parsed_name->bytes()
, pkg, strlen(pkg))) {
  // It is illegal to define classes in the 
"java." package from
  // JVM_DefineClass or jni_DefineClass unless you're the bootclassloader
  ResourceMark rm(THREAD)
;
  char* name = parsed_name->as_C_string();
  char* index = strrchr(name, '/');
  *index = '\0'; // chop to just the package name
  while ((index = strchr(name, '/')) != NULL) {
    *index 
= '.'; // replace '/' with '.' in package name
  }
  const char* fmt 
= "Prohibited package name: %s";
  size_t len = strlen(fmt) + strlen(name);
  char* message = NEW_RESOURCE_ARRAY(char, len);
  jio_snprintf(message, len, fmt, name);
  Exceptions::_throw_msg(THREAD_AND_LOCATION,
    vmSymbols::java_lang_SecurityException()
, message);
}
淇?#37824;?#29831;?#37826;?#28000;?#28000;?#37720;?#38340;?#37818;夌浉鍏?#38340;?#37714;?#38171;屽?#33333;?#22796;収鏈?#32495;诲垪?#27537;
绗?#28051;?#30226;鏂囩珷涓?#28000;?#32513;嶇殑鏂规硶鍘婚噸鏂?#37835;勫缓涓涓狾penJDK銆?#38317;?#28052;?#38171;岃繖涓?#37826;扮殑JDK?#30370;涓?#28028;氬?#23941;?#29808;寘鍚?#37832;?#28000;讳綍闄?#37714;朵簡銆?br />
3. 瑕?#37929;朖ava鏍稿績API锛?/strong>
寮?#24386;鑰?#28000;?#37734;娇鐢?#28051;?#23092;両DE鏃?#28028;氬彂?#24135;锛?#28641;傛灉宸?#32459;?#37832;?#28598;?#28051;jar鏂?#28000;舵垨婧?#37826;?#28000;剁洰褰?#28051;寘鍚浉鍚岀殑?#34987;锛岃繖浜IDE浼氭?#35268;嵁鐢埛?#23514;瀹氱殑浼?#37711;堢骇椤哄?#24532;潵鍔?#26462;?#26473;?#27996;涚被銆傛瘮濡?#38171;?#37734;Eclipse涓?#38171;屽彸閿?#37904;瑰?#32472;煇涓狫ava宸?#32459;?->灞?#37804;?->Java Build Path-->Order and Export锛?#37734;?#26473;?#38322;岃?#20905;暣鍚?#28051;?#32491;诲?#25780;垨婧?#37826;?#28000;剁洰褰曠殑浣嶇疆锛?#37719;冲彲?#23514;瀹氬姞杞?#32491;荤殑浼?#37711;堢骇銆?br />?#32139;寮?#24386;鑰?#37734;娇鐢煇涓?#23534;婧愮?#35826;簱(jar鏂?#28000;?鏃?#38171;屾兂瀵瑰叾涓煇涓被杩?#29723;屼慨鏀?#38171;?#38317;?#28052;?#28751;卞彲浠皢璇?#32491;荤殑婧?#28000;爜澶嶅?#36346;?#28888;潵锛屽苟鍦Java宸?#32459;?#28051;?#37714;涘缓涓涓?#37722;?#37722;嶇被锛岀劧鍚庢寚瀹欵clipse浼?#37711;?#37716;犳伅?#22172;宸?#37714;涘缓?#27537;?#34987;銆傚嵆锛?#37734;?#32514;?#29831;?#37827;?#28051;?#26473;?#29723;?#37827;?#37922;嚜宸?#37714;涘缓?#27537;?#34987;鍘?#29781;?#37929;?#32491;诲簱涓殑鍚?#37722;嶇被銆?#38317;?#28052;?#38171;?#37828;?#37722;彲浠?#28641;傛?#26336;偖鍒?#37720;?#29781;?#37929;朖ava鏍稿績API涓殑绫诲憿锛?br />鑰?#38095;?#37720;?#29781;?#37929;栫被java.util.HashMap锛屽彧鏄?#32480;鍗?#37734;?#28729;冪殑put()鏂规?#26335;?#35826;姞涓鏉?#37813;?#37719;?#29831;?#37510;?#38317;?#28052;?#28751;遍渶瑕佸皢src.zip涓殑java/util/HashMap.java澶嶅?#36346;?#28888;潵锛屽苟鍦綋鍓岼ava宸?#32459;?#28051;?#37714;涘缓涓涓?#37722;?#37722;嶇被java.util.HashMap锛屽苟淇?#37824;put()鏂规硶锛?#28641;?#28051;?#37813;绀?#38171;?br />
package java.util;

public class HashMap<K,V> extends AbstractMap<K,V>
    
implements Map<K,V>, Cloneable, Serializable {
    .
    
public V put(K key, V value) {
        System.out.printf(
"put - key=%s, value=%s%n", key, value);
        
return putVal(hash(key), key, value, falsetrue);
    }
    
}
姝?#37827;?#38171;?#37734;Eclipse鐜?#28583;?#28051;?#38171;孲ynchronizedHashMap?#23047;鐢殑java.util.HashMap琚?#29825;?#28051;?#37828;?#28051;婅堪鏂?#37714;涘缓?#27537;HashMap?#34987;銆?#38317;?#28052;?#26473;?#29723;屽簲鐢?#32459;嬪簭SyncMapTest鍚庣殑鏈?#37832;?#26440;撳?#21700;簲璇?#28641;?#28051;?#37813;绀?#38171;?br />
put - key=Key, value=Value
Value
浣嗚繍琛孲yncMapTest鍚庣殑瀹?#38340;?#26440;撳嚭鍗?#28051;?#28641;?#28051;?#38171;?br />
Value
鐪?#29863;锋潵锛?#37826;?#37714;涘缓?#27537;java.util.HashMap骞舵病鏈?#29722;娇鐢?#28051;?#37510;傝繖鏄?#28051;?#28000;涔?#37723;?#38171;?#38003;藉"鎯?#37709;??#22476;?#27537;鍘?#37733;?#26473;?#37828;?#32491;诲姞杞?#37731;?#37510;傚叧浜嶫ava绫诲姞杞?#37731;殑璁?#29825;鸿?#21613;?#36720;簡鏈?#37826;囩殑?#23510;鍥?#38171;岃?#28051;?#37711;?#27996;?#29831;?#28051;?#26864;樼殑鏂囩珷宸?#37828;?#23033;?#37911;涘厖鏍?#38171;屼絾鏈?#37826;?#28000;?#28028;?#32480;?#22570;鍏惰?#20346;偣銆?br />Java绫诲姞杞?#37731;?#37922;?#28051;?#38007;?#28051;?#37714;?#28051;?#28051;?#28051;?#28766;傛锛氬紩瀵?#32491;诲姞杞?#37731;?Bootstrap Class Loader)锛?#37813;?#28766;曠?#35826;姞杞?#37731;?Extension Class Loader)鍜屽簲鐢?#32459;嬪簭绫诲姞杞?#37731;?Application Class Loader)銆傚叾涓紩瀵?#32491;诲姞杞?#37731;?#37922;?#27996;庡姞杞絩t.jar杩?#37837;风殑鏍稿?#20906;?#35826;簱銆傚苟涓斿紩瀵?#32491;诲姞杞?#37731;?#28051;?#37813;?#28766;曠?#35826;姞杞?#37731;殑鐖跺姞杞?#37731;?#38171;岃?#37813;?#28766;曠?#35826;姞杞?#37731;?#37721;?#28051;哄簲鐢?#32459;嬪簭绫诲姞杞?#37731;殑鐖跺姞杞?#37731;?#37510;?#37722;?#37827;禞VM鍦?#37716;?#26462;界被鏃?#28729;?#29723;屽鎵樻寮?#37510;傚嵆锛屽綋鍓嶇?#35826;姞杞?#37731;?#37734;?#37716;?#26462;界被鏃?#38171;?#28028;氶?#26663;?#22557;鎵樿嚜宸辩殑鐖跺姞杞?#37731;?#37720;?#26473;?#29723;屽姞杞?#37510;傚?#20635;灉鐖跺姞杞?#37731;?#23480;?#32513;?#37716;?#26462;戒簡?#28999;涓被锛?#38317;?#28052;堝瓙鍔?#26462;?#37731;皢涓?#28028;氬?#23942;鍔?#26462;?#37510;?br />鐢?#28051;婂彲?#29025;锛屽綋?#31794;鐢?#32459;嬪簭璇?#37733;?#37716;?#26462;java.util.Map鏃?#38171;?#28729;?#28028;氶?#26663;厛閫愮骇鍚?#28051;婂鎵樼?#36346;姞杞?#37731;?#37720;诲姞杞?#29831;被锛岀洿?#22476;?#32041;瀵?#32491;诲姞杞?#37731;?#37716;?#26462;藉埌rt.jar涓殑java.util.HashMap銆?#37922;变簬璇?#32491;诲凡缁忚鍔?#26462;戒簡锛?#37812;?#28000;嚜宸?#37714;涘缓?#27537;java.util.HashMap灏?#28051;?#28028;氳閲嶅?#23941;姞杞?#37510;?br />?#23047;鐢java鍛?#28000;?#26473;?#29723;孲yncMapTest绋嬪簭鏃跺姞涓奦M鍙傛暟-verbose:class锛?#28028;?#37734;?#32464;楀彛涓?#37813;?#37719;?#37713;?#35120;?#23534;?#28641;?#28051;嬬殑璇?#37721;?#38171;?br />
[Opened /home/ubuntu/jdk1.8.0_custom/jre/lib/rt.jar]
[Loaded java.lang.Object from /home/ubuntu/jdk1.8.0_custom/jre/lib/rt.jar]

[Loaded java.util.HashMap from /home/ubuntu/jdk1.8.0_custom/jre/lib/rt.jar]
[Loaded java.util.HashMap$Node from /home/ubuntu/jdk1.8.0_custom/jre/lib/rt.jar]

[Loaded java.util.SynchronizedHashMap from file:/home/ubuntu/projects/test/classes/]
Value
[Loaded java.lang.Shutdown from /home/ubuntu/jdk1.8.0_custom/jre/lib/rt.jar]
[Loaded java.lang.Shutdown$Lock from /home/ubuntu/jdk1.8.0_custom/jre/lib/rt.jar]
浠?#28051;彲浠?#37930;嬪嚭锛岀被java.util.HashMap纭?#28729;?#37828;?#28000;巖t.jar涓?#37716;?#26462;藉埌?#27537;銆備絾鐞嗚涓?#38171;屽彲浠?#38315;?#26473;囪嚜瀹氫箟绫诲姞杞?#37731;?#37720;?#37813;?#37934;?#28654;?#37813;樻寮?#38171;岀劧鑰岃繖灏?#37828;?#37721;?#28051;涓?#29831;?#26864;樹簡銆?/div>


John Jiang 2015-11-01 20:06 ?#24386;琛?#29831;?#29825;?/a>
]]>Play OpenJDK: 鏋勫缓浣犺嚜宸辩殑JDK(鍘?http://www.8634070.com/jiangshachina/archive/2015/10/30/427994.htmlJohn JiangJohn JiangFri, 30 Oct 2015 15:17:00 GMThttp://www.8634070.com/jiangshachina/archive/2015/10/30/427994.htmlhttp://www.8634070.com/jiangshachina/comments/427994.htmlhttp://www.8634070.com/jiangshachina/archive/2015/10/30/427994.html#Feedback0http://www.8634070.com/jiangshachina/comments/commentRss/427994.htmlhttp://www.8634070.com/jiangshachina/services/trackbacks/427994.html
Play OpenJDK: 鏋勫缓浣犺嚜宸辩殑JDK

璁?#37714;掍娇Play OpenJDK鎴?#28051;?#28051;?#31885;浠?#32513;嶅?#20633;綍?#23047;鐢?#39582;跺弬涓嶰penJDK椤?#37929;殑绯诲垪鏂囩珷銆?#37832;?#37826;?#37828;?#29831;?#32495;诲垪?#27537;绗?#28051;?#30226;鏂囩珷锛?#28729;冨熀浜嶰penJDK 8?#27537;婧?#28000;爜浠?#32513;嶄簡鏋勫缓涓涓?#28766;?#27996;庤嚜宸辩殑JDK鏂规硶銆?2015.10.30鏈鍚庢洿鏂?

1. OpenJDK
?#27992;缁忕殑Sun Microsystems. Inc锛?#28052;熷氨鏄疛ava璇?#29815;?#27537;?#24386;鏄庤?#38171;屽皢瀹冪殑JDK浠?#37934;佽础?#23582;鍑烘潵锛?#37812;?#32468;嬩簡涓涓?#23534;婧?#26916;?#37929;?#38171;?#37719;?a href="http://openjdk.java.net/">OpenJDK
銆?br />    鍚?#37827;?#38171;?#28729;?#28052;?#37828;?#28051;涓?#32448;?#37718;?#37510;傜浉鍏崇殑鍏?#37721;?#38171;?#32513;勭鍜?#28051;?#27996;?#37734;?#26473;?#28051;?#32448;?#37718;?#28051;?#37719;?#28003;滃紑?#24386;OpenJDK銆?#32448;?#37718;烘?#35268;嵁涓?#37722;岀殑棰嗗?#29112;垨椤?#37929;?#37819;?#28186;涗簡涓绯诲垪?#27537;閭?#28000;跺垪琛?/a>锛屽埄鐩婄浉鍏?#37826;规垨瀵瑰?#33333;?#29111;?#30923;叮?#27537;涓?#27996;?#38318;藉彲浠?#29825;?#38339;?#26473;?#27996;?#38317;?#28000;跺垪琛?#37720;?#26473;?#29723;屽叧娉?#37724;岃璁?#37510;備緥濡?#38171;孞ava鏍稿績API?#27537;閭?#28000;跺垪琛?#37828;痗ore-libs-dev@openjdk.java.net锛屽叧浜巎ava.lang锛宩ava.util绛?#37837;稿績API?#27537;鏂?#37911;?#37804;?#38318;?#28028;?#37734;?#26473;?#38322;岃繘琛岃璁?#38171;屽?#36346;?#29808;?#33333;渶缁堢殑瀹炵幇浠爜杩?#29723;?#28729;?#37836;?#37510;?#28000;讳綍?#30370;瑕?#26473;涘叆OpenJDK鐗?#37832;?#25652;撶殑婧?#28000;爜锛?#37827;?#29825;?#37828;?#27996;?#37725;?#28000;爜(鍗?#38171;岃?#20352;殢JDK?#24386;甯冪殑绋嬪簭)锛岃繕鏄?#23092;?#29831;?#28000;爜锛?#38318;介渶瑕?#37734;?#32448;?#37718;?#28051;?#26473;?#29723;屽叕寮?#27537;浠爜瀹?#37836;?#37510;?br />    閫夋嫨涓涓嚜宸辨?#29111;?#30923;叮?#27537;棰嗗?#29112;垨椤?#37929;?#38171;屽姞鍏?#28729;冪殑閭?#28000;跺垪琛?#38171;岄暱鏈熻?#29115;釜瀹冪殑?#24386;?#30541;锛岀?#23340;潃涓?#28729;?#28000;殑璁?#29825;?#37510;佷簤璁?#37818;?#37716;JDK?#27537;?#32040;杩?#38171;屽涔犲紑?#24386;鑰?#28000;殑API璁?#29825;?#28051;?#28000;爜瀹炵幇锛?..锛岃繖浜涘?#30789;嚜宸辩殑鎴?#38336;?#38318;?#37828;?#37835;?#37832;?#29999;?#37716;殑銆備篃璁?#38171;岃繕鑳?#37930;嬪埌涓浜?#37832;夎叮?#27537;鍏?#37719;?-)

2. 鍑?#28598;?#23480;?#28003;?/span>
鍦?#37713;?#32457;?#28051;?#23092;佹搷浣?#32495;荤粺锛孡inux(濡俇buntu鍜孎edora)锛學indows(7鍜?)锛孧acOS(Lion鍜孧outain Lion)锛?#28051;?#38318;藉彲浠?#37835;勫缓OpenJDK锛屽?#34562;綋?#27537;缁嗚?#20634;彲浠?#37721;傝
瀹?#37826;圭殑涓?#30226;鏂囨銆?br />    鏈?#37826;?#38315;夋嫨?#23047;鐢Ubuntu 14.04銆傚潶鐧?#37734;?#29831;?#38171;?#38315;夋嫨?#23047;鐢Ubuntu锛?#28729;?#37828;?#37733;?#28051;?#37734;Linux鐜?#28583;?#28051;?#37835;勫缓OpenJDK闈?#29999;?#32480;鍗?#37510;傝嫢鍦Windows涓?#26473;?#29723;屾?#21227;缓锛屽垯?#28214;瑕?#28729;夎?#21299;isual Studio C++缂?#29831;?#37731;?#37510;侻acOS锛熷棷锛?#37812;?#23052;?#37832;MBP銆傚彲鑳芥洿澶氫汉骞?#37827;舵槸?#23047;鐢Windows锛屼絾瀹夎涓涓狶inux涔?#28051;嶉?#33636;儲銆傚厛瀹夎?#21613;?#23944;?#22317;殑VM宸?#37711;?a href="https://www.virtualbox.org/">VirtaulBox锛屽啀鍘讳笅杞経buntu?#27537;ISO鏂?#28000;?/a>锛岀劧鍚庝娇鐢VirtualBox鍘诲?#22798;?#21298;buntu銆俈irtualBox绠鍗?#37828;?#37922;?#38171;岀綉涓婄殑?#27977;鍏?#29863;?#37826;?#28052;?#38344;?#29999;哥殑澶?#37510;?br />    OpenJDK?#27537;婧?#37826;?#28000;?#37911;?#37832;?#25652;撳熀浜?a href="https://www.mercurial-scm.org/">Mercurial(hg)锛?#28729;?#37828;?#28051;涓?#28051;Git?#27977;浼肩殑鍒?#29999;?#23534;?#37911;?#37832;?#37818;?#37714;跺伐鍏?#37510;傚湪Ubuntu涓?#28729;夎?#21288;ercurial鍙渶瑕佹墽琛?#37723;?#28000;?br />
$ sudo apt-get install mercurial
    OpenJDK涓?#37722;?#26916;?#37929;殑婧?#28000;?#37934;佺増鏈?#25652;撶殑?#30718;寰勫潎鍦http://hg.openjdk.java.net/?#31651;涓?#38171;?#37734;?#26473;?#38322;屽彲浠?#37813;惧埌6锛?锛?锛?鍜孞igsaw?#27537;婧?#28000;爜銆傚叾涓璊DK 8?#27537;鏈鏂?#23534;?#24386;鐗?#37832;?#25652;撹矾寰?#28051;篽ttp://hg.openjdk.java.net/jdk8u/jdk8u-dev/銆?br />    鍙?#28598;?#38171;屾?#21227;缓OpenJDK鏃堕渶瑕佷竴涓?#37722;?#37716;JDK锛?#37832;?#37826;?#38315;夋嫨鏋勫缓OpenJDK 8锛?#38317;?#28052;?#37722;?#37716;JDK?#27537;鐗?#37832;簲涓?#28003;?#27996;?銆?br />
3. 涓?#26462;芥簮浠爜
鍏嬮殕鐗?#37832;?#25652;?#38171;?br />
$ hg clone http://hg.openjdk.java.net/jdk8u/jdk8u-dev/ jdk8-src
requesting all changes
adding changesets
adding manifests
adding file changes
added 
1570 changesets with 1958 changes to 141 files
updating to branch default
85 files updated, 0 files merged, 0 files removed, 0 files unresolved
杩涘叆婧?#28000;?#37934;佺洰褰?br />
$ cd jdk8-src/
OpenJDK?#27537;婧?#28000;?#37934;佺増鏈?#25652;?#28729;?#38340;?#28051;婂寘鍚?#28598;?#28051;嫭绔嬬殑瀛?#37911;?#37832;?#25652;?#38171;岄渶瑕佹墽琛?#28641;?#28051;嬭剼鏈?#37720;诲?#21975;埆涓?#26462;?#37722;?#28051;?#28699;?#37911;?#37832;?#25652;撶殑婧?#28000;爜锛?br />
$ sh get_source.sh
# Repositories:  corba jaxp jaxws langtools jdk hotspot nashorn
                corba:   hg clone http://hg.openjdk.java.net/jdk8u/jdk8u-dev/corba corba
                 jaxp
:   hg clone http://hg.openjdk.java.net/jdk8u/jdk8u-dev/jaxp jaxp
                corba
:   requesting all changes
                 jaxp
:   requesting all changes

4. 鏋勫缓
璋?#37922;configure杩?#29723;岄?#21228;?#21227;缓锛屽叾涓殑鍙傛暟--with-boot-jdk鐢?#27996;庢寚瀹?#37722;?#37716;JDK?#27537;?#30718;寰?#37510;傚?#20635;灉鍚?#37716;JDK?#27537;java鍛?#28000;?#23480;?#28699;?#37734;?#27996;PATH鐜?#28583;冨彉閲?#28051;?#38171;岃鍙傛暟?#24434;浠?#36423;?#37923;?#37510;?br />
$ sh configure --with-boot-jdk=/path/to/boot/jdk
璇?#26473;囩浼氭娴?#37835;勫缓鐜?#28583;?#37828;?#37722;?#32471;?#37722;?#29781;佹眰锛?#28641;?#37832;?#38338;?#26864;?#38171;?#28729;?#28028;?#32513;?#37713;烘彁绀?#37510;傛瘮濡?#38171;?br />
configure: error: Could not find X11 libraries. You might be able to fix this by running 'sudo apt-get install libX11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev'.
姝?#37827;舵?#35268;嵁鎻?#32448;?#28729;夎鎵?#28214;瑕佺殑搴?#37719;冲彲銆傚?#23945;噸鏂?#37813;?#29723;?#28051;婅堪configure鍛?#28000;?#38171;屽彲鑳?#26473;?#28028;氭彁绀虹己灏?#37711;?#28729;冪殑搴?#38171;?#38317;?#28052;堝?#23942;鏍规嵁鎻?#32448;?#26473;?#29723;?#28729;夎銆傚?#20635;鍙嶅锛屽彧?#22476;棰勬?#21227;缓鎴?#37716;?#28729;?#37812;?#37510;?br />鏈鍚庡氨鏄?#26473;?#29723;屾?#21227;缓锛岀洿鎺?#37813;?#29723;?#28641;?#28051;?#37723;?#28000;?#38171;?br />
$ make all
鍦?#23005;?#26473;囩涓彲浠?#28028;氶?#22247;埌涓浜涜鍛?#38171;?#28051;嶅繀?#24714;浼?#38171;岃?#36423;?#32475;?#23536;?..瀹?#37812;?#37722;?#38171;?#28028;氬?#34425;幇濡?#28051;嬬殑姹?#37804;讳俊?#20229;锛?br />
----- Build times -------
Start 2015-10-30 22:11:10
End   2015-10-30 22:52:54
00:01:01 corba
00:01:08 demos
00:06:49 docs
00:19:37 hotspot
00:01:47 images
00:00:35 jaxp
00:00:49 jaxws
00:08:23 jdk
00:01:09 langtools
00:00:25 nashorn
00:41:44 TOTAL
-------------------------
Finished building OpenJDK for target 'all'
鍦綋鍓嶈矾寰?#28051;?#28028;?#37922;?#37812;?#28051;涓猙uild鐩?#35120;?#38171;屾?#21227;缓濂界殑JDK灏?#37734;?#38317;?#38322;岄潰銆?#37826;JDK?#27537;鍏蜂綋?#30718;寰勭?#35763;技浜build/linux-x86_64-normal-server-release/images/jdk銆傚彲浠?#37813;?#29723;?#28641;?#28051;?#37723;?#28000;?#37720;?#23092;?#29831;?#26473;?#28051;狫DK锛?br />
$ build/linux-x86_64-normal-server-release/images/jdk/bin/java -version
openjdk version "1.8.0-internal"
OpenJDK Runtime Environment (build 1.8.0-internal-ubuntu_2015_10_30_22_07-b00)
OpenJDK 64-Bit Server VM (build 25.66-b00, mixed mode)


John Jiang 2015-10-30 23:17 ?#24386;琛?#29831;?#29825;?/a>
]]>鍒?#37922;Java SE 8娴?#28598;勭?#21976;?#29256;嵁II(璇?http://www.8634070.com/jiangshachina/archive/2014/08/15/417011.htmlJohn JiangJohn JiangFri, 15 Aug 2014 11:57:00 GMThttp://www.8634070.com/jiangshachina/archive/2014/08/15/417011.htmlhttp://www.8634070.com/jiangshachina/comments/417011.htmlhttp://www.8634070.com/jiangshachina/archive/2014/08/15/417011.html#Feedback2http://www.8634070.com/jiangshachina/comments/commentRss/417011.htmlhttp://www.8634070.com/jiangshachina/services/trackbacks/417011.html
鍒?#37922;Java SE 8娴?#28598;勭?#21976;?#29256;嵁
-- 缁?#37722;Stream API?#27537;?#29678;绾?#37823;?#28003;?#37720;?#29723;?#32448;?#28725;屾?#29256;嵁澶勭?#21976;煡璇?/div>
鏈?#37826;?#37828;?/span>
Java Magazine 201405/06鍒?#28051;殑涓?#30226;鏂囩珷锛?#28052;?#37828;?#37826;囩珷绯诲垪"鍒?#37922;Java SE 8娴?#28598;勭?#21976;?#29256;嵁"涓殑绗?#27996;岀瘒锛?#28729;冨熀浜巉latMap()鍜宑ollect()浠?#32513;嶄簡Java娴佺殑?#29678;绾?#37922;?#23049;?2014.08.15鏈鍚庢洿鏂?

鍦?#37832;?#32495;诲垪?#27537;绗?#28051;?#30226;鏂囩珷涓?#38171;屼?#29365;?#23338;埌?#31777;Java娴?#29825;?#28003;?#38003;藉?#29110;娇鐢?#28051;庢?#29256;嵁搴?#37823;?#28003;滅浉浼肩殑鏂规硶鍘诲?#21229;悊闆?#37722;?#37510;備綔涓?#28051;涓?#28598;?#28052;?#38171;屾竻鍗??#27537;渚嬪?#24876;睍绀轰簡濡備綍?#23047;鐢Stream API鍘绘?#20634;緱澶?#27996;?#37828;撶殑閲戦?#28613;箣鍜?#37510;?#37812;?#28000;?#32513;勫缓?#31777;涓涓?#32480;亾锛?#28729;?#37922;?#28051;?#38338;?#37823;?#28003;?filter鍜宮ap)涓?#37832;缁?#37823;?#28003;?reduce)鏋勬垚锛?#37733;?褰?#29854;?#37734;板睍绀?#28729;?#37510;?/span>
?#31483;鍗?
int sumExpensive =
        transactions.stream()
        .filter(t -> t.getValue() > 1000)
        .map(Transaction::getValue)
        .reduce(0, Integer::sum);
鍥?

?#21159;鑰?#37734;?#32495;诲垪?#27537;绗?#28051;?#20788;鍒?#28051;?#38171;屽?#33333;病鏈夌?#26050;杩?#28051;?#28051;?#37826;规硶锛?/span>
flatMap锛?#26473;?#37828;?#28051;涓?#28051;?#38338;?#37823;?#28003;?#38171;?#28729;冨厑璁稿皢涓涓?map"鍜?#28051;涓?flatten"鎿?#28003;?#32513;?#37722;?#37734;?#28051;璧?/span>
collect锛?#26473;?#37828;?#28051;涓?#37832;缁?#37823;?#28003;?#38171;?#28729;?#28186;濇嵁涓?#37722;岀殑鏂瑰紡锛屽皢娴佷腑?#27537;鍏?#32497;?#35120;?#38342;?#28051;?#28051;涓?#32513;撴灉銆?/span>
杩?#28051;?#28051;?#37826;规硶瀵?#27996;庤杈炬洿涓?#28598;?#37833;傜殑鏌?#29831;?#37828;?#37719;佸垎鏈?#37922;殑銆備緥濡?#38171;屼?#29362;彲浠皢flatMap鍜宑ollect缁?#37722;?#29863;锋潵锛?#37922;?#37812;?#28000;?#29723;?#28051;涓?#37826;囧瓧娴佷腑姣?#28051;?#28699;楁?#23941;?#34425;?#25198;殑娆?#37825;扮殑Map瀵?#29854;?#38171;?#28641;傛竻鍗?鎵绀?#37510;傚?#20635;灉绗?#28051;娆?#37930;嬪埌杩?#23048;?#28000;爜瑙?#23536;?#23536;?#37807;婂鏃?#38171;屼絾璇?#28051;?#29781;佹?#21613;績銆?#37832;?#37826;囩殑鐩殑灏?#37828;?#29781;?#29785;?#38322;?#39582;?#37818;?#32460;?#26473;?#28051;?#28051;?#37826;规?#26335;洿澶氱殑缁嗚妭銆?/span>
?#31483;鍗?
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.*;

Stream
<String> words = Stream.of("Java""Magazine""is""the""best");
Map
<String, Long> letterToCount =
        words.map(w 
-> w.split(""))
        .flatMap(Arrays::stream)
        .collect(groupingBy(identity(), counting()));
?#31483;鍗?涓殑浠?#37934;佸皢浼?#37922;?#37812;?#28641;傛竻鍗?绀虹殑缁撴灉銆傛鏋佷簡锛?#28051;?#37828;?#37722;?#38171;?#29825;?#37812;?#28000;?#23534;濮?#37818;?#32460;秄latMap鍜宑ollect鏂规硶鏄?#28641;備綍宸?#28003;滅殑銆?/span>
?#31483;鍗?
[a:4, b:1, e:3, g:1, h:1, i:2, ..]

flatMap鏂规硶
鍋?#29825;?#28003;犳兂鎵?#37713;?#37826;?#28000;?#28051;?#37813;鏈夌嫭涓?#25966;浜岀殑瀛?#37510;備綘浼氭?#28052;堝?#27692;憿锛?/span>
浣犲彲鑳?#29825;?#28051;?#26473;?#23536;堢畝鍗?#38171;?#37812;?#28000;彲浠Files.lines()锛?#37734;?#37715;?#38344;殑鏂囩珷涓?#23480;?#29785;?#26473;囦簡杩?#28051;?#37826;规硶锛?#37733;?#28051;?#28729;?#28028;?#26473;?#37733;?#28051;涓寘鍚?#37826;?#28000;?#28051;?#37813;鏈?#29723;岀殑娴?#37510;傜劧鍚庢垜浠?#28751;卞彲浠娇鐢map鏂规?#26334;皢姣?#28051;琛屾?#21975;垎鎴?#28699;?#38171;?#37832;鍚庡?#23940;娇鐢distinct鏂规硶鍘?#38340;?#38322;嶅?#23943;殑瀛?#37510;傜涓娆?#28751;?#29831;?#23536;楀埌?#27537;浠?#37934;佸彲鑳?#28641;傛竻鍗?鎵绀?#37510;?/span>
?#31483;鍗?
Files.lines(Paths.get("stuff.txt"))
        .map(line 
-> line.split("\\s+")) // Stream<String[]>
        .distinct() // Stream<String[]>
        .forEach(System.out::println);
涓嶅?#21733;殑鏄?#38171;岃繖娈电?#23338;簭骞?#28051;嶅?#20344;?#21976;纭?#37510;傚?#20635;灉杩?#29723;?#28729;?#38171;?#28028;氬?#26944;埌浠?#27996;?#37922;熺?#25120;殑缁撴灉锛?#28051;?#28051;?#38344;殑杈撳嚭鏈変?#28058;?#35763;技锛?/span>
[Ljava.lang.String;@7cca494b
[Ljava.lang.String;@7ba4f24f
...
鎴?#28000;殑绗?#28051;娆?#28751;?#29831;曠瀹?#37813;?#37719;?#37713;轰簡浠?#29723;?#37713;?#28051;?#23092;佸璞殑瀛楃涓?#37510;?#38317;彂鐢熶簡浠涔?#37723;?#38171;?#29831;?#37826;规?#26336;殑闂?#26864;?#37828;?#38171;?#28028;?#32513;檓ap鏂规?#26336;殑Lambda琛?#26440;?#23534;忚繑鍥炵殑鏄?#37826;?#28000;?#28051;?#23011;?#28051;琛岀殑String鏁扮粍(String[])銆傝?#37812;?#28000;?#37930;熸鎯?#29781;佺殑鏄?#28051;涓?#29723;?#32448;?#37826;囧瓧?#27537;娴佺殑Stream<String>瀵?#29854;?#37510;?/span>
骞?#26473;愮殑鏄?#38171;屽浜?#29831;?#38338;?#26864;?#37832;?#28051;涓?#29785;?#37712;?#37826;规锛屽氨鏄娇鐢flatMap鏂规硶銆傝鎴?#28000;?#28051;姝?#28051;姝?#37734;扮?#23340;湅濡備綍寰楀埌姝?#32429;殑瑙?#37712;?#37826;规硶銆?/span>
棣栧厛锛?#37812;?#28000;渶瑕佸瓧?#27537;娴?#38171;岃?#28051;?#37828;?#37825;?#32513;勭殑娴?#37510;?#37832;?#28051;涓?#37722;?#28051;篈rrays.stream()?#27537;鏂规硶锛?#28729;冨皢?#23047;鐢?#28051;涓?#37825;扮粍浣?#28051;?#37721;傛暟锛屽苟鐢?#37812;?#28051;涓?#23092;?#37510;?#29831;风?#23339;竻鍗?涓殑渚嬪瓙銆?/span>
?#31483;鍗?
String[] arrayOfWords = {"Java""Magazine"};
Stream
<String> streamOfwords = Arrays.stream(arrayOfWords);
璁?#37812;?#28000;?#37734;?#37715;?#38344;殑娴佺?#20158;涓娇鐢?#29831;?#37826;规硶锛岀?#23340;湅浼氬彂鐢?#28000;涔?瑙佹竻鍗?)銆傝繖涓?#37826;规渚濈劧琛?#28051;?#38315;?#37510;?#38317;?#37828;?#37733;?#28051;?#37812;?#28000;?#37832;缁?#23536;楀埌?#27537;鏄?#28051;?#31885;娴佺殑娴?鍑嗙鍦?#29831;?#38171;屽氨鏄疭tream<Stream<String>>)銆傜鍒?#37734;?#37828;?#38171;?#37812;?#28000;?#26851;栧?#22557;皢姣?#28051;琛岃浆鎹?#28051;?#28051;涓?#28699;楃殑鏁扮粍锛岀劧鍚庝娇鐢?#37826;规硶Arrays.stream()?#30370;姣?#28051;涓?#37825;扮粍杞?#37817;?#37812;?#28051;涓?#23092;?#37510;?/span>
?#31483;鍗?
Files.lines(Paths.get("stuff.txt"))
       .map(line 
-> line.split("\\s+")) // Stream<String[]>
       .map(Arrays::stream) // Stream<Stream<String>>
       .distinct() // Stream<Stream<String>>
       .forEach(System.out::println);
鎴?#28000;娇鐢flatMap()鏂规硶鍘?#29785;?#37712;?#26473;?#28051;?#38338;?#26864;?#38171;?#28641;傛竻鍗?鎵绀?#37510;備娇鐢flatMap()鏂规硶鑳藉鐢?#23092;佷腑?#27537;鍐?#28729;?#38171;岃?#28051;?#37828;?#23092;?#37720;绘浛鎹?#23011;?#28051;涓?#37922;?#37812;愮殑鏁扮粍銆傛崲瑷?#31651;锛?#38315;?#26473;map(Arrays::stream)鏂规硶鐢?#37812;愮殑鍏儴?#23277;绔嬬殑娴佽鍚堝?#33333;垨"鎵佸?#20914;寲"涓?#28051;涓?#23092;?#37510;?#37733;?褰?#29854;?#37734;板睍绀轰簡?#23047;鐢flatMap()鏂规?#26336;殑鏁堟灉銆?/span>
?#31483;鍗?
Files.lines(Paths.get("stuff.txt"))
       .map(line 
-> line.split("\\s+")) // Stream<String[]>
       .flatMap(Arrays::stream) // Stream<String>
       .distinct() // Stream<String>
       .forEach(System.out::println);
鏈?#29840;?#28051;?#38171;宖latMap璁?#28003;犲彲浠娇鐢?#37711;?#28729;?#23092;?#37720;绘浛鎹?#37721;?#28051;涓?#23092;佷腑?#27537;姣?#28051;?#37711;?#32497;?#38171;岀劧鍚庡?#23941;皢鎵鏈?#37922;?#37812;愮殑娴?#26473;?#37722;堝苟涓?#28051;涓?#23092;?#37510;?/span>
璇锋敞?#21104;锛宖latMap()鏄?#28051;涓?#38315;?#37922;殑妯?#23534;?#38171;?#37734;娇鐢Optaional?#22440;CompletableFuture鏃?#38171;屼綘杩?#28028;氱?#23338;埌瀹?#37510;?/span>

collect鏂规硶
?#24135;鍦?#29825;?#37812;?#28000;?#37930;嬬湅collect鏂规?#26336;殑?#27967;澶?#32513;嗚妭銆傚湪鏈?#32495;诲垪?#27537;绗?#28051;?#30226;鏂囩珷涓?#28003;?#37813;鐪嬪埌?#27537;鏂规硶锛岃?#20343;箞杩?#37733;?#37721;?#28051;涓?#23092;?鍗?#38171;岃繖浜?#37826;规硶鏄?#28051;?#38338;?#37823;?#28003;?锛岃?#20343;箞杩?#37733;?#28051;涓?#37706;?#38171;屼緥濡?#28051;涓猙oolean锛?#28051;涓猧nt锛屾垨涓涓狾ptional瀵?#29854;?鍗?#38171;岃繖浜?#37826;规硶鏄?#37832;缁?#37823;?#28003;?銆?/span>
collect灏?#37828;?#28051;涓?#37832;缁?#37826;规硶锛屼絾瀹?#37832;夌?#29808;効涓?#37722;?#38171;?#37733;?#28051;?#28003;犲彲浠?#37922;?#28729;冨皢涓涓猄tream瀵?#29854;?#26462;?#28051;?#28051;涓狶ist瀵?#29854;?#37510;備緥濡?#38171;?#28051;轰簡寰楀埌涓涓寘鍚?#37832;?#37813;鏈夐珮閲戦浜?#37828;揑D?#27537;?#22442;琛?#38171;屼?#29362;彲浠娇鐢?#37709;忔竻鍗?閭?#37837;风殑浠爜銆?/span>
?#31483;鍗?
import static java.util.stream.Collectors.*;

List
<Integer> expensiveTransactionsIds =
        transactions.stream()
        .filter(t 
-> t.getValue() > 1000)
        .map(Transaction::getId)
        .collect(toList());
浼?#38315;?#32513;collect鏂规?#26336;殑鍙傛暟灏?#37828;?#28051;涓?#32491;诲瀷涓簀ava.util.stream.Collector?#27537;瀵?#29854;?#37510;傝繖涓狢ollector瀵?#29854;?#37828;?#39582;?#28000;涔堢殑锛?#37832;?#29840;?#28051;婄湅锛?#28729;?#37819;忚堪?#31777;濡備綍鎸夌収?#28214;瑕?#37720;绘敹闆?#23092;佷腑?#27537;鍏?#32497;?#38171;屽?#23941;皢瀹?#28000;?#37922;?#37812;?#28051;?#28051;涓?#37832;缁?#32513;撴灉銆備箣鍓?#37922;埌?#27537;宸?#37720;?#37826;规硶Collector.toList()浼?#26473;?#37733;?#28051;涓狢ollector瀵?#29854;?#38171;?#28729;?#37819;忚堪?#31777;濡備綍?#30370;涓涓猄tream瀵?#29854;?#35120;?#38342;?#28051;?#28051;涓狶ist瀵?#29854;?#37510;傝?#28051;?#38171;孋ollctors鍐呭缓鏈?#37832;?#29825;?#28598;氱浉浼肩殑鏂规硶銆備緥濡?#38171;屼娇鐢toSet()鏂规?#26334;彲浠皢涓涓猄tream瀵?#29854;?#26462;寲涓?#28051;涓猄et瀵?#29854;?#38171;?#28729;?#28028;氬垹闄?#37813;鏈?#38322;嶅?#23943;殑鍏?#32497;?#37510;傛竻鍗?涓殑浠?#37934;佸睍绀轰簡濡備綍鐢?#37812;?#28051;涓?#28000;?#28000;呭寘鍚珮閲戦浜?#37828;?#37813;鍦?#37737;?#29999;傜殑Set瀵?#29854;?#37510;?娉剰锛?#37734;?#37722;?#38344;殑渚嬪瓙涓?#38171;?#37812;?#28000;?#37707;?#29825;綜ollectors绫讳腑?#27537;宸?#37720;?#37826;规硶閮藉凡閫?#26473;?#29831;?#37721;import static java.util.stream.Collectors.*琚?#38344;?#37804;佸紩鍏簡)
?#31483;鍗?
Set<String> cities =
        transactions.stream()
        .filter(t 
-> t.getValue() > 1000)
        .map(Transaction::getCity)
        .collect(toSet());
娉剰锛?#37827;犳?#26330;繚璇佷細杩?#37733;?#28003;?#32457;嶇?#35826;?#23340;殑Set瀵?#29854;?#37510;備絾鏄?#38171;?#38315;?#26473;囦娇鐢toCollection()锛屼?#29362;彲浠?#26473;?#29723;屾洿澶氱殑鎺?#37714;?#37510;備緥濡?#38171;岃嫢浣犳兂寰楀埌涓涓狧ashSet锛屽彲浠?#28028;?#28051;涓?#37835;?#38315;?#37731;?#32513;檛oCollection鏂规硶(瑙佹竻鍗?0)銆?/span>
?#31483;鍗?0
Set<String> cities =
        transactions.stream()
        .filter(t 
-> t.getValue() > 1000)
        .map(Transaction::getCity)
        .collect(toCollection(HashSet::
new));
?#21159;鑰?#38171;岃繖骞?#28051;?#37828;?#28003;?#38003;?#37922;collect鍜孋ollector鎵鍋氱殑鍏儴浜嬫儏銆傚疄闄?#28051;?#38171;岃繖鍙?#37828;?#28003;?#38003;?#37922;?#28729;?#28000;?#37813;鍋氱殑浜嬫儏涓殑鏋佸皬?#20788;鍒?#37510;?#28051;?#38344;?#37828;?#28051;浜?#28003;?#37813;鑳借杈剧殑鏌?#29831;殑渚嬪瓙锛?/span>
?#30370;浜?#37828;撴?#22798;揣甯佸?#21977;被锛屽苟璁?#32480;楁瘡绉嶈揣甯佺殑浜?#37828;?#38322;戦?#28613;箣鍜?杩?#37733;?#28051;涓狹ap<Currency, Integer>瀵?#29854;?
?#30370;浜?#37828;撳垝鍒?#37812;?#28051;粍锛氶珮閲戦浜?#37828;?#37724;岄?#28858;珮閲戦浜?#37828;?杩?#37733;?#28051;涓狹ap<Boolean, List<Transaction>>瀵?#29854;?
鍒涘缓澶氬?#20634;?#21977;粍锛屼緥濡傚厛鎸変氦鏄撳彂鐢熺殑鍩?#29999;傚?#21977;粍锛屽啀杩?#28051;姝?#37816;?#28729;?#28000;?#37828;?#37722;?#28051;洪珮閲戦浜?#37828;?#26473;?#29723;屽?#21977;粍(杩?#37733;?#28051;涓狹ap<String, Map<Boolean, List<Transaction>>>)
鍏?#28610;?#37722;?#38171;熷?#22557;銆傝鎴?#28000;?#37930;嬬湅锛屼綘鏄?#28641;備綍?#23047;鐢Stream API鍜孋ollector?#28533;琛?#26440;?#28051;婅堪鏌?#29831;殑銆?#37812;?#28000;?#26851;栧厛浠?#28051;涓?#32480;鍗曠殑渚嬪瓙寮濮?#38171;岃繖涓?#28186;嬪瓙瑕佸杩?#28051;?#23092;?#26473;?#29723;?鎬荤粨"锛?#29825;?#32480;?#28729;冪殑骞?#37735;囧?#38171;?#37832;澶?#37706;?#37724;?#37832;灏?#37706;?#37510;傜劧鍚庢垜浠?#37712;嶇?#23340;湅濡備綍琛?#26440;?#32480;鍗曠殑鍒嗙粍锛?#37832;鍚?#38171;屽?#23943;?#23340;湅濡備綍?#30370;Collector?#31885;鍚?#29863;锋潵鍘诲?#28056;?#28888;洿涓?#23534;?#28598;殑鏌?#29831;?#38171;屼緥濡傚?#27692;?#20634;?#21977;粍銆?/span>
鎬荤粨銆傝鎴?#28000;?#37922;?#28051;浜涚畝鍗曠殑渚嬪?#24877;潵?#20793;?#38889;涓涓?#37510;傚湪?#31651;鍓嶇殑鏂囩珷涓?#38171;屼綘宸?#32513;忕?#23338;埌濡備綍?#23047;鐢reduce鏂规硶鍘?#29825;?#32480;?#23092;佷腑鍏?#32497;犵殑鏁?#38322;?#38171;?#37832;灏?#37706;?#38171;?#37832;澶?#37706;?#37724;屽钩鍧囧?#38171;?#28000;強濡備綍?#23047;鐢熀鏈?#37825;版嵁绫诲?#23338;厓绱犵殑娴?#37510;?#37832;?#28051;浜?#26864;?#28729;氫箟?#27537;Collector绫讳篃鑳?#29825;?#28003;?#28729;?#37812;?#38317;?#27996;涘姛鑳?#37510;備緥濡?#38171;屽彲浠娇鐢counting()鏂规硶鍘?#29825;?#32480;楀厓绱犵殑鏁?#38322;?#38171;?#28641;傛竻鍗?1鎵绀?#37510;?/span>
?#31483;鍗?1
long howManyTransactions = transactions.stream().collect(counting());
浣犲彲浠娇鐢summingDouble()锛宻ummingInt()鍜宻ummingLong()鍒嗗埆瀵?#23092;佷腑鍏?#32497;?#32491;诲瀷涓Double锛孖nt?#22440;Long?#27537;灞?#37804;?#23033;傚拰銆傚湪?#31483;鍗?2涓?#38171;?#37812;?#28000;?#29825;?#32480;楀?#36720;簡鎵鏈変氦鏄撶殑閲戦?#28613;箣鍜?#37510;?/span>
?#31483;鍗?2
int totalValue = transactions.stream().collect(summingInt(Transaction::getValue));
绫讳?#32937;殑锛屼娇鐢averagingDouble()锛宎veragingInt()鍜宎veragingLong()鍘?#29825;?#32480;楀钩鍧囧?#38171;?#28641;傛竻鍗?3鎵绀?#37510;?/span>
?#31483;鍗?3
double average = transactions.stream().collect(averagingInt(Transaction::getValue));
鍙?#28598;?#38171;屼娇鐢maxBy()鍜宮inBy()鏂规硶锛屽彲浠?#29825;?#32480;楀嚭娴佷腑鍊?#37832;澶?#37724;?#37832;灏忕殑鍏?#32497;?#37510;備?#21978;繖閲?#37832;?#28051;涓?#38338;?#26864;?#38171;氫?#29371;渶瑕佷负娴佷腑鍏?#32497;?#28729;氫箟涓涓?#26916;哄簭锛?#28000;?#38003;藉?#29111;瀹?#28000;?#26473;?#29723;屾瘮杈?#37510;傝繖灏?#37828;?#28051;?#28000;涔坢axBy锛?#38171;?#37724;宮inBy()鏂规?#26330;娇鐢娇鐢?#28051;涓狢omparator瀵?#29854;?#28003;?#28051;?#37721;傛暟锛?#37733;?琛?#37828;庝簡杩?#28051;?#20579;銆?/span>
鍥?

鍦竻鍗?4?#27537;渚嬪瓙涓?#38171;?#37812;?#28000;娇鐢簡闈?#37804;?#37826;规硶comparing()锛?#28729;冨皢浼?#37711;殑鍑芥暟浣?#28051;?#37721;傛暟锛?#28000;?#28051;?#37922;?#37812;?#28051;涓狢omparator瀵?#29854;?#37510;?#29831;?#37713;芥暟鐢?#27996;?#28000;?#23092;佺殑鍏?#32497;?#28051;?#29785;?#37835;?#37713;?#37922;?#27996;?#26473;?#29723;屾瘮杈冪殑鍏?#38335;?#28699;?#37510;傚湪杩?#28051;?#28186;嬪瓙涓?#38171;?#38315;?#26473;囦娇鐢?#27996;?#37828;?#38322;戦浣?#28051;烘瘮杈冪殑鍏?#38335;?#28699;?#38171;?#37812;?#28000;?#37813;惧埌?#31777;閭?#32471;?#37832;?#29678;閲戦?#28616;殑浜?#37828;?#37510;?/span>
?#31483;鍗?4
Optional<Transaction> highestTransaction =
        transactions.stream()
        .collect(maxBy(comparing(Transaction::getValue)));
杩?#37832;?#28051;涓猺educing()鏂规硶锛?#37922;?#28729;?#27996;?#37922;熺殑Collector瀵?#29854;?#28028;氭妸娴佷腑?#27537;鎵鏈?#37711;?#32497;?#35120;?#38342;?#37734;?#28051;璧?#38171;屽瀹?#28000;?#38322;嶅?#23943;殑?#31794;鐢?#37722;?#28051;涓?#37823;?#28003;?#38171;岀洿?#22476;浜?#37922;?#32513;撴灉銆?#29831;?#37826;规硶涓庝箣鍓嶇湅杩囩殑reduce()鏂规硶鍦?#37720;熺悊涓?#28051;鏍风殑銆備緥濡?#38171;屾竻鍗?5?#30541;绀轰簡?#23047;鐢簡?#29056;浜巖educing()鏂规?#26336;殑鍙?#28051;绉?#37826;瑰紡鍘?#29825;?#32480;?#37813;鏈変氦鏄撶殑閲戦?#28613;箣鍜?#37510;?/span>
?#31483;鍗?5
int totalValue = transactions.stream().collect(reducing(0, Transaction::getValue, Integer::sum));
reducing()鏂规?#26330;娇鐢?#28051;?#28051;?#37721;傛暟锛?/span>
鍒?#28654;嬪?濡傛灉娴佷负绌?#38171;屽垯杩?#37733;?#28729;?锛?#23005;?#28598;?#38171;岃鍊?#28051;?銆?/span>
?#31794;鐢?#27996;?#23092;佷腑姣?#28051;?#37711;?#32497;犵殑鍑芥暟瀵?#29854;?#38171;?#23005;?#28598;?#38171;岃鍑芥暟浼?#29785;?#37835;?#37713;烘?#24533;瑪浜?#37828;撶殑閲戦銆?/span>
?#30370;涓?#28051;?#37922;?#29785;?#37835;?#37713;芥暟鐢?#37812;愮殑閲戦鍚堝苟鍦?#28051;璧风殑鏂规硶锛?#23005;?#28598;?#38171;?#37812;?#28000;?#37721;?#37828;?#37814;?#38322;戦鍔?#29863;锋潵銆?/span>
浣犲彲鑳?#28028;?#29831;?#38171;?绛?#32475;?#38171;屼娇鐢?#37711;?#28729;冪殑娴?#37826;规硶锛?#28641;俽educe()锛宮ax()鍜宮in()锛?#37812;?#23480;?#32513;忓彲浠?#37707;氬埌杩?#27996;涗簡銆?#38317;?#28052;?#38171;屼綘涓?#28000;涔?#26473;?#29781;佺粰鎴戠湅杩?#27996;?#37826;规硶鍛?#38171;?鍚?#38344;?#38171;屼?#29362;皢浼氱?#23338;埌鎴?#28000;皢Collector缁?#37722;?#29863;锋潵鍘?#37835;勫?#28888;洿涓?#28598;?#37833;傜殑鏌?#29831;?渚?#28641;?#38171;屽?#29808;?#29363;硶骞?#37735;囨暟杩?#29723;屽?#21977;粍)锛?#37813;浠?#38171;岃繖涔?#38003;芥洿鏄?#27996;庣悊瑙?#26473;?#27996;涘?#21613;缓?#27537;Collector銆?/span>
鍒嗙粍銆傝繖鏄?#28051;涓櫘閫氱殑鏁版嵁搴?#37836;?#29831;?#37823;?#28003;?#38171;?#28729;冧娇鐢?#28766;?#37804;?#37720;?#37825;版嵁杩?#29723;屽?#21977;粍銆備緥濡?#38171;屼綘涔?#29825;告?#34411;寜甯佺?#23941;涓?#31885;浜?#37828;?#26473;?#29723;屽?#21977;粍銆傝嫢浣犱娇鐢?#28641;傛竻鍗?6鎵绀虹殑浠爜锛?#38315;?#26473;?#37828;?#23534;忕殑閬?#37720;?#37720;?#29723;?#26440;?#26473;?#28051;?#37836;?#29831;?#38171;?#38317;?#28028;?#37828;?#23536;堢?#28060;?#27537;銆?/span>
?#31483;鍗?6
Map<Currency, List<Transaction>> transactionsByCurrencies = new HashMap< >();
for(Transaction transaction : transactions) {
    Currency currency 
= transaction.getCurrency();
    List
<Transaction> transactionsForCurrency =
    transactionsByCurrencies.get(currency);

    
if (transactionsForCurrency == null) {
        transactionsForCurrency 
= new ArrayList<>();
        transactionsByCurrencies.put(currency, transactionsForCurrency);
    }
    transactionsForCurrency.add(transaction);
}
浣犻?#26663;?#22565;渶瑕佸?#28056;缓涓涓狹ap瀵?#29854;?#38171;?#28729;冨皢?#25977;闆?#37813;鏈夌殑浜?#37828;?#29825;?#35120;?#37510;傜劧鍚?#38171;屼?#29371;渶瑕侀亶鍘?#37813;鏈夌殑浜?#37828;?#29825;?#35120;?#38171;屽苟瑙?#37835;?#37713;烘?#24533;瑪浜?#37828;撶殑甯佺銆傚湪?#30370;浜?#37828;?#29825;?#35120;曚娇鐢?#28051;涓?#37706;兼彃鍏Map涓箣鍓?#38171;岄渶瑕佸?#22559;鏌?#28051;涓?#38171;岃繖涓狶ist鏄?#37722;?#23480;?#32513;?#37714;涘缓杩囦簡锛?#32475;?#32475;?#37510;?/span>
鐪?#37828;?#28000;?#27996;烘棰滃锛?#37733;?#28051;?#37812;?#28000;?#37807;?#29781;?#37828;?鎸?#29999;佺?#23941;浜?#37828;?#26473;?#29723;屽?#21977;粍"銆?#28051;?#28000;涔?#28051;嶅緱涓嶆?#22794;強杩?#28052;?#28598;?#28000;爜鍛?#38171;?#37832;?#28610;芥?#22559;伅锛?#37832;?#28051;涓?#32457;?#28051;篻roupingBy()?#27537;Collector锛?#28729;冨厑璁?#37812;?#28000;?#28000;?#32480;娲佺殑鏂瑰紡?#28533;琛?#26440;?#26473;?#28051;?#28186;嬪瓙銆?#37812;?#28000;彲浠娇鐢竻鍗?7涓殑渚嬪?#24877;潵琛?#26440;?#26473;?#28051;浉鍚岀殑鏌?#29831;?#38171;岀幇鍦?#28000;?#37934;佺殑闃?#29831;绘洿鎺?#26473;?#38338;?#26864;?#29831;?#37721;簡銆?/span>
?#31483;鍗?7
Map<Currency, List<Transaction>> transactionsByCurrencies =
        transactions.stream().collect(groupingBy(Transaction::getCurrency));
宸?#37720;?#37826;规硶groupingBy()?#23047;鐢?#28051;涓?#37713;芥暟瀵?#29854;?#28003;?#28051;?#37721;傛暟锛岃鍑芥暟浼?#29785;?#37835;?#37713;?#37922;?#27996;庡垎绫讳氦鏄?#29825;?#35120;曠殑鍏?#38335;?#28699;?#37510;?#37812;?#28000;?#32457;?#28051;?#26473;?#28051;?#37713;芥暟涓?#37714;?#32491;诲?#33445;暟銆傚湪姝?#28598;?#38171;?#28051;轰簡鎸?#29999;佺?#23941;浜?#37828;?#26473;?#29723;屽?#21977;粍锛?#37812;?#28000;?#28028;?#37711;?#28051;涓?#37826;规?#26334;紩鐢?#38171;孴ransaction::getCurrency銆?#37733;??#32040;绀轰簡杩?#28051;?#37714;?#32513;勬搷浣?#37510;?/span>
鍥?

鍒嗗壊銆?#37832;?#28051;涓?#32457;?#28051;簆artitioningBy()?#27537;宸?#37720;?#37826;规硶锛?#28729;冨彲琚?#29785;?#28051;?#28051;绉?#37911;?#23048;婄殑groupingBy()鏂规硶銆傚畠?#23047;鐢?#28051;涓?#29835;?#29831;?#28003;?#28051;?#37721;傛暟(璇?#37721;傛暟杩?#37733;?#28051;涓猙oolean鍊?锛岀劧鍚庢?#35268;嵁鍏?#32497;?#37828;?#37722;弧瓒?#26473;?#28051;?#29835;?#29831;?#28725;?#28729;?#28000;?#26473;?#29723;屽?#21977;粍銆傛崲瑷?#31651;锛?#28729;冨皢缁勬垚娴佺殑浜?#37828;撳?#21975;壊鎴愪簡缁?#37835;凪ap<Boolean, List<Transaction>>銆備緥濡?#38171;?#28641;傝嫢浣犳兂?#30370;浜?#37828;撳?#21975;壊鎴?#28051;粍--浣庡?#22796;殑鍜?#37828;傝?#30005;殑--浣?#28751;卞彲浠?#37709;忔竻鍗?8閭?#37837;?#37720;讳娇鐢partitioningBy()浜?#37922;熺殑Collector銆傛渚?#28051;殑Lambda琛?#26440;?#23534;?#38171;宼->t.getValue() > 1000锛屽氨鏄?#28051;涓皢浜?#37828;撳垎鎴?#28003;庡粔鍜?#37828;傝?#30005;殑璋?#29831;?#37510;?/span>
?#31483;鍗?8
Map<Boolean, List<Transaction>> partitionedTransactions =
        transactions.stream().collect(partitioningBy(t 
-> t.getValue() > 1000));
?#31885;鍚Collector銆傚?#20635;灉浣犵?#29112;倝SQL锛屼?#29362;簲璇煡閬撳彲浠皢GROUP BY涓庡?#33445;暟COUNT()鍜孲UM()涓鍧楀?#22840;娇鐢?#38171;?#28000;?#37816;?#29999;佺?#23941;拰浜?#37828;?#38322;戦?#28613;箣鍜岃繘琛屽?#21977;粍銆?#38317;?#28052;?#38171;屼娇鐢Stream API鏄?#37722;?#28052;熷彲浠?#28729;炵?#25198;浉浼肩殑鍔?#38003;?#37723;?#38171;熷綋鐒跺彲浠?#37510;傜鍒?#37734;?#29831;?#38171;?#37832;?#28051;涓噸杞界殑groupingBy()鏂规硶锛?#28729;冧娇鐢?#37721;?#28051;涓狢ollector浣?#28051;虹浜?#28051;?#37721;傛暟銆傝繖涓?#26864;?#28598;栫殑Collector瀵?#29854;?#37922;?#27996;?#28729;氫箟鍦娇鐢?#37922;groupingBy()浜?#37922;熺殑Collector鏃?#28641;備綍姹?#38342;?#37813;鏈?#28051;庡叧閿?#28699;楃浉鍏崇殑鍏?#32497;?#37510;?/span>
濂?#37722;?#38171;岃繖鍚?#29863;锋潵鏈変簺鎶借薄锛?#38317;?#28052;?#29825;?#37812;?#28000;?#37930;?#28051;涓?#32480;鍗曠殑渚嬪瓙銆?#37812;?#28000;?#37807;冲熀浜庢瘡涓?#37737;?#29999;傜殑浜?#37828;?#38322;戦?#28613;箣鍜?#37922;?#37812;?#28051;涓?#37737;?#29999;傜殑Map瀵?#29854;?瑙佹竻鍗?9)銆傚湪姝?#28598;?#38171;?#37812;?#28000;?#37723;?#29831;塯roupingBy()鏂规?#26330;娇鐢getCity()鏂规硶浣?#28051;?#37714;嗙被鏂规硶銆?#38317;?#28052;?#38171;屽?#26944;埌?#27537;Map缁撴灉?#27537;Key灏?#28051;?#37737;?#29999;?#37510;傛甯?#37734;?#38171;?#37812;?#28000;?#37832;?#37832;涘Map涓?#23011;?#28051;?#38335;?#37813;瀵瑰簲?#27537;鍊?#38171;?#37719;矻ist<Transaction>瀵?#29854;?#38171;屼娇鐢groupingBy()鏂规硶銆?/span>
?#31483;鍗?9
Map<String, Integer> cityToSum =
        transactions.stream().collect(groupingBy(Transaction::getCity,
        summingInt(Transaction::getValue)));
?#21159;鍚?#38171;?#37812;?#28000;?#37719;?#37828;?#28028;?#37711;簡鍙?#28051;涓狢ollector瀵?#29854;?#38171;?#28729;?#37922;眘ummingInt()鏂规硶浜?#37922;?#38171;岃鏂规硶浼氬皢鎵鏈?#28051;?#37911;?#28729;氬煄甯傜浉鍏崇殑浜?#37828;?#29825;?#35120;曠殑閲戦鍔?#29863;锋潵銆傜粨?#28745;锛?#37812;?#28000;?#23536;楀埌?#31777;涓涓狹ap<String, Integer>瀵?#29854;?#38171;?#28729;冨皢姣?#28051;?#37737;?#29999;?#28051;?#28729;?#28000;?#28725;瑰簲?#27537;鎵鏈変氦鏄撶殑閲戦?#28613;箣鍜岃繘琛屼簡鏄?#28751;?#37510;傞叿锛?#28051;?#37828;?#37722;?#38171;熸?#34411;兂杩?#28051;?#38171;氬熀鏈殑groupingBy(Transaction:getCity)鏂规硶鍏?#28729;?#28751;?#37721;?#37828;痝roupingBy(Transaction:getCity, toList())?#27537;绠鍐?#37510;?/span>
璁?#37812;?#28000;?#37930;嬬?#23338;彟涓涓?#28186;嬪瓙銆傚?#20635;灉浣犳兂鐢?#37812;?#26473;?#37837;?#28051;涓狹ap锛?#28729;?#28725;?#23011;?#28051;?#37737;?#29999;?#28051;?#28729;冪殑鏈澶?#38322;戦?#28616;殑浜?#37828;?#29825;?#35120;?#26473;?#29723;?#37828;?#28751;?#38171;?#38317;?#29781;佹?#28052;堝?#27692;憿锛?#28003;犲彲鑳藉凡缁忕?#28355;埌?#31777;锛?#37812;?#28000;彲浠噸鐢?#37715;?#38344;?#26473;囩殑鐢眒axBy()鏂规硶浜?#37922;熺殑Collector锛?#28641;傛竻鍗?0鎵绀?#37510;?/span>
?#31483;鍗?0
Map<String, Optional<Transaction>> cityToHighestTransaction =
        transactions.stream().collect(groupingBy(Transaction::getCity,
        maxBy(comparing(Transaction::getValue))));
浣?#23480;?#32513;忕?#23338;埌Stream API寰堝杽浜庤杈?#38171;?#37812;?#28000;?#23005;?#37734;?#37835;勫缓?#27537;涓浜?#37719;佸垎鏈夎叮?#27537;鏌?#29831;?#38318;藉彲浠?#37712;欑殑绠娲佷簺銆備綘杩?#38003;芥兂璞?#37713;?#37733;炲埌浠庡墠鍘婚亶鍘?#37734;?#28598;勭悊涓涓?#38342;?#37722;?#37722;?#38171;?#29825;?#37812;?#28000;?#37930;?#28051;涓洿涓?#28598;?#37833;傜殑渚嬪瓙锛?#28000;?#32513;?#37833;?#26473;欑瘒鏂囩珷銆備綘宸茬?#23338;埌groupingBy()鏂规?#26334;彲浠皢涓涓狢ollector瀵?#29854;?#28003;?#28051;?#37721;傛暟锛屽啀鏍规嵁杩?#28051;姝殑鍒嗙被瑙勫垯鍘绘敹闆?#23092;佷腑?#27537;鍏?#32497;?#37510;?#37733;?#28051;篻roupingBy()鏂规硶鏈韩寰楀埌?#27537;涔?#37828;?#28051;涓狢ollector瀵?#29854;?#38171;?#38317;?#28052;?#38315;?#26473;?#28028;?#37711;?#37721;?#28051;涓?#37922;groupingBy()鏂规硶寰楀埌?#27537;Collector瀵?#29854;?#38171;岃Collector瀹氫箟?#31777;绗?#27996;岀骇?#27537;鍒嗙被瑙勮寖锛?#37812;?#28000;?#28751;?#38003;藉?#29111;?#28056;缓澶氬?#20635;鍒嗙粍銆?/span>
鍦竻鍗?1?#27537;浠?#37934;佷腑锛屽厛鎸?#37737;?#29999;傚浜?#37828;?#29825;?#35120;?#26473;?#29723;屽?#21977;粍锛屽啀杩?#28051;姝?#28725;?#23011;?#28051;?#37737;?#29999;?#28051;殑浜?#37828;?#29825;?#35120;曟寜甯佺杩?#29723;屽?#21977;粍锛?#28000;?#23536;楀埌姣?#28051;?#37737;?#29999;?#28051;?#23011;?#28051;?#29999;佺?#23943;殑鎵鏈変氦鏄?#29825;?#35120;曠殑骞?#37735;?#38322;戦銆?#37733;?灏?#35120;?#29854;?#37734;板睍绀轰簡杩?#32457;?#37832;?#37714;?#37510;?/span>
?#31483;鍗?1
Map<String, Map<Currency, Double>> cityByCurrencyToAverage =
        transactions.stream().collect(groupingBy(Transaction::getCity,
        groupingBy(Transaction::getCurrency,  
        averagingInt(Transaction::getValue))));
鍥?

鍒涘缓浣犺嚜宸辩殑Collector銆傚埌鐩?#37715;?#28051;烘锛?#37812;?#28000;睍绀虹殑鍏儴Collector閮?#28729;炵幇?#31777;鎺?#37721;java.util.stream.Collector銆傝繖灏辨剰鍛崇潃锛屼?#29362;彲浠?#28729;炵幇?#22172;宸辩殑Collector锛?#28000;?瀹氬埗"褰?#28051;鎿?#28003;?#37510;備絾鏄?#28725;?#27996;?#26473;?#28051;?#28051;?#26864;?#38171;屽?#23941;啓涓?#30226;鏂囩珷?#24434;鑳芥洿鍚?#38315;?#28051;浜?#38171;?#37813;浠?#37812;?#28000;?#28051;?#28028;?#37734;?#37832;?#37826;?#28051;?#29825;?#29825;?#26473;?#28051;?#38338;?#26864;?#37510;?/span>

缁?#29825;?/span>
鍦?#37832;?#37826;?#28051;?#38171;?#37812;?#28000;?#37818;?#29825;簡Stream API涓殑涓?#28051;珮绾?#38171;歠latMap鍜宑ollect銆傚畠浠?#37828;彲浠?#37716;犲埌浣犵殑鍏?#37731;?#25652;?#28051;殑宸?#37711;?#38171;屽彲浠?#37922;潵琛堪涓?#28725;岀殑鏁版嵁澶勭?#21976;煡璇?#37510;?/span>
鐗瑰埆鍦?#38171;屼綘涔熷凡缁忕?#23338;埌?#31777;锛宑ollect()鏂规?#26334;彲琚?#37922;?#27996;庡?#25492;撼锛屽垎缁勫拰鍒嗗壊鎿?#28003;?#37510;傚彟澶?#38171;岃繖浜?#37823;?#28003;?#26473;樺彲鑳?#29722;?#32513;?#37722;?#37734;?#28051;璧?#38171;?#37720;?#37835;勫?#28888;洿涓?#28051;?#28725;岀殑鏌?#29831;?#38171;屼緥濡?鐢?#27996;?#28051;涓?#28051;眰Map瀵?#29854;?#38171;?#28729;?#28000;?#29723;?#23011;?#28051;?#37737;?#29999;?#28051;?#23011;?#28051;?#29999;佺?#23943;殑骞?#37735;?#27996;?#37828;?#38322;戦"銆?/span>
?#21159;鑰?#38171;?#37832;?#37826;?#28052;熸病鏈?#37836;?#32460;跺埌鎵鏈夌殑鍐呭缓Collector瀹炵幇銆?#29831;?#28003;?#37720;荤?#23340;湅Collectors?#34987;锛屽苟璇?#29831;?#37711;?#28729;冪殑Collector瀹炵幇锛屼緥濡?#37922;眒apping()锛宩oining()鍜宑ollectingAndThen()锛?#28052;?#29825;?#28003;?#28028;氬彂?#24135;瀹?#28000;?#28052;熷緢鏈?#37922;?#37510;?/span>


John Jiang 2014-08-15 19:57 ?#24386;琛?#29831;?#29825;?/a>
]]>鍒?#37922;Java SE 8娴?#28598;勭?#21976;?#29256;嵁(I)(璇?http://www.8634070.com/jiangshachina/archive/2014/07/27/416235.htmlJohn JiangJohn JiangSun, 27 Jul 2014 12:54:00 GMThttp://www.8634070.com/jiangshachina/archive/2014/07/27/416235.htmlhttp://www.8634070.com/jiangshachina/comments/416235.htmlhttp://www.8634070.com/jiangshachina/archive/2014/07/27/416235.html#Feedback6http://www.8634070.com/jiangshachina/comments/commentRss/416235.htmlhttp://www.8634070.com/jiangshachina/services/trackbacks/416235.html
鍒?#37922;Java SE 8娴?#28598;勭?#21976;?#29256;嵁
-- ?#23047;鐢Java娴佹搷浣?#37720;?#29723;?#26440;?#28598;?#37833;傜殑鏁版嵁鏌?#29831;?/span>

鏈?#37826;?#37828;?a href="http://www.oracle.com/technetwork/java/javamagazine/index.html">Java Magazine
201403/04鍒?#28051;殑涓?#30226;鏂囩珷锛?#28052;?#37828;?#37826;囩珷绯诲垪"鍒?#37922;Java SE 8娴?#28598;勭?#21976;?#29256;嵁"涓殑绗?#28051;?#30226;锛?#28729;?#22994;傝堪?#31777;Java娴佺殑?#29056;鏈?#37720;熺悊涓庡熀鏈簲鐢?#38171;?#37828;?#28051;?#30226;寰堝?#30028;殑Java Streams API?#27537;鍏?#38338;?#37826;囩珷銆?2014.07.27鏈鍚庢洿鏂?

    娌?#37832;?#38342;?#37722;堝璞?#38171;屼綘浼氭?#28052;?#37837;?#38171;熷嚑涔庢瘡涓狫ava?#31794;鐢?#38318;?#28028;氬?#28056;缓骞?#28598;勭悊闆?#37722;?#37510;傚畠浠?#37828;?#29825;?#28598;?#32514;栫浠诲姟?#27537;?#29056;纭锛?#38342;?#37722;堜娇浣?#38003;藉?#29111;鏁版嵁杩?#29723;屽垎缁勫拰澶勭悊銆備緥濡?#38171;屼綘涔?#29825;?#28028;氬?#28056;缓涓涓?#37711;?#27996;庨?#24816;浜?#37828;撶殑闆?#37722;?#38171;岃闆?#37722;?#28000;?#29723;簡?#28999;涓?#37922;埛?#27537;閾惰娴佹?#23384;竻鍗?#37510;傜劧鍚?#38171;屼?#29362;彲鑳芥兂瑕?#28598;勭?#21976;暣涓?#38342;?#37722;?#37720;荤?#26944;嚭璇?#37922;埛鑺变簡澶氬?#25126;挶銆傚敖绠?#37825;版嵁澶勭悊鍗佸垎?#22136;瑕?#38171;屼絾Java鍦?#23005;?#37826;?#38344;?#29723;?#37916;扮殑杩?#28051;?#28729;岀編銆?/span>
    棣栧厛锛屽吀鍨嬬殑闆?#37722;?#28598;勭?#21976;寮?#28051;庣被SQL鎿?#28003;滅浉浼?#38171;岃濡?鏌?#37813;?(濡?#37813;?#37713;?#37832;澶?#38322;戦?#28616;殑閭?#32471;?#27996;?#37828;??#22440;鑰?鍒嗙粍"(濡?#38171;屽皢涓庤涔?#37833;傝揣?#27977;鍏崇殑浜?#37828;?#26473;?#29723;屽?#21977;粍)銆傚?#20788;鍒嗘?#29256;嵁搴撳厑璁?#28003;?#28000;?#28601;?#37828;庣殑褰?#23534;?#37720;绘寚瀹?#26473;?#27996;?#37823;?#28003;?#37510;備緥濡?#38171;?#37722;?#38344;殑SQL鏌?#29831;?#28028;?#29825;?#28003;?#37813;惧埌閭?#32471;?#37832;澶?#38322;戦浜?#37828;撶殑ID锛?SELECT id, MAX(value) from transctions"銆?/span>
    濡備綘鎵瑙?#38171;?#37812;?#28000;?#39582;?#28051;嶉渶瑕?#37720;诲?#28853;幇濡備綍璁?#32480;?#37832;澶?#37706;?渚?#28641;?#38171;屼娇鐢?#23536;?#37916;?#37724;?#28051;涓彉閲?#37720;?#26473;借釜杩?#28051;?#37832;澶?#37706;?銆?#37812;?#28000;?#28000;呴渶瑕佽杈?#28000;涔?#37828;?#37812;?#28000;?#37807;?#29781;佺殑銆傝繖绉嶅熀鏈?#37804;?#37807;?#28751;辨剰鍛崇潃锛屼綘涓嶅?#28214;瑕佹?#21613;績鍘?#37828;?#23534;?#37734;?#28729;炵幇杩?#27996;?#37836;?#29831;?-瀹?#28000;?#23480;?#32513;?#28051;?#28003;?#28598;勭?#21975;?#25106;簡銆?#28051;?#28000;涔?#37812;?#28000;?#28051;?#38003;?#37734;?#28598;勭悊闆?#37722;?#37827;?#28052;?#26473;?#37837;?#37707;氬憿锛?#28003;犲彂?#24135;?#22172;宸?#37832;?#28598;氬皯娆?#38318;?#37828;?#37734;?#28051;閬嶅張涓閬嶅湴?#23047;鐢?#23536;?#37916;?#37720;?#38322;嶅瀹炵幇杩?#27996;?#37823;?#28003;?#37723;?#38171;?/span>
    鍏舵锛?#37812;?#28000;?#28641;備綍鎵?#38003;芥洿楂樻晥鍦?#37720;诲?#21229;悊澶?#37736;?#38342;?#37722;?#38171;熺?#21976;?#34411;?#21613;喌涓?#38171;?#37734;?#37716;?#38315;?#26473;?#29723;?#28598;勭悊鏃?#38171;屼綘浼氭兂?#22476;鍒?#37922;?#28598;氭牳鏋舵瀯銆傜劧鑰?#38171;岀?#26663;啓骞惰?#23680;?#23338;簭鏃?#37733;伴毦鍙?#28729;?#37828;撳嚭閿?#37510;?/span>
    Java SE 8璧舵潵甯?#36423;欎簡锛Java API?#27537;璁?#29825;?#38000;?#28000;?#37734;?#37719;囩骇API鏃跺紩鍏簡涓绉?#37826;扮殑绉颁箣涓篔ava娴?娴??#27537;鎶借薄锛?#28729;冨厑璁?#28003;?#28000;?#28601;?#37828;庡舰寮?#37720;诲?#21229;?#21976;?#29256;嵁銆傚彟澶?#38171;孞ava娴佸彲浠?#37714;?#37922;埌澶氭牳鏋舵?#21230;?#28051;嶅繀缂栧啓涓琛?#28598;氱?#36328;浠爜銆?#37722;?#29863;锋潵涓嶉敊锛?#28051;?#37828;?#37722;?#38171;?#26473;?#28751;?#37828;?#37832;?#37826;囩珷绯诲垪鎵瑕?#37818;?#32460;剁殑涓?#26864;?#37510;?/span>
    Java娴?#38003;?#28051;?#37812;?#28000;?#37707;氫簺浠涔?#37723;?#38171;?#37734;?#37818;?#32460;?#26473;?#27996;涚?#21978;?#20633;箣鍓?#38171;岃鎴?#28000;?#37711;堢湅涓涓?#28186;嬪瓙锛岃繖鏍?#28003;?#37813;?#38003;藉杩?#32457;?#37826;扮殑?#23047;鐢Java SE 8 Java娴佺殑缂栫?#23342;?#24226;牸鏈夋劅瑙?#37510;傚亣璁?#37812;?#28000;?#29781;佹壘?#22476;鎵鏈夌?#35826;瀷涓篻rocery?#27537;浜?#37828;撳苟杩?#37733;?#28729;?#28000;殑ID?#22442;琛?#38171;屽?#33333;?#22793;氦鏄?#38322;戦?#28616;殑閫?#37713;?#26916;哄簭瀵?#29831;垪琛?#26473;?#29723;?#37818;?#25652;?#37510;傚湪Java SE 7涓?#38171;?#37812;?#28000;簲璇?#28028;氭妸?#31483;鍗?鎵绀虹殑绋嬪簭閭?#37837;?#37720;诲仛銆傝?#37734;Java SE 8涓?#38171;?#37812;?#28000;?#37714;?#28028;氬?#24532;竻鍗?鎵绀虹殑閭?#37837;?#37720;诲?#28853;幇銆?/span>
?#31483;鍗?
List<Transaction> groceryTransactions = new Arraylist<>();
for(Transaction t: transactions){
  
if(t.getType() == Transaction.GROCERY){
    groceryTransactions.add(t);
  }
}
Collections.sort(groceryTransactions, 
new Comparator(){
  
public int compare(Transaction t1, Transaction t2){
    
return t2.getValue().compareTo(t1.getValue());
  }
});
List
<Integer> transactionIds = new ArrayList<>();
for(Transaction t: groceryTransactions){
  transactionsIds.add(t.getId());
}

?#31483;鍗?
List<Integer> transactionsIds =
    transactions.stream()
                .filter(t 
-> t.getType() == Transaction.GROCERY)
                .sorted(comparing(Transaction::getValue).reversed())
                .map(Transaction::getId)
                .collect(toList());

    鍥?褰?#29854;?#37734;?#29785;?#38322;婁簡閭?#23048;Java SE 8绋嬪簭銆傞?#26663;厛锛?#37812;?#28000;?#29835;?#37922;List瀵?#29854;?#28051;殑Java娴?)鏂规硶浠?#27996;?#37828;撳垪琛?鏁版嵁)涓?#38014;?#37721;?#28051;涓狫ava娴佸璞?#37510;傜劧鍚?#38171;?#28598;?#28051;?#37823;?#28003;?杩囨护锛?#37818;?#25652;?#38171;?#37828;?#28751;?#38171;屽綊闆??#25724;鎺?#37734;?#28051;璧?#35120;?#37812;愪簡涓鏉?#32510;?#38171;岃繖鏉?#32510;垮彲浠?#29722;?#37930;?#28003;?#37835;勬垚?#31777;涓鏉?#37825;版嵁鏌?#29831;?#37510;?/span>


    閭?#28052;?#28641;備綍骞惰鍦?#37813;?#29723;岃绋嬪簭鍛?#38171;?#37734;Java SE 8涓?#26473;?#23536;堢畝鍗?#38171;氬彧?#28214;瑕佷娇鐢parallelJava娴?)鏂规硶鍘绘浛鎹Java娴?)鏂规硶锛?#28641;傛竻鍗?鎵绀?#37510;侸ava娴?API浼?#37734;?#37712;呴儴瀵?#28003;犵殑鏌?#29831;?#26473;?#29723;岃鏋?#38171;屽?#36346;埄鐢?#28051;?#28003;?#37832;?#37731;?#28051;殑澶氭牳澶勭悊鍣?#37510;?/span>
?#31483;鍗?
List<Integer> transactionsIds =
    transactions.parallelStream()
                .filter(t 
-> t.getType() == Transaction.GROCERY)
                .sorted(comparing(Transaction::getValue).reversed())
                .map(Transaction::getId)
                .collect(toList());

    鍦?#29831;?#37711;?#27996;嶫ava SE 8 Java娴佺殑鏂囩珷绯诲垪缁?#37833;?#37827;?#38171;屼?#29362;皢鑳藉?#29110;娇鐢Java娴?API缂栧啓鍑哄?#24532;竻鍗?閭?#37837;风殑鍔?#38003;藉己澶殑鏌?#29831;?#32459;嬪簭銆?/span>

Java娴佸叆闂?/span>
    璁?#37812;?#28000;?#37711;?#28000;?#28051;?#20579;鐞嗚寮濮?#37510;侸ava娴佺殑瀹氫箟鏄?#28000;涔?#38171;?#28051;涓?#32480;?#29037;?#27537;瀹氫箟灏?#37828;??#28533;?#22172;浜?#28051;涓?#37825;版嵁婧愮殑鑳藉鏀?#37816;佽仛鍚?#37823;?#28003;滅殑涓涓?#37711;?#32497;?銆傝鎴?#28000;?#37814;?#28729;冩?#21975;紑?#28533;璇?#38171;?/span>
    涓涓?#37711;?#32497;?/strong>锛欽ava娴佷负涓涓?#37911;?#28729;?#32491;诲?#23338;肩殑闆?#37722;?#37819;?#28186;涗簡涓涓?#37818;?#37721;?#37510;傜劧鍚?#38171;孞ava娴?#28729;?#38340;?#28051;?#39582;?#28051;嶅?#27194;偍鍏?#32497;?#38171;?#28729;?#28000;?#28028;?#37734;渶瑕佹椂琚?#37922;?#28051;?#37510;?/span>
    鏁版嵁婧?/strong>锛欽ava娴佽?#20343;娇鐢?#28051;涓?#37819;?#28186;?#37825;版嵁?#27537;婧?#38171;岃濡?#38342;?#37722;堝璞?#38171;屾暟缁勬垨I/O璧勬簮銆?/span>
    ?#20187;鍚?#37823;?#28003;?/strong>锛欽ava娴佹敮鎸佺被SQL?#27537;鎿?#28003;?#38171;?#28000;強?#28533;?#22172;浜庡?#33445;?#25198;?#26667;璇?#29815;?#27537;閫?#37922;?#37823;?#28003;?#38171;岃濡傝繃?#25252;锛?#37828;?#28751;?#38171;屽綊涓锛屾煡鎵?#38171;屽?#24402;厤锛?#37818;?#25652;?#38171;?#32475;?#32475;?#37510;?/span>
    鍙?#28598;?#38171;?#28051;?#38342;?#37722;?#37823;?#28003;滈潪甯?#28051;?#37722;岀殑鏄?#38171;孞ava娴佹搷浣滄嫢鏈?#28051;?#26916;瑰熀鏈?#37911;硅川锛?/span>
    绠亾锛?#29825;?#28598;欽ava娴佹搷浣?#28028;?#26473;?#37733;?#28729;?#28000;嚜宸?#38171;岃繖灏变娇寰?#26473;?#27996;?#37823;?#28003;?#38003;藉?#29116;摼鎺?#37734;?#28051;璧?#28000;?#32513;勬垚涓涓?#28598;?#37736;嬬?#20158;銆傝繖鏍?#28751;卞彲浠?#26473;?#29723;?#28051;浜?#29831;?#28641;傛儼鎬?#37724;岀煭?#30718;?#31651;绫荤殑浼樺寲锛?#37722;?#38344;?#37812;?#28000;?#28028;?#26473;?#29723;?#37818;?#32460;?#37510;?/span>
    鍐呴儴閬?#37720;?#38171;?#38342;?#37722;?#37828;?#37828;?#23534;?#37734;?#26473;?#29723;岄亶鍘?澶栭儴閬?#37720;?锛屼絾涓?#37722;?#27996;?#38342;?#37722;?#38171;孞ava娴?#37828;?#37734;?#39582;?#37722;?#26473;?#29723;岄亶鍘?#37510;傝鎴?#28000;噸鏂扮?#23340;湅?#31651;鍓嶇殑绀?#28186;?#28000;?#37934;佹潵瑙?#38322;?#26473;?#27996;?#37720;熺悊銆?#37733;?褰?#29854;?#37734;?#29785;?#38322;婁簡?#31483;鍗??#27537;?#27967;澶?#32513;嗚妭銆?/span>


    棣栧厛锛?#38315;?#26473;囪皟鐢Java娴?)鏂规硶锛?#37812;?#28000;?#28000;?#27996;?#37828;撳垪琛?#28051;?#23536;楀埌?#31777;涓涓狫ava娴佸璞?#37510;?#38317;?#28052;?#37825;版嵁婧?#28751;?#37828;?#27996;?#37828;撳垪琛?#38171;?#28729;冨皢鍚慗ava娴佷腑鎻?#28186;?#28051;涓?#37711;?#32497;?#37510;傜劧鍚?#38171;?#37812;?#28000;?#28725;?#29831;Java娴佸簲鐢簡涓绯诲垪?#27537;?#20187;鍚?#37823;?#28003;?#38171;?#26473;囨护(鎻?#28186;?#28051;涓?#29835;?#29831;?#37720;?#26473;囨护鍏?#32497;?锛?#37818;?#25652;?鎻?#28186;?#28051;涓瘮杈?#37731;?#37720;诲?#29808;厓绱?#26473;?#29723;?#37818;?#25652;?锛?#28000;強鏄?#28751;?瑙?#37835;?#37713;?#28103;伅)銆?#37813;鏈夌殑鎿?#28003;?#38318;?#28028;?#26473;?#37733;?#29831;Java娴?#38171;?#28000;?#28186;?#38003;藉?#29116;摼鎺?#26473;?#27996;?#37823;?#28003;?#37720;荤?#21228;垚涓涓?#32480;亾锛岃繖?#24434;琚?#37930;?#28003;?#37828;?#28725;?#37825;版嵁婧愮殑涓涓?#37836;?#29831;?#37510;?/span>
    鍦?#29835;?#37922;collect()鎿?#28003;滀箣鍓?#38171;屾病鏈?#28729;?#38340;?#23480;?#28003;?#28028;氳鎵?#29723;?#37510;俢ollect()鏂规硶灏嗗紑濮?#28598;勭?#21978;繖涓?#32480;亾浠?#26473;?#37733;?#28051;涓?#32513;撴灉(?#28999;涓?#28051;?#37828;疛ava娴佺殑瀵?#29854;?#38171;?#37734;?#23005;?#28598;?#38171;?#37828;?#28051;涓狶ist瀵?#29854;?銆傜幇鍦?#26473;?#28051;嶉渶瑕?#37720;诲叧娉collect()鏂规硶锛?#37812;?#28000;?#28028;?#37734;?#28000;?#37722;庣殑鏂囩珷鍘讳竴鎺?#32460;?#32468;?#37510;傛鏃?#38171;屼綘浼氬彂?#24135;collect浼氬皢鍚勭鏁版嵁鍔?#23480;?#37826;规硶浣?#28051;?#37721;傛暟锛屽皢?#25977;闆嗗埌?#27537;Java娴佸厓绱?#35120;?#32513;?#28051;?#28051;涓?#32513;撴灉銆傛澶?#38171;宼oList()灏辨?#24538;堪?#31777;涓涓皢Java娴佸璞?#26462;寲涓篖ist瀵?#29854;殑鍔?#23480;?#37826;规硶銆?/span>
    鍦?#37818;?#32460;?#28051;嶫ava娴?#37832;?#37711;崇殑鍚?#28051;?#37826;规?#26330;箣鍓?#38171;?#37832;濂?#37828;?#37707;?#28051;嬫潵?#32321;鍏?#37804;?#38000;?#28051;涓婮ava娴?#37724;?#38342;?#37722;堜箣闂?#29785;傚康涓婄殑涓?#37722;屼箣澶?#37510;?/span>

Java娴?vs. 闆?#37722;?/span>
    宸?#37832;夌殑Java闆?#37722;?#22994;傚康涓?#37826;扮殑Java娴?#22994;傚康閮?#28051;?#28051;涓?#37711;?#32497;犳彁渚涗簡鎺?#37721;?#37510;?#38317;?#28729;?#28000;?#37832;変綍涓?#37722;?#37722;?#38171;?#32480;鍗?#37734;?#29831;?#38171;?#38342;?#37722;?#37828;?#37711;?#27996;庢?#29256;嵁?#27537;锛岃孞ava娴?#37828;?#37711;?#27996;?#29825;?#32480;楃殑銆?/span>
    鎯虫兂杩?#32457;嶆?#21613;喌锛?#28051;?#20788;瀛樺偍鍦DVD涓殑鐢?#35120;?#37510;傝繖灏?#37828;?#28051;涓?#38342;?#37722;??#24434;鑳?#37828;?#28699;?#38010;?#38171;屽彲鑳?#37828;?#29999;?-鍦?#23005;?#28598;?#38171;?#37812;?#28000;?#28051;嶅?#21613;叧蹇?#26473;?#27996;?锛?#37733;?#28051;?#28729;冨寘鍚?#37832;?#37711;儴?#27537;鏁版嵁缁?#37835;?#37510;傜幇鍦?#37712;嶆?#34411;兂杩?#32457;嶆?#21613;喌锛岃繖?#20788;鐢?#35120;?#29722;?#26462;寲鎴愪簡鏁版嵁娴?#38171;?#38315;?#26473;?#27996;掕?#26050;綉鍘?#29785;傜湅瀹?#37510;傛鏃?#28729;?#28751;?#37828;?#28051;涓?瀛?#38010;傛垨甯殑)娴?#37510;?#23092;?#29785;?#26864;戞鏀?#37731;?#37721;渶瑕佷笅杞?#28051;浜涙?#27691;簬鐢埛?#32139;鍓?#37813;瑙傜湅浣嶇疆?#27537;甯?#28751;卞彲浠簡銆傝繖鏍?#38171;屼綘灏卞彲浠?#37734;?#28598;儴鍒嗗?#29722;?#29825;?#32480;楀?#28888;潵?#31651;鍓嶅?#22557;睍绀?#23092;佸紑澶?#28598;勭殑鍊?鎯虫兂娴佸寲涓鍦虹幇鍦虹洿鎾殑瓒?#37918;冩瘮璧?銆?/span>
    绮楃湅?#31651;锛?#38342;?#37722;?#28051;?#23092;佺殑鍖?#37714;?#28751;?#37828;?#28051;?#28003;?#37827;?#28598;勭?#21976;?#29256;嵁鏈?#37711;?#37510;?#38342;?#37722;?#37828;?#37712;呭瓨涓殑鏁版嵁缁?#37835;?#38171;?#28729;冨寘鍚?#37832;夊綋鍓?#37825;版嵁缁?#37835;?#28051;殑鍏儴鍊?-?#30370;鎵鏈?#37711;?#32497;?#37716;?#37711;埌闆?#37722;堜箣鍓?#38171;屽?#21620;?#35826;?#22557;鎵鏈?#37711;?#32497;?#26473;?#29723;?#28598;勭悊锛岀浉鍙嶅湴锛孞ava娴佸彧鏄?#38315;?#26440;?#28051;?#37733;?#28729;氱殑鏁版嵁缁?#37835;?#38171;?#28729;?#38322;岄潰?#27537;鍏?#32497;?#37721;?#28028;氭?#35268;嵁?#28214;瑕?#26473;?#29723;?#28598;勭悊銆?/span>
    ?#23047;鐢Collection鎺?#37721;?#38171;岃?#20345;眰鐢埛瀹炵幇閬?#37720;?渚?#28641;?#38171;屼娇鐢?#28583;?#23534;虹殑for寰?#37916;?#38171;?#37719;foreach)锛?#26473;?#29722;?#32457;颁箣涓?#28598;栭儴寰?#37916;?#37510;傜浉鍙嶅湴锛孲tream绫诲?#25778;娇鐢?#37712;呴儴閬?#37720;?-瀹?#23480;?#32513;?#28051;?#28003;?#28729;炵幇濂戒簡閬?#37720;?#38171;?#28729;?#28028;氬叧蹇?#28699;樺偍娴佺殑缁?#37835;滃肩殑浣嶇疆锛?#28003;?#28000;呴渶瑕佹彁渚?#28051;涓?#37713;芥暟鍛?#29831;?#28729;?#29781;佸?#27691;簺浠涔?#28751;?#29723;屼簡銆傛竻鍗?(瀵?#38342;?#37722;堢殑澶栭儴閬?#37720;?鍜屾竻鍗?(瀵笿ava娴佺殑鍐呴儴閬?#37720;?涓殑浠?#37934;佸舰璞?#37734;板睍绀轰簡杩?#28051;涓?#37722;屼箣澶?#37510;?/span>
?#31483;鍗?
List<Integer> transactionIds = new ArrayList<>();
for(Transaction t: transactions){
    transactionIds.add(t.getId());
}

?#31483;鍗?
List<Integer> transactionIds =
    transactions.stream()
                .map(Transaction::getId)
                .collect(toList());

    鍦竻鍗?涓?#38171;?#37812;?#28000;?#37828;?#23534;?#28051;?#26916;哄簭鍦伴亶鍘嗕簡浜?#37828;撳垪琛?#38171;屾?#34249;彇?#31777;姣?#28051;?#27996;?#37828;揑D锛岀劧鍚庡皢瀹?#37716;犲埌涓涓敹闆?#37731;?#28051;?#37510;傜浉鍙嶅湴锛屽綋?#23047;鐢?#23092;佹椂锛屾病鏈?#37828;?#23534;忕殑閬?#37720;?#37510;傛竻鍗?涓殑浠?#37934;佹?#21227;缓?#31777;涓涓?#37836;?#29831;?#38171;屽叾涓殑map鎿?#28003;?#29722;?#29825;?#28729;?#28051;?#28051;涓?#37721;傛暟锛?#28729;?#28028;氭?#34249;彇浜?#37828;揑D锛岀劧鍚巆ollect鎿?#28003;?#28028;氭妸缁撴灉Stream瀵?#29854;?#26462;寲鎴?#28051;涓狶ist瀵?#29854;?#37510;?/span>
浣犵幇鍦簲璇煡?#20158;浠涔?#37828;疛ava娴?#38171;?#28000;強濡備綍鍘讳娇鐢?#28729;?#37510;傜幇鍦?#29825;?#37812;?#28000;?#37930;嬬湅Java娴佹墍鏀?#37816;佺殑鎿?#28003;滀箣闂寸殑鍖?#37714;?#38171;岃繖鏍?#28003;?#28751;?#38003;芥?#21227;缓?#22172;宸辩殑鏁版嵁鏌?#29831;簡銆?/span>

Java娴佹搷浣?#38171;氫娇鐢?#23092;?#37720;诲?#21229;?#21976;?#29256;嵁

    java.util.stream.Stream鎺?#37721;?#28729;氫箟?#31777;璁?#28598;氭搷浣?#38171;?#28729;?#28000;彲琚?#35120;?#38342;?#28051;?#28051;被銆傚湪鍥?鎵绀虹殑渚嬪瓙涓?#38171;屼?#29362;彲浠?#37930;嬪埌濡?#28051;?#37823;?#28003;?#38171;?/span>
    杩囨护锛?#37818;?#25652;?#37724;?#37828;?#28751;?#38171;?#28729;?#28000;彲琚?#26473;?#37818;?#37734;?#28051;璧?#32513;勬垚涓涓?#32480;亾
    ?#25977;闆?#38171;?#28729;?#37711;?#38338;簡杩?#28051;?#32480;?#38316;撳苟杩?#37733;?#32513;撴灉
    鑳藉琚?#26473;?#37818;?#37734;?#28051;璧风殑Java娴佹搷浣?#29722;?#32457;?#28051;?#28051;?#38338;?#37823;?#28003;?#37510;傝繖浜?#37823;?#28003;滀箣鎵浠?#38003;?#29722;?#26473;?#37818;?#37734;?#28051;璧?#38171;?#37828;?#37733;?#28051;?#28729;?#28000;?#38318;?#28028;?#26473;?#37733;濻tream瀵?#29854;?#37510;傝繖浜?#37823;?#28003;?#28000;?#26473;?#28051;?#32480;亾涓?#26473;?#37733;?#32513;撴灉锛?#32513;撴灉?#27537;绫诲?#23338;彲浠?#37828;List锛孖nteger锛?#37922;氳嚦鏄void(浠讳綍Stream浠?#28598;栫殑绫诲瀷)
    浣?#28052;?#29825;?#23536;堝?#34249;涓?#28000;涔?#26473;?#32457;嶅尯鍒?#23536;堥噸瑕?#37510;?#37828;?#26473;?#37837;风殑锛?#37734;?#26473;?#28051;狫ava娴佺?#20158;?#27537;鏈缁?#37823;?#28003;?#29722;?#29835;?#37922;箣鍓?#38171;?#28051;?#38338;?#37823;?#28003;滃苟涓?#28028;氭墽琛?#28000;讳綍澶勭悊锛?#28729;?#28000;?#37828;?鎯?#37804;?鏂规硶銆傝繖鏄?#37733;?#28051;?#28051;?#38338;?#37826;规硶缁?#29999;?#28028;氳"鍚堝苟"锛?#37734;?#37832;缁?#37823;?#28003;?#28051;?#28729;?#28000;?#28028;氳鍚?#37812;?#28051;?#37719;?#28051;?#27537;鎵?#29723;岃矾寰?#37510;?/span>
?#31483;鍗?
List<Integer> numbers = Arrays.asList(12345678);
List
<Integer> twoEvenSquares =
    numbers.stream()
           .filter(n 
-> {
                    System.out.println(
"filtering " + n);
                    
return n % 2 == 0;
                  })
           .map(n 
-> {
                    System.out.println(
"mapping " + n);
                    
return n * n;
                  })
           .limit(
2)
           .collect(toList());

    渚?#28641;?#38171;?#37804;?#38000;?#28051;嬫竻鍗?涓殑绋嬪簭锛?#28729;?#37828;?#28000;?#32513;?#28729;氱殑鏁板垪涓?#29825;?#32480;楀?#22248;?#25198;殑骞?#37826;?#37510;備?#29362;彲鑳?#28028;氬緢鎯?#29825;?#38171;?#28729;?#37813;?#37719;?#37713;?#28641;?#28051;?#32513;撴灉锛?/span>
filtering 1
filtering 
2
mapping 
2
filtering 
3
filtering 
4
mapping 
4

    杩?#37828;?#37733;?#28051;limit(2)?#23047;鐢簡?#29037;?#30718;锛?#37812;?#28000;?#37721;渶瑕?#28598;勭悊娴佺殑涓?#20788;鍒?#38171;岃?#28051;?#37828;?#37711;儴锛?#37720;诲?#26944;埌涓涓?#32513;撴灉銆傝繖灏辩?#35763;技浜?#23092;?#29831;?#28051;涓?#37922;and鎿?#28003;?#32471;?#37711;宠仈璧锋潵?#27537;澶?#37736;?#29999;?#28751;?#29723;?#26440;?#23534;忛摼锛?#28051;鏃煇涓?#29723;?#26440;?#23534;忚繑鍥炰簡false锛?#38317;?#28052;?#28751;卞彲浠?#29825;?#28051;烘暣涓?#29723;?#26440;?#23534;忛摼灏?#37828;false锛岃?#28051;嶅繀娴?#29831;勬墍鏈夌殑琛?#26440;?#23534;忎簡銆傚湪杩?#28051;?#28186;嬪瓙涓?#38171;宭imit()鏂规?#26334;皢杩?#37733;炵殑Java娴佺殑闀垮害闄?#28729;?#28051;?銆傚彟澶?#38171;宖ilter涓巑ap琚?#37722;堝苟鍦簡鍚?#28051;鏉?#37813;?#29723;岃矾寰?#28051;簡銆?/span>

    褰掔撼涓涓嬪埌鐩?#37715;?#28051;烘锛?#37734;娇鐢Java娴佹?#33333;垜浠?#37813;瀛埌?#27537;鍐?#28729;?#38171;?#37804;昏?#31651;锛屾?#22794;強涓?#28051;?#37826;?#38344;?#38171;?/span>
    涓涓?#37825;版嵁婧?渚?#28641;?#28051;涓?#38342;?#37722;?锛屽瀹?#37813;?#29723;屾煡璇?/span>
    涓涓?#28051;?#38338;?#37823;?#28003;滅殑?#25724;锛?#28729;冪?#21228;垚涓涓?#23092;佺殑绠亾
    涓涓?#37832;缁?#37823;?#28003;?#38171;?#28729;?#37813;?#29723;?#23092;佺殑绠?#38316;撳苟浜?#37922;?#32513;撴灉
    ?#24135;鍦?#29825;?#37812;?#28000;?#37930;嬬湅Java娴佹墍鏀?#37816;佺殑涓浜?#37823;?#28003;?#37510;傚?#20637;僯ava.util.stream.Stream鎺?#37721;彲浠?#23536;楀埌杩?#27996;?#37826;规?#26336;殑瀹屾暣?#31483;鍗?#38171;屽?#23943;?#23340;湅鏈?#37826;?#37832;?#28751;?#37813;缁?#37713;虹殑璧勬簮锛?#28729;冨寘鍚?#37832;夋洿澶氱殑渚嬪瓙銆?/span>

    杩囨护銆?#37832;?#28598;?#32457;?#37826;规?#26334;彲浠?#28725;?#23092;佷腑?#27537;鍏?#32497;?#26473;?#29723;岃繃?#25252;锛?/span>
    filter(Predicate)锛氫娇鐢?#28051;涓?#29835;?#29831;?java.util.function.Predicate)浣?#28051;?#37721;傛暟锛?#28729;?#28028;?#26473;?#37733;?#28051;涓寘鍚?#37813;鏈?#37718;归?#23943;粰瀹氳皳璇?#37833;?#28000;跺厓绱犵殑Java娴?#37510;?/span>
    distinct锛?#26473;?#37733;?#28051;涓寘鍚?#37832;夊敮涓鍏?#32497;犵殑Java娴?#37510;?/span>
    limit(n)锛?#26473;?#37733;炵殑娴佺殑闀垮害涓?#38003;借秴杩n銆?/span>
    skip(n)锛?#26473;?#37733;炵殑娴佸皢涓嶅寘?#23277;鍓n涓?#37711;?#32497;?#37510;?/span>

    鏌?#37813;?#28051;庡?#24402;厤銆?#28051;涓?#38315;?#37922;殑鏁版嵁澶勭?#21976;寮?#28751;?#29781;佽?#38095;?#28051;浜涘厓绱?#37828;?#37722;?#37718;归?#23943;粰瀹氱殑灞?#37804;?#37510;備?#29362;彲浠娇鐢anyMatch锛宎llMatch鍜宯oneMatch鏂规硶甯?#28003;犲?#27692;埌杩?#28051;?#20579;銆傝繖浜?#37826;规硶閮?#28028;氫娇鐢?#28051;涓?#29835;?#29831;?#37721;傛暟骞?#26473;?#37733;boolean鍊?#28003;?#28051;?#32513;撴灉(鎵浠?#38171;?#28729;?#28000;?#37828;?#37832;缁?#37823;?#28003;?銆備緥濡?#38171;屼娇鐢allMatch鍘?#37836;?#37713;?#27996;?#37828;?#23092;佷腑鎵鏈?#38322;戦澶?#27996;?00?#27537;浜?#37828;?#38171;?#28641;傛竻鍗?鎵绀?#37510;?/span>
?#31483;鍗?
boolean expensive =
    transactions.stream()
                .allMatch(t 
-> t.getValue() > 100);

    鍙?#28598;?#38171;孲tream鎺?#37721;?#37819;?#28186;涗簡鏂规硶findFirst鍜宖indAny锛?#28000;?#37721;栧嚭娴佷腑浠讳竴鍏?#32497;?#37510;傚畠浠彲浠?#28051;庡叾瀹冪殑娴佹搷浣?#38171;?#28641;俧ilter锛?#32513;?#37722;?#29863;锋潵?#23047;鐢?#37510;俧indFirst鍜宖indAny閮?#28028;?#26473;?#37733;?#28051;涓狾ptinal瀵?#29854;?瑙佹竻鍗?)銆?/span>
?#31483;鍗?
Optional<Transaction> =
    transactions.stream()
                .findAny(t 
-> t.getType() == Transaction.GROCERY);

    Optional<T>?#34987;(java.util.Optional)鏄?#28051;涓?#28729;?#37731;被锛?#28729;?#28000;?#29723;?#28051;涓?#28699;?#37734;垨涓嶅瓨鍦殑鍊?#37510;傛竻鍗?涓殑绋嬪簭锛宖indAny鏂规?#26334;彲鑳芥病鏈?#37813;惧埌浠讳?#26336;?#35826;瀷涓篻rocery?#27537;浜?#37828;?#37510;侽ptional绫诲寘鍚?#28598;?#28051;?#37826;规硶鍘?#23092;?#29831;?#28051;涓?#37711;?#32497;?#37828;?#37722;?#28699;?#37734;?#37510;備緥濡?#38171;?#28641;傛灉浜?#37828;撳瓨鍦?#38171;?#38315;?#26473;囦娇鐢ifPresent鏂规硶锛?#37812;?#28000;彲浠?#38315;夋嫨涓涓?#37823;?#28003;?#37720;诲簲鐢?#26473;?#28051;狾ptaional瀵?#29854;?#38171;?#28641;傛竻鍗?鎵绀?姝?#28598;勫彧鏄?#37813;?#37719;?#27996;?#37828;?銆?/span>
?#31483;鍗?
transactions.stream()
              .findAny(t 
-> t.getType() == Transaction.GROCERY)
              .ifPresent(System.out::println);

    鏄?#28751;?/strong>銆侸ava娴佹敮?#23508;map鏂规硶锛?#28729;冧娇鐢?#28051;涓?#37713;芥暟(java.util.function.Function)浣?#28051;?#37721;傛暟锛屽皢娴佸厓绱?#37814;?#35120;卞埌鍏?#28729;?#35120;?#23534;?#37510;傝繖涓?#37713;芥暟浼氳?#31794;鐢埌姣?#28051;?#37711;?#32497;?#38171;屽?#36346;?#21975;厓绱?鏄?#28751;??#22476;鏂扮殑鍏?#32497;?#37510;?/span>
    渚?#28641;?#38171;屼?#29362;彲鑳?#28028;氭兂?#22476;?#23047;鐢?#28729;?#37720;?#37814;藉彇娴佷腑姣?#28051;?#37711;?#32497;犵殑淇伅銆傚湪?#31483;鍗?0?#27537;渚嬪瓙涓?#38171;?#37812;?#28000;?#26473;?#37733;炰簡涓涓垪琛?#28051;?#23011;?#28051;?#28699;楃殑闀垮害銆?/span>
?#31483;鍗?0
List<String> words = Arrays.asList("Oracle""Java""Magazine");
 List
<Integer> wordLengths =
    words.stream()
         .map(String::length)
         .collect(toList());

    褰?#28051;銆傚埌鐩?#37715;?#28051;烘锛?#37812;?#28000;?#23480;?#29785;?#26473;囩殑鏈缁?#37823;?#28003;?#28028;?#26473;?#37733;boolean(allMatch绛?#32475;?锛寁oid(forEach)?#22440;Optaional瀵?#29854;?findAny绛?#32475;?銆?#37812;?#28000;?#28052;熶娇鐢collect鏂规?#26334;皢Stream瀵?#29854;?#28051;殑鎵鏈?#37711;?#32497;?#37824;惧埌涓涓狶ist瀵?#29854;?#28051;?#37510;?/span>
    ?#21159;鑰?#38171;屼綘涔熷彲浠皢娴佷腑?#27537;鍏?#32497;?#37824;惧埌涓涓?#37836;?#29831;?#28051;?#38171;岃鏌?#29831;彲琛?#26440;炬洿涓?#28598;?#37833;傜殑鏁版嵁澶勭悊锛屼緥濡??#23266;鏈?#37832;澶ID"?#22440;鑰?绠楀嚭鎵浠?#27996;?#37828;?#38322;戦?#28616;殑鍜?銆傝繖灏卞彲鑳藉?#31551;ava娴佺敤涓妑educe鏂规硶锛岃鏂规硶浼氬姣?#28051;?#37711;?#32497;犻?#23941;?#23941;湴?#31794;鐢?#28051;涓?#37823;?#28003;?渚?#28641;?#38171;屽姞涓?#28051;?#28051;?#37825;?#28699;?锛岀洿?#22476;鐢?#37812;愮粨?#28745;銆傚湪鍑芥暟寮?#32514;栫涓?#38171;岃繖甯歌绉?#28051;烘鍙?#37823;?#28003;?#37510;?#37733;?#28051;?#29831;?#37823;?#28003;滃彲琚?#37930;?#28003;滈?#23941;?#23941;湴"鎶?#37721;?涓?#32054;寰?#38336;跨殑绾?Stream瀵?#29854;?锛岀洿?#22476;杩欏紶绾哥殑闈?#32457;彉寰楀彧鏈?#28051;鐐瑰?#22840;簡銆傝繖灏?#37828;?#37814;?#37721;?#37823;?#28003;滅殑缁撴灉銆?/span>
    鐪嬬湅鎴?#28000;?#37828;?#28641;備綍?#23047;鐢?#23536;?#37916;?#37720;?#29825;?#32480;?#28051;涓?#32513;勬暟瀛楃殑鍜?#28028;?#37832;?#37716;?#27996;庣悊瑙?#26473;?#28051;?#38338;?#26864;?#38171;?/span>
int sum = 0;
for (int x : numbers) {
  sum 
+= x;
}

    ?#22442;琛?#28051;殑姣?#28051;涓?#37825;?#28699;楀厓绱?#38318;?#29722;?#26473;?#28000;?#37734;扮粍鍚?#37734;?#28051;璧?#38171;屽?#26421;娇鐢?#28051;涓?#26864;?#28598;栫殑鎿?#28003;?#32471;?#37720;讳骇鐢?#32513;撴灉銆?#37832;?#29840;?#28051;?#38171;?#37812;?#28000;?#28751;?#37828;?#37814;?#28051;缁勬暟瀛?褰?#28051;"鎴?#28051;涓?#37825;?#28699;?#37510;傚湪杩?#23048;?#28000;?#37934;佷腑鏈?#28051;?#28051;?#37721;傛暟锛氭暟瀛?#37724;屽彉閲忕殑鍒?#28654;嬪?#38171;?#37719;?#29831;?#28186;?#28051;殑0锛?#28000;強鐢?#27996;?#37722;堝?#33333;墍鏈?#37711;?#32497;犵殑鎿?#28003;?#32471;?#38171;?#37719;?#37832;?#28186;?#28051;殑+銆?/span>
?#31483;鍗?1
int sum = numbers.stream().reduce(0, (a, b) -> a + b);

    瀵笿ava娴佷娇鐢reduce鏂规硶锛?#37812;?#28000;彲浠?#29825;?#32480;楀嚭娴佷腑?#27537;鎵鏈?#37711;?#32497;犲间箣鍜?#38171;?#28641;傛竻鍗?1鎵绀?#37510;俽educe鏂规?#26330;娇鐢?#28051;?#28051;?#37721;傛暟锛?/span>
    鍒?#28654;嬪?#38171;?
    BinaryOperation<T>锛?#37722;堝苟涓?#28051;?#37711;?#32497;?#38171;屽苟浜?#37922;?#28051;涓?#37826;?#37706;?/span>
    reduce鏂规硶鏈?#29840;?#28051;?#28751;?#37828;?#38322;嶅?#23941;簲鐢?#22959;?#23534;忕殑鎶借薄銆傚叾瀹冪殑鏌?#29831;?#38171;?#28641;?璁?#32480;?#27996;?#38322;??#22440;"璁?#32480;?#37832;澶?#37706;?(濡傛竻鍗?2鎵绀?鍒?#37828;痳educe鏂规?#26336;殑鐗瑰埆瀹?#28186;?#37510;?/span>
?#31483;鍗?2
int product = numbers.stream().reduce(1, (a, b) -> a * b);
int product = numbers.stream().reduce(1, Integer::max);

鏁?#28699;?#23092;?/span>
    浣?#23480;?#32513;忕?#23338;埌?#24434;浠娇鐢reduce鏂规硶鍘?#29825;?#32480;楁暣鏁?#23092;佺殑鍜?#37510;傜劧鍚?#38171;岃繖涔?#37828;?#37832;?#37812;?#37832;殑锛?#37812;?#28000;?#38322;嶅鎵?#29723;屼簡璁?#28598;氭媶绠?#37823;?#28003;?#28000;皢Integer瀵?#29854;?#37716;犲埌涓璧?#37510;傚?#20635;灉鎴?#28000;?#38003;借皟鐢?#28051;涓猻um鏂规硶锛屼娇绋嬪?#24533;殑?#21104;鍥炬洿涓?#37828;庢樉锛屽氨鍍忔竻鍗?3閭?#37837;?#38171;屽矀涓?#37828;洿濂?#38171;?/span>
?#31483;鍗?3
int statement =
    transactions.stream()
                .map(Transaction::getValue)
                .sum(); 
// error since Stream has no sum method

    Java 8?#32041;鍏殑涓?#28051;?#37911;?#28729;氱殑?#29056;鏈?#37825;版嵁绫诲?#23340;殑娴?#37818;?#37721;潵?#31794;瀵?#26473;?#28051;?#38338;?#26864;?-IntStream锛孌oubleStream鍜孡ongStream--瀹?#28000;?#28051;撴敞浜庡厓绱?#37714;嗗埆涓int锛宒ouble鍜宭ong鍨嬬殑Java娴?#37510;傚皢涓涓?#23092;?#26462;寲涓?#37911;?#28729;?#32491;诲?#23340;殑娴?#38171;屼綘鏈甯镐娇鐢殑鏂规硶灏?#37828;mapToInt锛宮apToDouble鍜宮apToLong銆傝繖浜?#37826;规硶涓庢垜浠?#26440;?#37827;?#37715;嶇?#23338;埌?#27537;map鏂规硶鏄?#28051;鏍风殑锛屼絾瀹?#28000;?#28028;?#26473;?#37733;?#37911;?#28729;?#32491;诲?#23340;殑Stream瀵?#29854;?#38171;岃?#28051;?#37828;疭tream<T>瀵?#29854;?#37510;備緥濡?#38171;?#37812;?#28000;彲浠?#37824;?#26473;?#28051;嬫竻鍗?3涓殑浠爜锛?#28641;傛竻鍗?4鎵绀?#38317;?#37837;?#37510;備綘涔熷彲浠娇鐢?#29761;?#32480;?#37823;?#28003;滃皢涓涓熀鏈?#37825;版嵁绫诲?#23340;殑娴?#26462;寲鎴?#28051;涓娇鐢寘瑁呭璞殑娴?#37510;?/span>
?#31483;鍗?4
int statementSum =
    transactions.stream()
                .mapToInt(Transaction::getValue)
                .sum(); 
// works!

    鏈鍚?#38171;屾暟瀛?#23092;佺殑鍙?#28051;绉?#37832;?#37922;殑褰?#23534;?#37828;?#37825;?#28699;?#37718;?#38338;?#37510;傛瘮濡?#38171;屼?#29362;彲鑳芥兂鐢?#37812;?#28000;?#27996;??#22476;100?#31651;闂寸殑鎵鏈?#37825;?#28699;?#37510;?#28051;轰簡甯?#37716;?#37922;?#37812;?#26473;?#32457;嶅尯闂?#38171;孞ava SE 8鍦IntStream锛孌oubleStream鍜孡ongStream涓?#37714;嗗埆?#32041;鍏簡涓?#28051;?#38344;?#37804;?#37826;规硶锛range鍜宺angeClosed銆?/span>
    杩?#28051;?#28051;?#37826;规硶閮?#28028;氫娇鐢?#28051;?#28051;?#37721;傛暟锛岀涓涓?#37721;傛暟鏄?#29863;?#28654;嬪?#38171;岀浜?#28051;?#37721;傛暟鏄?#32513;?#23005;?#37706;?#37510;備絾鏄痳ange鏂规硶鐢?#37812;愮殑鍖?#38338;?#28051;?#28028;氬寘鍚?#32513;?#23005;?#37706;?#37832;韩锛屼絾rangeClosed鐢?#37812;愮殑鍖?#38338;?#37714;?#28028;氬寘鍚?#37510;傛竻鍗?5鏄?#28051;涓娇鐢rangeClosed鏂规?#26336;殑渚嬪瓙锛?#28729;?#26473;?#37733;?#28051;涓寘鍚?#37832;?#37711;儴浠?#27996;?0?#22476;30?#31651;闂?#28610;囨?#25198;殑娴?#37510;?/span>
?#31483;鍗?5
IntStream oddNumbers =
    IntStream.rangeClosed(
1030)
             .filter(n 
-> n % 2 == 1);

鏋勫缓娴?/span>
    鏈?#28598;?#32457;?#38315;?#23536;勫彲浠?#37720;?#37835;勫缓涓涓?#23092;?#37510;備綘宸?#32513;忕湅杩?#28641;備綍浠?#38342;?#37722;堝璞?#28051;?#37835;勫缓娴?#37510;傚彟澶?#38171;?#37812;?#28000;?#26473;樻搷鎺?#26473;囨暟瀛?#23092;?#37510;備綘涔熷彲浠?#28000;庡?#38171;屾暟缁勬垨鏂?#28000;?#28051;?#37720;诲?#28056;缓娴?#37510;傚彟澶?#38171;屼綘鐢氳嚦浜庡彲浠?#28000;?#28051;涓?#37713;芥暟涓?#37922;?#37812;?#37827;?#38340;?#23092;?#37510;?/span>
    ?#24434;浠洿鎴簡?#32139;鍦?#28000;庡兼垨鏁扮粍涓?#37714;涘缓娴?#38171;氬彧?#28214;瑕佷娇鐢?#28051;浜?#38344;?#37804;?#37826;规硶鍗冲彲锛屽浜庡?#38171;?#37828;疭tream.of()锛涜屽浜庢?#25198;粍锛屽垯瑕佽皟鐢Arrays.stream()銆傚?#20635;竻鍗?6鎵绀?#37510;?/span>
?#31483;鍗?6
Stream<Integer> numbersFromValues = Stream.of(1234);
int[] numbers = {1234};
IntStream numbersFromArray 
= Arrays.stream(numbers);

    浣?#28052;熷彲浠皢涓涓?#37826;?#28000;?#26462;寲涓?#37711;跺唴瀹?#29723;岀殑娴?#38171;屼娇鐢?#38344;?#37804;?#37826;规硶Files.lines()鍗冲彲銆傛竻鍗?7灏变娇鐢?#29831;?#37826;规硶璁?#32480;椾簡鏂?#28000;?#28051;?#29723;岀殑鏁?#38322;?#37510;?/span>
?#31483;鍗?7
long numberOfLines =
    Files.lines(Paths.get(“yourFile.txt”), Charset.defaultCharset())
         .count();

    鏃?#38340;?#23092;?/strong>銆?#37832;鍚?#38171;?#37734;?#37804;荤粨鏈?#37826;囦箣鍓?#38171;?#37832;?#28051;涓?#28000;?#27996;?#38344;?#29999;?#37711;?#28610;嬬殑涓绘剰銆傚埌?#24135;鍦?#28051;烘锛屼?#29362;簲璇悊瑙埌娴佷腑?#27537;鍏?#32497;?#37828;?#37816;夐渶鐢?#37812;愮殑銆?#37832;?#28051;?#28051;?#38344;?#37804;?#37826;规硶--Stream.iterate()鍜孲tream.generate()--?#24434;浠?#29825;?#28003;?#28000;?#28051;涓?#37713;芥暟涓?#37714;涘缓娴?#37510;傜劧鑰?#38171;?#37733;?#28051;?#29722;娇鐢殑鍏?#32497;?#37828;?#37816;夐渶鐢?#37812;愮殑锛?#37813;浠?#26473;?#28051;?#28051;?#37826;规?#26334;彲浠?姘?#26473;?#37734;?鐢?#37812;?#37711;?#32497;?#37510;傝繖灏?#37828;?#28051;?#28000;涔?#37812;?#28000;?#32457;?#28729;?#28051;?#37827;?#38340;?#23092;?#38171;?#28729;?#28751;?#37828;?#23052;?#37832;?#37733;?#28729;氬灏忕殑娴?#38171;屼絾瀹冨?#27697;殑浜嬫儏涓?#28051;涓?#28000;?#37733;?#28729;?#38342;?#37722;?#37922;?#37812;愮殑娴?#37828;?#28051;鏍风殑銆?/span>
    ?#31483;鍗?8灏?#37828;?#28051;涓娇鐢iterate鏂规?#26336;殑渚嬪瓙锛?#28729;?#28028;氬寘鍚?0?#27537;鎵鏈?#37706;?#37825;?#37510;俰terate鏂规?#26330;娇鐢?#28051;涓?#29863;?#28654;嬪?姝?#28598;勭殑0)鍜?#28051;涓狶ambda琛?#26440;?#23534;?绫诲瀷涓篣naryOperator<T>)鍘?#26916;哄簭鍦?#37922;?#37812;?#23011;?#28051;涓?#37826;?#37706;?#37510;?/span>
?#31483;鍗?8
Stream<Integer> numbers = Stream.iterate(0, n -> n + 10);

    鎴?#28000;?#28052;熷彲浠娇鐢limit鏂规硶锛?#28000;?#28000;?#28051;涓?#37827;?#38340;?#23092;佷腑寰楀埌涓涓?#37733;?#28729;?#23092;?#37510;傚?#20635;竻鍗?9鎵绀?#38171;屽彲浠皢娴佺殑闀垮害闄?#37714;?#28051;?銆?/span>
?#31483;鍗?9
numbers.limit(5).forEach(System.out::println); // 0, 10, 20, 30, 40

缁?#29825;?/span>
    Java SE 8?#32041;鍏簡Streams API锛?#28729;?#29825;?#28003;?#38003;藉琛?#26440;炬洿涓?#28598;?#37833;傜殑鏁版嵁澶勭?#21976;煡璇?#37510;傚湪鏈?#37826;?#28051;?#38171;屼綘宸?#29785;佸埌娴佸彲浠?#37824;寔璁?#28598;氭搷浣?#38171;岃濡傝繃?#25252;锛?#37828;?#28751;?#38171;屽綊涓鍜岃凯浠?#38171;屾妸瀹?#28000;?#32513;?#37722;?#37734;?#28051;璧峰彲浠?#37712;?#37713;虹畝娲佺殑銆佹洿瀵岃?#24135;鍔涚殑鏁版嵁澶勭?#21976;煡璇?#37510;傝繖绉?#37826;扮殑缂栫鏂规硶杩?#28051;?#37722;?#27996;嶫ava SE 8?#31651;鍓嶇殑闆?#37722;?#28598;勭悊銆備絾鏄?#38171;?#28729;?#37832;?#29825;?#28598;氬?#34249;銆傞?#26663;厛锛?#28729;?#37714;?#37922;埌?#31777;璇?#28641;傛儼鎬垨?#29037;?#30718;杩?#37837;风殑鎶鏈?#38171;?#28000;?#28028;樺寲鏁版嵁澶勭?#21976;煡璇殑鎬?#38003;?#37510;傚?#33333;锛岃?#34249;?#29115;嚜鍔?#37734;?#37714;?#37922;?#28051;?#28598;氭牳鏋舵瀯锛?#28000;?#39582;惰鍦?#28598;勭悊娴?#37510;傚湪鏈?#37826;囩珷绯荤?#29114;殑绗?#27996;岄儴鍒?#28051;?#38171;?#37812;?#28000;皢鎺储?#27967;?#29678;绾殑鎿?#28003;?#38171;屼緥濡俧latMap鍜宑ollect銆?#29831;?#32513;?#32513;?#37711;?#23049;?#37510;?/span>


]]>2014骞?4鏈堢編鍥?#38344;?#37712;?#28051;氬氨涓氭?#21613;喌鎶?#37723;?璇?http://www.8634070.com/jiangshachina/archive/2014/05/05/413246.htmlJohn JiangJohn JiangSun, 04 May 2014 16:12:00 GMThttp://www.8634070.com/jiangshachina/archive/2014/05/05/413246.htmlhttp://www.8634070.com/jiangshachina/comments/413246.htmlhttp://www.8634070.com/jiangshachina/archive/2014/05/05/413246.html#Feedback1http://www.8634070.com/jiangshachina/comments/commentRss/413246.htmlhttp://www.8634070.com/jiangshachina/services/trackbacks/413246.html
2014骞?4鏈堢編鍥?#38344;?#37712;?#28051;氬氨涓氭?#21613;喌鎶?#37723;?/span>
鏈?#37826;?#37828;?#28051;?#37723;?#27996;?2014.05.02)鎵嶅彂甯冪殑
缇?#37733;?014骞?4鏈堝害闈?#37712;?#28051;氬氨涓氭姤鍛?/a>涓殑姒傝?#20352;儴鍒?#38171;?#28051;?#26462;?#28000;舵妧鏈?#23011;?#37827;?#39582;?#32495;?#37510;傝繖浠芥姤鍛?#37734;?#37713;?#37721;?#37722;庣殑鏋佺煭鏃?#38338;?#37712;呭?#29808;姹?#29999;傚満閫?#37812;愪簡鏋?#28598;?#35120;?#37725;?#38171;岀绮?#37828;?#37713;?#27996;庡?#34249;?#22249;湅?#31777;涓涓?#26473;?#28000;芥姤鍛?#38171;屽?#22476;?#31777;涓嶅?#25123;嫳鏂?#29723;?#28051;?#37722;?#32457;?#38171;?#28000;強鑻?#37826;?#37734;?#37825;?#28699;?#37826;?#38344;殑鐢?#23049;?#37510;?2014.05.05鏈鍚庢洿鏂?

    浠?#28598;?#38171;岀編鍥藉姵宸?#32513;?#29825;?#32515;?#37814;?#37723;?#38171;?鏈?#28000;界殑鍏儴闈?#37712;?#28051;氬氨涓氫?#28888;暟澧?#37716;犱簡288000锛岃?#28051;?#28598;?#28051;氱巼闄?#28003;庝簡0.4涓?#37927;?#37714;嗙偣锛岃?#24807;埌鐧?#37714;嗕箣6.3銆傚氨涓氬?#28858;?#36328;殑鎵?#28598;?#37721;?#37929;?#27996;?#28051;?#28051;?#28051;庡晢涓?#37832;嶅姟涓?#38171;?#38342;跺敭涓?#38171;岄?#24880;涓氬拰?#32531;绛?#28051;?#37510;?/span>

瀹跺涵璋?#37836;?#37825;版嵁
    鍦?鏈?#28000;?#38171;?#28598;?#28051;氱巼浠庣櫨鍒嗕箣6.7闄嶅埌?#31777;鐧?#37714;嗕箣6.3锛?#28598;?#28051;氫?#28888;暟鍒?#28051;?80涓?#38171;屽噺灏戜簡73.3涓?#37510;傝繖涓?#26916;?#32513;?#29825;睍绀轰簡鍓?涓?#37832;堢殑鍔?#37804;?#37510;?#28000;?#39582;?#28000;潵锛?#28598;?#28051;氱巼鍜?#28598;?#28051;氫?#28888;暟鍒嗗埆鍑?#28751;戜簡1.2涓?#37927;?#37714;嗙?#29808;拰190涓?#27996;?#37510;?璇?#29785;佽A-1)
    鍦?#28051;?#29781;佺殑鍔?#37716;?#38000;呭?#21977;粍涓?#38171;?鏈堢殑澶?#28051;氱巼鍒嗗埆涓?#38340;嶄簡锛?#37812;?#39582;寸敺瀛?5.9%)锛?#37812;?#39582;?#28610;?#28699;?5.7%)锛岄潚灏?#39582;?19.1%)锛岀櫧浜?5.3%)锛岄粦浜?11.6%)锛?#28000;強瑗跨彮鐗欒(7.3%)銆備?#27699;?#26050;殑鏃?#28051;氱巼涓?.7%(鏈?#37813;?#38340;?#28699;?#38010;?#37804;?#37733;?#32497;?锛?#28000;?#39582;?#28000;潵锛岃鍊肩殑?#24393;?#23538;寰?#28751;?#37510;?璇?#29785;佽A-1锛孉-2鍜孉-3)
    鍦?鏈?#38322;?#38171;屽?#23942;澶?#28051;氳?#28051;?#37826;?#23480;?#28003;?#38000;呭?#21975;埆鍑?#28751;戜簡41.7涓?#37724;?2.6涓?#37510;?鍐嶆澶?#28051;氳呬箣鍓嶅伐浣?#26473;?#38171;屼絾鍦?#23534;濮嬮噸鏂?#37813;?#23480;?#28003;滀箣鍓?#28000;?#28000;?#39582;?#28051;嶇畻浣滃姵鍔?#37716;?#38171;涜?#37826;?#23480;?#28003;?#38000;?#37828;寚?#31651;鍓?#28000;?#37832;?#23480;?#28003;?#26473;囩殑浜?#37510;?涓?#37818;?#23480;?#28003;滅殑鍜屽彧瀹?#37812;?#28051;?#37827;跺伐浣滅殑鍔?#37716;?#38000;呯殑鏁?#38322;?#37713;?#28751;戜簡25.3涓?#38171;岃?#24807;埌520涓?#37510;?璇?#29785;佽A-11)
    闀?#37832;熷涓氳??#23514;澶?#28051;氭椂闂?#26440;惧埌?#22440;?#31220;杩?7鍛殑浜??#27537;鏁?#38322;?#37734;?鏈?#28000;藉噺灏戜簡28.7涓?#38171;岃?#24807;埌350涓?#38171;?#26473;?#27996;?#28051;綋鍗?#28598;?#28051;氫?#28888;?#25198;殑35.3%銆傚湪杩?#37720;荤殑12涓?#37832;?#38322;?#38171;岄暱鏈熷涓氳呯殑鏁?#38322;?#23480;?#32513;?#37713;?#28751;戜簡90.8涓?#37510;?璇?#29785;佽A-12)
    ?#27554;?#28483;姘?#37922;?#37716;?#37716;?#37716;?#37734;?鏈堝?#28858;暱50.3涓囦箣鍚?#38171;屾皯鐢?#37716;?#37716;?#37716;?#37734;?鏈?#38322;屽噺灏戜簡80.6涓?#37510;傚湪鏈杩?#37713;?#28051;?#37832;?#38322;?#38171;屽氨涓氬弬涓庣巼骞舵病鏈?#37828;?#32448;?#37713;烘?#21614;?#25198;殑瓒嬪娍锛屽綋鍓嶇殑鍊?#28052;?#37720;诲勾10鏈堢殑鍊?#37828;浉鍚岀殑銆傚湪杩?#37720;?#26473;?#28051;?#37832;?#28051;?#38171;屽氨涓氳?#28051;?#27996;?#37721;瘮渚嬫病鏈夊彂鐢熷彉?#23538;(58.9%)锛屼絾鍦?#26473;?#28051;骞?#28051;?#37923;?#37832;夊彉?#23538;銆?璇?#29785;佽A-1)
    鍑?#27996;?#32513;?#23092;?#37720;?#37733;犵殑鍏艰?#23677;姵鍔?#38000;?鏈?#37827;跺?#28052;?#32457;颁箣涓?#28051;嶆?#21614;?#36328;殑鍏艰?#23677;姵鍔?#38000;??#27537;鏁?#38322;?#37734;?鏈?#37923;?#37832;夊彉?#23538;锛岃?#24807;埌750涓?#37510;傝繖浜?#28051;綋?#31651;鎵浠?#37711;艰亴鏄?#37922;变簬浠?#28000;殑宸?#28003;?#37827;?#38338;?#29722;?#37715;?#37713;?#38171;屾垨鑰?#37828;?#28000;?#28000;?#37827;犳硶鎵惧埌鍏?#38001;屽伐浣?#37510;?璇?#29785;佽A-8)
    4鏈?#28000;?#37832;?20涓囧?#21975;緟涓氬姵鍔?#37716;?#38171;屾瘮浠?#39582;?#37827;?#27996;?#37827;跺?#37832;?#26462;诲井涓?#38340;?璇?#37825;版嵁鏈?#37813;?#38340;?#28699;?#38010;?#37804;?#37733;?#32497;?銆傝繖浜?#28051;綋骞?#28051;嶇畻浣滃姵鍔?#37716;?#38171;屽敖绠?#28000;?#28000;?#37808;挎剰骞?#28051;?#28052;熷彲浠?#37720;诲伐浣?#38171;岃?#28051;?#37734;?#37715;?2涓?#37832;堢殑?#28999;浜?#37827;跺?#28052;?#37813;?#26473;?#23480;?#28003;?#37510;備絾鏄?#28000;?#28000;?#39582;?#28051;?#29722;?#29825;?#32480;?#37734;?#28598;?#28051;氳?#28051;?#38171;?#37733;?#28051;?#37734;?#29831;?#29835;?#37836;?#23534;濮嬬殑鍓?鍛?#37712;?#38171;?#28000;?#28000;?#39582;舵病鏈?#37720;?#37813;?#23480;?#28003;?#37510;?璇?#29785;佽A-16)
    鍦?#26473;?#27996;涘?#21975;緟涓氳?#28051;?#38171;?#37832;?#37832;?#37832;?8.3涓?#28751;?#28051;氫俊蹇?#28051;?#28598;?#38000;?#38171;屾瘮浠?#39582;?#37827;?#27996;?#37827;跺?#37923;?#37832;夊彉?#23538;(璇?#37825;版嵁鏈?#37813;?#38340;?#28699;?#38010;?#37804;?#37733;?#32497;?銆傚氨涓氫俊蹇?#28051;?#28598;?#38000;?#37929;?#37715;嶅?#33333;病鏈?#37720;诲鎵?#23480;?#28003;?#38171;?#37733;?#28051;?#28000;?#28000;?#29825;?#28051;烘病鏈?#37722;?#38315;?#28000;?#28000;殑宸?#28003;?#37510;傚墿浣欑殑140涓囧?#21975;緟涓氳呮病鏈?#37720;诲鎵?#23480;?#28003;滅殑鍘?#37733;?#37714;?#37828;?#29831;?#28641;傚氨瀛垨瀹跺涵璐?#28000;?#37510;?璇?#29785;佽A-16)

鏈烘?#21230;皟鏌?#37825;版嵁
    鍏儴?#27537;闈?#37712;?#28051;氬氨涓氳?#37734;?鏈?#28000;藉?#28858;暱?#31777;28.8涓?#37510;傚湪鍓?2涓?#37832;?#28051;?#38171;屽钩鍧囨瘡涓?#37832;堝?#28858;暱19.9涓?#28051;?#23480;?#28003;滃矖浣?#37510;傚氨涓氬?#28858;暱鍦?鏈?#38322;屽?#26944;埌?#31777;鎵?#28598;?#38171;岃繖寰?#37929;?#27996;?#28051;?#28051;?#28051;庡晢涓?#37832;嶅姟涓?#38171;?#38342;跺敭涓?#38171;岄?#24880;涓氬拰?#32531;绛?#28051;氱殑灏?#28051;氬?#28858;暱銆?璇?#29785;佽B-1)
    涓?#28051;?#28051;庡晢涓?#37832;嶅姟涓?#37734;?鏈?#28000;藉鍔犱簡7.5涓?#28051;?#28751;?#28051;氬矖浣?#37510;傚湪鍓?2涓?#37832;堝唴锛岃琛?#28051;氭瘡涓?#37832;?#28028;氬钩鍧囧鍔?.5涓?#28051;?#28751;?#28051;?#37510;傚湪鏈?#37832;?#38171;岃琛?#28051;氱殑灏?#28051;氬?#28858;暱浠嶅湪缁?#32513;?#38171;屽叾涓?#28051;?#37827;?#26440;呭姪鏈嶅姟涓?2.4+涓?锛屽叕鍙?#28051;?#28028;佷笟绠悊(1.2+涓?锛岃绠?#37832;?#32495;荤粺璁?#29825;強鍏剁浉鍏?#37832;嶅姟涓?0.9+涓?銆?/span>
    闆跺敭涓氱殑灏?#28051;氬矖浣嶅湪鏈?#37832;堝鍔犱簡3.5涓?#28051;?#37510;傚湪杩?#37720;荤殑12涓?#37832;?#28051;?#38171;岃琛?#28051;氱殑灏?#28051;氬凡缁忓鍔犱簡32.7涓?#37510;傚湪闆跺敭涓?#28051;?#38171;?#37832;?#37832;堢殑宸?#28003;滃矖浣嶅?#28858;?#22446;彂鐢?#37734;?#26967;?#26891;熷?#21975;簵(0.9+涓?锛?#37827;?#37922;?#37927;捐揣鍟嗗簵(0.8+涓?锛屾苯杞?#28051;?#38342;堕儴浠跺晢(0.6+涓?鍜岄潪瀹炰綋鍟嗗簵(0.4+涓?銆?#37922;?#28699;?#28051;?#37922;?#37731;?#37727;嗗簵鍦?#37832;?#37832;?#37714;?#28051;?#28598;变簡1.1涓?#23480;?#28003;滃矖浣?#37510;?#37813;瑰彂涓?#37734;?#26473;?#37720;讳竴涓?#37832;堝?#21613;鍔犱簡1.6涓?#28051;?#23480;?#28003;滃矖浣?#38171;岃?#28729;?#37734;?#26473;?#37720;讳竴骞?#37712;呭鍔犱簡12.6涓?#28051;?#23480;?#28003;滃矖浣?#37510;?/span>
    椁愰涓?#37734;?#37832;?#37832;堝鍔犵殑灏?#28051;氭暟(3.3+涓?鍒?#28051;?#26473;?#37720;?2涓?#37832;堢殑骞?#37735;囧?#28858;暱(2.8涓?鏈?澶?#27996;?#37722;?#28051;姘?#39582;?#32510;?#28051;?#37510;?/span>
    ?#32531;绛戣涓氱殑灏?#28051;?#37734;?鏈?#28000;藉鍔犱簡3.2涓?#28051;?#38171;岃繖浜?#23480;?#28003;滃?#28858;暱鍒嗗埆涓?#38322;嶅瀷鍜屾皯鐢?#23480;?#32459;嬪缓绛?#28051;?1.1+涓?鍜屼綇瀹呭缓绛?#28051;?0.7+涓?銆傚湪杩?#37720;讳竴骞?#28051;?#38171;屽缓绛?#28051;氬凡缁忓鍔犱簡18.9涓?#28051;?#23480;?#28003;滃矖浣?#38171;岃?#28051;?#37711;?#28051;?#37713;?#28052;?#37733;?#37714;嗕箣涓夌殑澧為暱閮藉彂鐢?#37734;?#26473;?#37720;?涓?#37832;?#28051;?#37510;?/span>
    鍖荤枟琛?#28051;?#37832;?#37832;堝鍔犱簡1.9涓?#28051;?#23424;?#28003;?#38171;?#28051;?#26473;?#37720;?2涓?#37832;堢殑骞?#37735;囧?#28858;暱(1.7涓?鏈?姘?#39582;崇浉鎸佸钩銆傚寘?#23277;浼氬憳鍗?#28028;?#38171;?#28051;?#27996;?#37832;嶅姟涓?#28051;庢琛?#28051;?#32475;?#37711;?#28729;?#37832;嶅姟鎬?#29723;?#28051;氱殑灏?#28051;?#37734;?#26473;?#28051;?#37832;堝?#21613;鍔犱簡15000涓?#37510;?/span>
    閲囩熆涓?#37734;?鏈?#28000;藉鍔犱簡10000涓?#23480;?#28003;滃矖浣?#38171;?#28598;儴鍒嗙殑澧為暱閮?#37828;?#37734;?#38333;堝閲囩?#36328;殑鏀?#37816;佹?#35826;姩涓?7000+)銆?/span>
    鍏?#28729;?#28051;?#29781;佽涓?#38171;屽寘?#23277;鍒?#38315;?#28051;?#38171;岃繍杈?#28051;?#28000;撳偍涓?#38171;屼俊?#20229;鏈嶅姟涓?#38171;岄?#25123;瀺鏈嶅姟锛?#28000;強鏀垮鏈嶅姟涓?#38171;岀殑灏?#28051;?#37734;?#26473;?#28051;?#37832;堝?#21613;皯鏈夊彉?#23538;銆?/span>
    鍏儴绉佽惀闈?#37712;?#28051;氬氨涓氳呯殑骞?#37735;?#37723;?#23480;?#28003;?#37827;?#38338;存病鏈夊彉?#23538;锛?#28000;?#28051;?4.5灏?#37827;?#37510;傚埗閫?#28051;氱殑鍛?#23480;?#28003;?#37827;?#38338;?#37713;?#28751;戜簡0.2涓?#28751;?#37827;?#38171;岃?#24807;埌40.8涓?#28751;?#37827;?#37510;傚伐鍘傜殑鍔犵彮鏃?#38338;存病鏈夊彉?#23538;锛?#28000;?#28051;?.5涓?#28751;?#37827;?#37510;?#32457;佽惀闈?#37712;?#28051;氬氨涓氳?#28051;殑鐢?#27996;?#28051;?#38344;?#32480;悊闃跺?#20634;憳宸殑骞?#37735;?#37723;?#23480;?#28003;?#37827;?#38338;?#28052;熸病鏈夊彂鐢熷彉?#23538;锛屼?#28616;劧涓?3.7涓?#28751;?#37827;?#37510;?璇?#29785;B-2鍜孊-7)
    鏈?#37832;?#38171;?#32457;佽惀闈?#37712;?#28051;氬氨涓氳呯殑骞?#37735;?#37827;惰柂浠嶇劧涓?4.31缇庡厓銆傚湪杩?#37720;?2涓?#37832;?#28051;?#38171;屽钩鍧?#37827;惰柂宸?#32513;忓?#28858;暱?#31777;1.9%銆?#32457;佽惀?#27537;鐢?#27996;?#28051;?#38344;?#32480;悊闃跺?#20634;憳宸殑骞?#37735;?#37827;惰柂鍒欏?#28858;暱?#31777;3%锛岃?#24807;埌20.50缇庡厓銆?璇?#29785;佽B-3鍜孊-8)
    浜?#37832;?#28000;界殑鍏儴?#27537;闈?#37712;?#28051;氬氨涓氬矖浣?#28000;?9.7+涓?#28103;?#23005;?#28051;?2.2+涓?#38171;岃屽浜?#28051;?#37832;?#28000;界殑杩?#28051;鏁版嵁锛屽垯鏄?#28000;?9.2+涓?#28103;?#23005;?#28051;?0.3+涓?#37510;?#37837;规嵁杩?#27996;?#28103;?#23005;?#38171;?#27996;?#37832;?#28000;?#28051;?#28051;?#37832;?#28000;界殑灏?#28051;氬?#28858;暱?#30254;?#31651;鍓?#37814;?#37723;婄殑瑕侀?#27194;嚭3.6涓?#28051;?#37510;?br />


]]>Java 8?#27537;璇?#29815;?#24393;?#23538;(璇?http://www.8634070.com/jiangshachina/archive/2014/04/19/412695.htmlJohn JiangJohn JiangSat, 19 Apr 2014 15:48:00 GMThttp://www.8634070.com/jiangshachina/archive/2014/04/19/412695.htmlhttp://www.8634070.com/jiangshachina/comments/412695.htmlhttp://www.8634070.com/jiangshachina/archive/2014/04/19/412695.html#Feedback0http://www.8634070.com/jiangshachina/comments/commentRss/412695.htmlhttp://www.8634070.com/jiangshachina/services/trackbacks/412695.html
Java 8?#27537;璇?#29815;?#24393;?#23538;
--?#24714;瑙Lambda琛?#26440;?#23534;?#37724;屽彉?#23538;?#27537;鎺?#37721;被鏄?#28641;備綍?#23047;Java 8鎴?#28051;?#37826;扮殑璇?#29815;
鏈?#37826;?#37828;疘BM developerWorks涓殑涓?#30226;浠?#32513;岼ava 8鍏?#38335;?#37826;?#37911;?#37804;殑
鏂囩珷锛?#28729;?#28051;?#29781;佸叧娉Lambda琛?#26440;?#23534;?#37724;?#37824;?#26473;涚殑鎺?#37721;?#37510;?2014.04.19鏈鍚庢洿鏂?

    Java 8?#23512;鍚簡涓?#31885;?#22136;瑕佺殑鏂扮殑璇?#29815;鐗?#37804;?#38171;屼娇浣?#38003;藉?#29112;洿鏂?#28186;?#37734;?#37835;?#38315;犵?#23338;簭銆侺ambda琛?#26440;?#28051;?#37712;呰?#26050;殑浠爜鍧?#28729;氫箟?#31777;涓绉?#37826;扮殑璇?#23049;?#38171;?#32513;?#27996;?#28003;?#28051;庡尶鍚嶅?#21620;儴绫荤浉鍚岀殑鐏垫椿鎬?#38171;屼?#21975;張娌?#37832;?#38317;?#28052;?#28598;氭鏉?#28000;爜銆?#37818;?#37721;殑鏀瑰彉?#23047;寰?#38003;藉涓?#23480;?#37832;?#37818;?#37721;?#37716;?#37711;?#37826;扮殑鐗?#37804;?#38171;岃?#28051;嶅繀鎵?#37934;寸幇鏈?#28000;?#37934;佺殑鍏?#28729;?#37804;?#37510;備簡瑙?#26473;?#27996;?#29831;?#29815;?#24393;?#23538;鏄?#37804;庢牱涓璧?#23480;?#28003;滅殑锛岃闃?#29831;?#37832;?#32495;诲垪鍙?#28051;?#30226;鏂囩珷"Java 8骞跺彂?#29056;纭"锛屽彲浠?#37930;嬪埌濡備綍鍦Java 8娴佷腑?#23047;鐢Lambda銆?/span>
    Java 8?#27537;鏈澶?#37824;瑰彉灏?#37828;?#28583;?#37716;犱簡瀵筁ambda琛?#26440;?#23534;忕殑鏀寔銆侺ambda琛?#26440;?#23534;?#28051;绉?#38315;?#26473;囧紩鐢?#26473;?#29723;?#28028;?#38315;掔殑浠爜鍧?#37510;傚?#20906;?#35763;技浜庢煇浜涘叾瀹?#29831;?#29815;?#27537;闂寘锛?#28000;爜瀹炵幇?#31777;涓涓?#37716;?#38003;?#38171;屽彲浠?#28028;?#37711;?#28051;涓垨澶?#28051;?#37721;傛暟锛岃?#27194;彲浠?#26473;?#37733;?#28051;涓?#32513;?#37835;滃?#37510;?#38338;寘琚?#28729;氫箟鍦?#28051;涓?#28051;?#28051;?#37826;?#28051;?#38171;?#28729;冨彲浠?#29825;?#38338;?鍦Lambda涓?#37828;?#37721;?#29831;?#29825;?#38338;?涓?#28051;?#37826;?#28051;殑鍊?#37510;?/span>
    濡傛灉浣?#28051;嶇?#29112;倝闂寘锛?#28052;?#28051;嶅繀鎷呭績銆侸ava 8?#27537;Lambda琛?#26440;?#23534;?#37828;?#37713;?#28052;庢瘡涓狫ava寮?#24386;鑰?#38318;界?#29112;?#22796;殑鍖?#37722;嶅?#21620;儴绫荤殑涓涓?#26946;樻晥鐗?#29785;勮寖銆傚?#20635;灉浣?#37721;?#37807;?#37734;?#28051;涓?#28003;嶇疆瀹炵幇涓涓?#37818;?#37721;?#38171;屾垨鏄?#37714;涘缓涓涓熀绫荤殑瀛愮被鏃?#38171;屽尶鍚嶅?#21620;儴绫讳?#28888;鎻?#28186;涗簡涓绉嶅唴鑱?#28729;炵幇銆侺ambda琛?#26440;?#23534;?#28052;?#37922;?#27996;庣浉鍚岀殑鏂瑰紡锛屼絾鏄?#28729;冧娇鐢?#28051;绉嶇缉鐣殑璇?#23049;?#38171;屼娇寰?#26473;?#27996;?#28729;炵幇?#30254;涓涓爣鍑嗙殑鍐呴儴绫诲畾?#31647;?#27967;涓虹畝娲?#37510;?/span>
    鍦?#37832;?#37826;?#28051;?#38171;屼?#29362;皢鐪嬪埌濡備綍鍦?#28051;?#37722;岀殑鍦烘櫙涓嬩娇鐢Lambda琛?#26440;?#23534;?#38171;屽苟涓?#28003;?#28028;氬?#22476;涓嶫ava鎺?#37721;?#28729;氫箟?#27977;鍏崇殑鎵睍銆傚湪鏈?#37826;囩珷?#27537;濮?#28641;圭瘒JVM骞跺彂绯诲垪?#27537;"Java 8骞跺彂?#29056;纭"涓鏂?#28051;?#38171;屽彲浠?#37930;嬪埌?#27967;澶氫娇鐢Lambda琛?#26440;?#23534;忕殑渚嬪瓙锛屽寘?#23277;鍦Java 8娴佺壒鎬?#28051;殑?#31794;鐢?#37510;?/span>

杩涘叆Lambda
    Lambda琛?#26440;?#23534;?#28751;?#37828;疛ava 8鎵绉扮殑鍑芥暟鎺?#37721;殑瀹炵幇锛?#28051;涓?#37818;?#37721;?#37721;?#28729;氫箟涓涓?#37814;借薄鏂规硶銆傚彧瀹氫箟涓涓?#37814;借薄鏂规?#26336;殑闄?#37714;舵槸闈?#29999;搁噸瑕佺殑锛?#37733;?#28051;篖ambda琛?#26440;?#23534;忕殑璇?#23049;?#39582;?#28051;?#28028;氫娇鐢?#37826;规硶鍚?#37510;傜浉鍙?#38171;岃琛?#26440;?#23534;?#28028;氫娇鐢?#37716;?#37804;佺?#35826;瀷璇嗗埆(鍖归?#23941;?#20635;暟鍜岃繑鍥?#32491;诲瀷锛屽緢澶氬姩鎬?#29831;?#29815;閮?#26473;?#28052;堝仛)鍘讳繚璇佹彁渚涚殑Lambda鑳藉涓?#37832;?#37832;涚殑鎺?#37721;?#37826;规硶鍏?#28729;?#37510;?/span>
    鍦竻鍗?鎵绀虹殑绠鍗?#28186;嬪瓙涓?#38171;?#28051;涓狶ambda琛?#26440;?#23534;忚鐢潵瀵筃ame瀹?#28186;?#26473;?#29723;?#37818;?#25652;?#37510;俶ain()鏂规硶涓殑绗?#28051;涓?#28000;爜鍧椾娇鐢?#28051;涓?#37718;?#37722;嶅?#21620;儴?#34987;鍘诲?#28853;幇Comparator<Name>鎺?#37721;?#38171;岀浜?#28051;?#29831;?#37721;?#37735;楀垯?#23047;鐢Lambda琛?#26440;?#23534;?#37510;?/span>
?#31483;鍗?. ?#30254;杈Lambda琛?#26440;?#23534;?#28051;庡尶鍚嶅?#21620;儴?#34987;
public class Name {
    
public final String firstName;
    
public final String lastName;

    
public Name(String first, String last) {
        firstName 
= first;
        lastName 
= last;
    }

    
// only needed for chained comparator
    public String getFirstName() {
        
return firstName;
    }

    
// only needed for chained comparator
    public String getLastName() {
        
return lastName;
    }

    
// only needed for direct comparator (not for chained comparator)
    public int compareTo(Name other) {
        
int diff = lastName.compareTo(other.lastName);
        
if (diff == 0) {
            diff 
= firstName.compareTo(other.firstName);
        }
        
return diff;
    }
    
}

public class NameSort {
    
    
private static final Name[] NAMES = new Name[] {
        
new Name("Sally""Smith"),
        
    };
    
    
private static void printNames(String caption, Name[] names) {
        
    }

    
public static void main(String[] args) {

        
// sort array using anonymous inner class
        Name[] copy = Arrays.copyOf(NAMES, NAMES.length);
        Arrays.sort(copy, 
new Comparator<Name>() {
            @Override
            
public int compare(Name a, Name b) {
                
return a.compareTo(b);
            }
        });
        printNames(
"Names sorted with anonymous inner class:", copy);

        
// sort array using lambda expression
        copy = Arrays.copyOf(NAMES, NAMES.length);
        Arrays.sort(copy, (a, b) 
-> a.compareTo(b));
        printNames(
"Names sorted with lambda expression:", copy);
        
    }
}
    鍦竻鍗?涓?#38171;孡ambda琚?#37922;?#27996;庡彇浠?#37718;?#37722;嶅?#21620;儴?#34987;銆傝繖绉嶅尶鍚嶅?#21620;儴绫诲湪?#31794;鐢?#28051;?#38344;?#29999;告櫘閬?#38171;?#37813;浠Lambda琛?#26440;?#23534;?#23536;堝揩灏?#29863;?#23536;椾簡Java8绋嬪簭鍛?#28000;殑闈掔潗銆?鍦?#37832;?#28186;?#28051;?#38171;?#37722;?#37827;朵娇鐢?#37718;?#37722;嶅?#21620;儴绫诲拰Lambda琛?#26440;?#23534;?#37720;诲?#28853;幇Name绫讳腑?#27537;涓涓?#37826;规硶锛?#28000;?#37826;?#28186;垮杩?#28051;?#32457;?#37826;规硶杩?#29723;屾瘮杈?#37510;傚?#20635;灉鍦Lambda涓?#28725;compareTo()鏂规硶杩?#29723;屽唴鑱旂殑璇?#38171;岃琛?#26440;?#23534;忓皢浼氭洿鍔?#32480;娲?#37510;?

?#29219;鍑嗙殑鍑芥暟寮?#37818;?#37721;?/span>
    涓轰簡?#31794;鐢Lambda锛?#37826;扮殑?#23512;java.util.function涓?#28729;氫箟?#31777;骞?#23049;涚殑鍑芥暟寮?#37818;?#37721;?#37510;傚畠浠?#29722;?#35120;?#32513;?#28051;?#28641;?#28051;嬪嚑涓?#32491;诲埆锛?/span>
    鍑芥暟锛氫娇鐢?#28051;涓?#37721;傛暟锛屽熀浜庡?#20635;?#25198;殑鍊?#26473;?#37733;?#32513;撴灉銆?/span>
    璋?#29831;?#38171;氫娇鐢?#28051;涓?#37721;傛暟锛屽熀浜庡?#20635;?#25198;殑鍊?#26473;?#37733;?#29999;?#28751;?#32513;撴灉銆?/span>
    鍙屽?#33445;暟锛氫娇鐢?#28051;?#28051;?#37721;傛暟锛屽熀浜庡?#20635;?#25198;殑鍊?#26473;?#37733;?#32513;撴灉銆?/span>
    渚涘簲鍣?#38171;?#28051;嶄娇鐢?#28000;讳綍鍙傛暟锛屼絾浼?#26473;?#37733;?#32513;撴灉銆?/span>
    娑堣垂鑰?#38171;氫娇鐢?#28051;涓?#37721;傛暟锛屼絾涓?#26473;?#37733;?#28000;讳綍缁撴灉銆?/span>
澶氭暟绫诲埆閮藉寘鍚?#28598;?#28051;?#28051;?#37722;岀殑?#24393;?#32139;锛?#28000;?#28186;?#38003;藉浣?#37922;?#27996;庡熀鏈?#37825;版嵁绫诲?#23340;殑鍙傛暟鍜岃繑鍥?#37706;?#37510;傝澶?#37818;?#37721;?#37813;瀹氫箟?#27537;鏂规硶閮藉彲琚?#37922;?#27996;庣粍鍚堝璞?#38171;?#28641;傛竻鍗?鎵绀?#38171;?/span>
?#31483;鍗?. ?#31885;鍚堣皳璇?/span>
// use predicate composition to remove matching names
List<Name> list = new ArrayList<>();
for (Name name : NAMES) {
    list.add(name);
}
Predicate
<Name> pred1 = name -> "Sally".equals(name.firstName);
Predicate
<Name> pred2 = name -> "Queue".equals(name.lastName);
list.removeIf(pred1.or(pred2));
printNames(
"Names filtered by predicate:", list.toArray(new Name[list.size()]));
    ?#31483;鍗?瀹氫箟?#31777;涓瀵筆redicate<Name>?#24393;閲?#38171;?#28051;涓?#37922;?#27996;庡?#24402;厤鍚?#28051;篠ally?#27537;鍚嶅瓧锛屽彟涓涓?#37922;?#27996;庡?#24402;?#23941;涓篞ueue?#27537;鍚嶅瓧銆傝皟鐢?#37826;规硶pred1.or(pred2)浼氭瀯閫?#28051;涓粍鍚堣皳璇?#38171;岃璋?#29831;?#37711;?#37722;庝娇鐢簡涓?#28051;?#29835;?#29831;?#38171;屽綋瀹?#28000;?#28051;殑浠讳綍涓涓?#26473;?#37733;瀟rue鏃?#38171;岃繖涓粍鍚堣皳璇?#28751;卞皢杩?#37733;瀟rue(杩?#28751;辩浉?#32139;浜庢棭鏈烰ava涓殑閫?#26440;?#37823;?#28003;?#32471;||)銆侺ist.removeIf()鏂规硶灏卞簲鐢?#26473;?#28051;粍鍚堣皳璇?#37720;诲垹闄垪琛?#28051;殑鍖归厤鍚嶅瓧銆?/span>
    Java 8瀹氫箟?#31777;璁?#28598;?#37832;?#37922;殑java.util.function?#23512;涓?#37818;?#37721;殑?#31885;鍚?#37818;?#37721;?#38171;屼?#21978;繖绉嶇粍鍚堝苟涓?#38318;?#37828;?#28051;鏍风殑銆?#37813;鏈夌殑璋?#29831;殑?#24393;?#32139;(DoublePredicate锛孖ntPredicate锛孡ongPredicate鍜孭redicate<T>)閮?#28729;氫箟?#31777;?#27977;鍚岀殑?#31885;鍚?#28051;庝慨鏀?#37826;规硶锛歛nd()锛宯egate()鍜宱r()銆備絾鏄疐unction<T>?#27537;?#29056;鏈?#37825;版嵁绫诲?#23338;彉?#32139;灏辨病鏈?#28729;氫箟浠讳?#26336;粍鍚?#28051;庝慨鏀?#37826;规硶銆傚?#20635;灉浣犳嫢鏈変娇鐢?#37713;芥暟寮?#32514;栫璇?#29815;?#27537;缁?#26976;?#38171;?#38317;?#28052;?#28003;犲彲鑳藉氨?#24386;浼氬彂?#24135;杩?#27996;?#28051;?#37722;屼箣澶勫拰濂?#37804;殑蹇?#37923;?#37510;?/span>

鏀瑰彉鎺?#37721;?/span>
    鍦Java 8涓?#38171;?#37818;?#37721;?濡傛竻鍗??#27537;Comparator)?#27537;缁?#37835;勫凡?#24386;鐢熶簡鏀瑰彉锛岄儴鍒?#37720;?#37733;?#37828;?#28051;轰簡璁Lambda?#27967;濂?#37922;?#37510;侸ava 8?#31651;鍓嶇殑鎺?#37721;?#37721;?#38003;?#28729;氫箟甯搁噺锛?#28000;強蹇呴琚?#28729;炵?#25198;殑鎶借薄鏂规硶銆傝孞ava 8涓殑鎺?#37721;?#37714;?#38003;藉瀹氫箟闈?#37804;佷笌榛?#29825;?#37826;规硶銆?#37818;?#37721;?#28051;殑闈?#37804;?#37826;规硶涓庢?#20511;薄绫讳腑?#27537;闈?#37804;?#37826;规硶鏄?#28729;屽叏涓鏍风殑銆傞粯璁?#37826;规硶鍒欐洿鍍?#37827;?#23534;忕殑鎺?#37721;?#37826;规硶锛屼?#21976;彁渚涗簡璇?#37826;规?#26336;殑涓涓?#28729;炵幇銆?#29831;?#37826;规硶瀹炵幇?#24434;鐢?#27996;?#29831;?#37818;?#37721;殑瀹炵?#25198;被锛岄櫎闈?#28729;?#29722;?#28729;炵?#25198;被瑕?#37929;?#37818;変簡銆?/span>
    榛?#29825;?#37826;规?#26336;殑涓涓噸瑕佺壒鎬?#28751;?#37828;?#28729;冨彲浠?#29722;?#37716;?#37711;埌宸?#37832;?#37818;?#37721;?#28051;?#38171;屼?#21975;張涓?#28028;氱牬鍧?#23480;蹭娇鐢簡杩?#27996;?#37818;?#37721;殑浠?#37934;佺殑鍏?#28729;?#37804;?闄?#38344;?#23480;?#37832;?#28000;?#37934;佹伆宸娇鐢簡?#27977;鍚?#37722;嶅瓧?#27537;鏂规硶锛屽苟涓?#37711;剁洰?#27537;涓?#27035;?#29825;?#37826;规硶涓?#37722;?銆傝繖鏄?#28051;涓?#38344;?#29999;稿己澶殑鍔?#38003;?#38171;孞ava 8?#27537;璁?#29825;?#38000;?#28000;?#37714;?#37922;?#26473;?#28051;鐗?#37804;?#28051;?#29825;?#28598;氬凡鏈塉ava绫诲?#25779;姞鍏簡瀵筁ambda琛?#26440;?#23534;忕殑鏀寔銆傛竻鍗?灏卞睍绀轰簡杩?#37837;风殑涓涓?#28186;嬪瓙锛?#28729;?#37828;竻鍗?涓?#28725;?#37722;嶅瓧杩?#29723;?#37818;?#25652;忕殑绗?#28051;?#32457;?#28729;炵幇鏂瑰紡銆?/span>
?#31483;鍗?. 閿?鎻?#37721;栨瘮杈?#37731;摼
// sort array using key-extractor lambdas
copy = Arrays.copyOf(NAMES, NAMES.length);
Comparator
<Name> comp = Comparator.comparing(name -> name.lastName);
comp 
= comp.thenComparing(name -> name.firstName);
Arrays.sort(copy, comp);
printNames(
"Names sorted with key extractor comparator:", copy);
    ?#31483;鍗?棣栧?#22557;睍绀轰簡濡備綍?#23047;鐢?#37826;扮殑Comparator.comparing()闈?#37804;?#37826;规硶鍘诲?#28056;缓涓涓熀浜庨敭-鎻?#37721;?Key-Extraction) Lambda?#27537;?#30254;杈?#37731;?浠庢妧鏈?#28051;婄湅锛岄敭-鎻?#37721;朙ambda灏?#37828;java.util.function.Function<T,R>鎺?#37721;殑涓涓?#28729;?#28186;?#38171;?#28729;?#26473;?#37733;炵殑?#30254;杈?#37731;殑绫诲瀷閫?#37922;?#27996;?#32491;诲瀷T锛岃屾彁鍙栫殑閿殑绫诲瀷R鍒欒瀹炵幇Comparable鎺?#37721;?銆傚畠杩樺睍绀轰簡濡備綍?#23047;鐢?#37826;扮殑Comparator.thenComparing()榛?#29825;?#37826;规硶鍘荤粍鍚堜娇鐢瘮杈?#37731;?#38171;屾竻鍗?灏?#26473;?#37733;炰簡涓涓?#37826;扮殑?#30254;杈?#37731;?#38171;?#28729;?#28028;氬厛鎸?#28654;?#37818;?#25652;?#38171;屽?#23942;寜鍚?#37818;?#25652;?#37510;?/span>
    浣?#28052;?#29825;?#37832;?#37832;?#38003;藉?#29111;?#35268;瘮杈?#37731;?#26473;?#29723;屽唴鑱?#38171;?#28641;?#38171;?/span>
Comparator<Name> comp = Comparator.comparing(name -> name.lastName)
    .thenComparing(name 
-> name.firstName);
浣?#28051;嶅垢鍦?#37828;?#38171;孞ava 8?#27537;绫诲瀷鎺?#28725;?#28051;嶅厑璁?#26473;?#28052;堝仛銆?#28051;?#28000;?#38344;?#37804;?#37826;规硶涓?#23536;楀埌鏈?#37832;涚?#35826;?#23340;殑缁撴灉锛屼?#29371;渶瑕佷负缂?#29831;?#37731;?#37819;?#28186;涙洿澶氱殑淇伅锛屽彲浠娇鐢?#28641;?#28051;?#28000;讳綍涓绉嶅舰寮?#38171;?/span>
Comparator<Name> com1 = Comparator.comparing((Name name1) -> name1.lastName)
    .thenComparing(name2 
-> name2.firstName);
Comparator
<Name> com2 = Comparator.<Name,String>comparing(name1 -> name1.lastName)
    .thenComparing(name2 
-> name2.firstName);
    绗?#28051;绉?#37826;瑰紡鍦Lambda琛?#26440;?#23534;?#28051;?#37716;?#37711;?#37721;傛?#25198;殑绫诲瀷锛?Name name1) -> name1.lastName銆?#37832;変簡杩?#28051;?#26440;呭姪淇伅锛岀紪璇?#37813;?#38003;界煡?#20158;涓?#38344;?#28729;?#29831;?#37707;氫簺浠涔?#37510;傜浜?#32457;?#37826;瑰紡鏄?#37723;?#29831;夌紪璇?#37731;?#29781;佷紶閫?#32513;Function鎺?#37721;?鍦?#23005;?#28598;?#38171;岃鎺?#37721;?#38315;?#26473;Lambda琛?#26440;?#23534;?#28729;炵幇)涓璫omparing()鏂规?#26336;殑娉涘?#23338;彉閲廡鍜孯?#27537;绫诲瀷銆?/span>
    鑳藉鏂?#28186;?#37734;?#37835;勫?#28888;瘮杈?#37731;?#28000;強?#30254;杈?#37731;摼鏄疛ava 8涓?#23536;?#37832;?#37922;殑鐗?#37804;?#38171;屼絾瀹冪殑浠?#28000;?#37828;?#28583;?#37716;犱簡澶?#37833;傚害銆侸ava 7?#27537;Comparator鎺?#37721;?#28729;氫箟?#31777;涓?#28051;?#37826;规硶(compare()鏂规硶锛?#28000;強閬嶅竷浜庢瘡涓?#28725;?#29854;?#28051;殑equals()鏂规硶)銆傝?#37734;Java 8涓?#38171;岃鎺?#37721;?#37714;?#28729;氫箟?#31777;18涓?#37826;规硶(闄簡鍘?#37832;夌殑2涓?#37826;规硶锛岃繕鏂?#37716;?#37711;簡9涓?#38344;?#37804;?#37826;规硶鍜?涓?#27035;?#29825;?#37826;规硶)銆備綘灏嗗彂?#24135;锛?#28051;轰簡鑳藉?#29110;娇鐢Lambda鑰?#38315;?#37812;愮殑杩?#32457;?#37818;?#37721;啫鑳浼?#38322;嶇幇浜庣浉?#32139;涓?#20788;鍒咼ava?#29219;鍑?#32491;诲簱涓?#37510;?/span>

鍍廘ambda閭?#37837;蜂娇鐢?#23480;?#37832;?#37826;规硶
    濡傛灉涓涓?#28699;?#37734;殑鏂规硶宸?#32513;?#28729;炵幇?#31777;浣犵殑?#28214;?#30512;锛屼?#29362;彲浠洿鎺娇鐢?#28051;涓?#37826;规?#26334;紩鐢?#28725;?#28729;?#26473;?#29723;?#28028;?#38315;?#37510;傛竻鍗??#30541;绀轰簡杩?#32457;?#37826;规硶銆?/span>
?#31483;鍗?. 瀵瑰凡鏈?#37826;规?#26330;娇鐢Lambda

// sort array using existing methods as lambdas
copy = Arrays.copyOf(NAMES, NAMES.length);
comp 
= Comparator.comparing(Name::getLastName).thenComparing(Name::getFirstName);
Arrays.sort(copy, comp);
printNames(
"Names sorted with existing methods as lambdas:", copy);
    ?#31483;鍗?鍋氱潃涓庢竻鍗??#27977;鍚岀殑浜嬫儏锛屼絾瀹冧娇鐢簡宸?#37832;?#37826;规硶銆備娇鐢Java 8?#27537;褰?#28051;?绫诲悕:鏂规硶鍚??#27537;鏂规?#26334;紩鐢?#29831;?#23049;?#38171;屼?#29362;彲浠娇鐢?#28000;绘剰鏂规硶锛屽氨鍍廘ambda琛?#26440;?#23534;?#38317;?#37837;?#37510;傚?#33333;晥鏋滃氨涓?#28003;?#28729;氫箟涓涓狶ambda琛?#26440;?#23534;?#37720;昏皟鐢?#29831;?#37826;规硶涓鏍?#37510;傚绫荤殑闈?#37804;?#37826;规硶锛?#37911;?#28729;氬璞垨Lambda杈撳叆绫诲?#23340;殑瀹?#28186;?#37826;规硶(濡傚湪?#31483;鍗?涓?#38171;実etFirstName()鍜実etLastName()鏂规硶灏?#37828;Name绫荤殑瀹?#28186;?#37826;规硶)锛?#28000;強?#34987;鏋?#38315;?#37731;?#38171;?#38318;藉彲浠娇鐢?#37826;规?#26334;紩鐢?#37510;?/span>
    鏂规?#26334;紩鐢?#28051;?#28000;?#37826;?#28186;?#38171;?#37733;?#28051;?#28729;?#28000;瘮?#23047;鐢Lambda琛?#26440;?#23534;忓彲鑳芥洿楂樻晥锛岃?#28051;?#28051;虹紪璇?#37731;?#37819;?#28186;涗簡?#27967;濂界殑绫诲瀷淇伅(杩?#28052;熷氨鏄?#28051;?#28000;涔?#37734;?#28051;?#28051;鑺傜殑Lambda涓娇鐢?thenComparing()鏋?#38315;燙omparator浼氬?#34425;幇闂?#26864;?#38171;岃?#37734;竻鍗?鍗?#38003;芥甯?#23480;?#28003;?銆傚?#20635;灉鏃彲浠娇鐢?#28725;瑰凡鏈?#37826;规?#26336;殑鏂规?#26334;紩鐢?#38171;?#28052;熷彲浠娇鐢Lambda琛?#26440;?#23534;?#38171;岃?#34562;娇鐢?#37715;嶈?#37510;?/span>

鎹曡幏涓?#38344;?#37817;曡幏Lambda
    浣?#37734;?#37832;?#37826;?#28051;?#23480;?#29785;?#26473;囩殑Lambda琛?#26440;?#23534;?#38318;?#37828;?#38344;?#37817;曡?#39118;殑锛屾剰鍗?#38171;?#28729;?#28000;?#38318;?#37828;?#37814;?#28028;?#37711;殑鍊煎綋浣?#37818;?#37721;?#37826;规硶鍙傛暟?#23047;鐢殑绠鍗昄ambda琛?#26440;?#23534;?#37510;侸ava 8?#27537;鎹曡幏Lambda琛?#26440;?#23534;?#37714;?#37828;娇鐢?#28598;?#37733;寸幆澧?#28051;殑鍊?#37510;傛?#26337;幏Lambda绫讳技浜庢煇浜汮VM璇?#29815;(濡係cala)?#23047;鐢殑闂寘锛屼絾Java 8?#27537;瀹炵幇涓庝箣鏈?#37813;涓?#37722;?#38171;?#37733;?#28051;烘潵?#22172;鍦?#28598;?#37733;寸幆澧?#28051;殑鍊?#36423;呴?#35826;鏄?#28051;final銆備?#29111;氨鏄?#29831;?#38171;岃繖浜涘?#29781;佷?#22562;瀹?#28051;final(灏?#28641;?#37722;?#28000;?#37715;嶇殑Java鐗?#37832;?#28051;?#37922;?#37718;?#37722;嶅?#21620;儴?#34987;鎵?#32041;鐢殑鍊?锛岃?#20343;箞鍦?#28598;?#37733;寸幆澧?#28051;?#28051;?#28028;氳淇?#37824;?#37510;傝繖涓瑙勮寖閫?#37922;?#27996;嶭ambda琛?#26440;?#23534;?#37724;屽尶鍚嶅?#21620;儴?#34987;銆?#37832;?#28051;浜?#37826;规?#26334;彲浠?#32513;?#26473;?#28725;瑰肩殑final闄?#37714;?#37510;備緥濡?#38171;?#37734;Lambda涓?#28000;呬娇鐢?#37911;?#28729;氬彉閲忕殑?#32139;鍓嶅?#38171;屼?#29362;彲浠?#23075;诲姞涓涓?#37826;扮殑鏂规硶锛屾妸杩?#27996;涘?#28003;?#28051;?#37826;规硶鍙傛暟锛屽?#23941;皢鎹曡?#39118;殑鍊?浠?#37805;板綋?#27537;鎺?#37721;紩鐢?#26473;?#32457;嶅舰寮?杩?#37733;?#32513;橪ambda銆傚?#20635;灉鏈?#37832;?#28051;涓狶ambda鍘讳慨鏀瑰鍥寸幆澧?#28051;殑鍊?#38171;?#38317;?#28052;堝彲浠?#37922;?#28051;涓彲淇?#37824;圭殑?#23508;鏈?#37731;被(Holder)瀵?#26473;?#27996;涘?#26473;?#29723;屽寘瑁?#37510;?/span>
    ?#27977;?#30254;浜庢?#26337;幏Lambda锛屽彲浠洿楂樻晥鍦?#28598;勭悊闈?#37817;曡幏Lambda锛?#38317;?#37828;?#37733;?#28051;虹紪璇?#38003;藉?#29112;妸瀹?#37922;?#37812;?#28051;?#32491;讳腑?#27537;闈?#37804;?#37826;规硶锛岃岃繍琛?#37827;剁幆澧冨彲浠洿鎺?#37712;呰?#26050;殑璋?#37922;?#26473;?#27996;?#37826;规硶銆傛?#26337;幏Lambda涔?#29825;?#28003;庢晥涓浜?#38171;屼絾鍦浉鍚?#28051;?#28051;?#37826;囩幆澧?#28051;?#28729;?#38007;?#28751;戝彲浠?#29723;?#37916;扮殑鍜屽尶鍚嶅?#21620;儴绫讳竴鏍?#28610;?#37510;?/span>

骞?#37722;庣殑Lambda
    Lambda琛?#26440;?#23534;忕湅璧锋潵鍍?#37718;?#37722;嶅?#21620;儴?#34987;锛屼絾瀹?#28000;殑瀹炵幇鏂规硶涓?#37722;?#37510;侸ava?#27537;鍐呴儴?#34987;鏈?#23536;?#28598;氭瀯閫?#37731;?#38171;?#23011;?#28051;?#37712;呴儴?#34987;閮?#28028;?#37832;?#28051;涓?#28699;?#38010;傜?#20346;骇鍒殑?#23277;绔嬬被鏂?#28000;?#37510;傝繖灏?#28028;氫骇鐢熷閲忕殑閲嶅浠爜(澶儴鍒?#37828;?#37734;?#29999;搁噺姹?#28729;炰綋涓?锛岀?#35826;姞杞?#37827;?#28028;?#38315;?#37812;?#28598;?#38322;忕殑杩?#29723;?#37827;跺紑閿锛屽摢鎬?#37721;?#37832;?#28751;戦?#24533;殑浠?#37934;佷篃浼?#37832;?#28641;傛鍚庢灉銆?/span>
    Java 8娌?#37832;?#28051;篖ambda鐢?#37812;愮嫭绔嬬殑?#34987;鏂?#28000;?#38171;岃?#37828;娇鐢簡鍦Java 7涓紩鍏殑invokedynamic瀛?#38010;傜?#20345;寚浠?#37510;俰nvokedynamic浣?#37922;?#27996;?#28051;涓?#37722;?#37716;?#37826;规硶锛屽綋璇?#37826;规?#26336;涓娆?#29722;?#29835;?#37922;?#37827;?#28729;?#28028;?#26462;?#38000;?#37720;诲?#28056;缓Lambda琛?#26440;?#23534;忕殑瀹炵幇銆傜劧鍚?#38171;岃瀹炵幇浼氳杩?#37733;?#39582;?#29722;洿鎺?#29835;?#37922;?#37510;傝繖鏍?#28751;遍伩鍏嶄簡?#23277;绔嬬被鏂?#28000;?#29999;潵?#27537;绌?#38338;?#23534;閿锛?#28000;強鍔?#26462;?#32491;荤殑澶?#38322;忚繍琛?#37827;跺紑閿銆傜鍒?#37734;?#29831;?#38171;孡ambda鍔?#38003;界殑瀹炵幇琚?#28051;?#32513;欎簡鍚?#37716;?#32459;嬪簭銆傜洰鍓岼ava 8鐢?#37812;愮殑鍚?#37716;?#32459;嬪簭浼?#37734;?#26473;?#29723;?#37827;?#28051;篖ambda鍒涘缓涓涓?#37826;扮被锛屼絾鍦皢?#28533;浼氫娇鐢?#28051;?#37722;岀殑鏂规硶鍘诲?#28853;幇銆?/span>
    Java 8?#23047;鐢殑浼樺寲?#23047;寰?#38315;?#26473;invokedynamic?#23514;浠?#28729;炵?#25198;殑Lambda鍦?#28729;?#38340;?#28051;?#26473;?#29723;屾甯?#37510;傚?#27693;暟鍏?#28729;冪殑JVM璇?#29815;锛屽寘?#23277;Scala (2.10.x)锛?#38318;?#28028;?#28051;?#38338;寘?#23047;鐢?#32514;?#29831;?#37731;?#37922;?#37812;愮殑鍐呴儴?#34987;銆傚湪?#30370;?#28533;锛岃繖浜?#29831;?#29815;?#24434;鑳?#28028;?#26462;?#38000;屼娇鐢invokedynamic?#23514;浠?#38171;?#28000;?#28186;?#37714;?#37922;埌Java 8(?#24375;鍏?#37722;?#32513;?#37911;?#37832;??#27537;浼樺寲銆?/span>

Lambda?#27537;灞闄?/span>
    濡傚湪鏈?#37826;囧紑濮?#37827;舵垜鎵鎻愬埌?#27537;锛孡ambda琛?#26440;?#23534;?#37804;?#37828;煇浜涚壒娈?#37713;芥暟寮?#37818;?#37721;殑瀹炵幇銆備?#29362;彲浠?#28000;?#37814;Lambda?#32139;浣?#37818;?#37721;紩鐢?#37720;讳紶閫?#38171;岃屽浜庡叾瀹冪殑鎺?#37721;?#28729;炵幇锛屼綘涔熷彲浠?#37721;?#37828;?#37814;Lambda?#32139;浣?#26473;?#27996;涚壒瀹?#37818;?#37721;?#37720;讳娇鐢?#37510;傛竻鍗??#30541;绀轰簡杩?#32457;嶅眬闄?#37804;?#38171;?#37734;?#29831;?#32448;?#28186;嬩娇鐢簡涓瀵圭浉鍚岀殑(鍚?#32457;?#38340;?#28598;?鍑芥暟寮?#37818;?#37721;?#37510;侸ava 8缂?#29831;?#37818;?#37721;String::lenght?#28533;浣?#28051;?#26473;?#28051;?#28051;?#37818;?#37721;殑Lambda瀹炵幇銆備絾鏄?#38171;?#37734;?#28051;涓狶ambd琛?#26440;?#23534;忚瀹氫箟涓虹涓涓?#37818;?#37721;殑瀹?#28186;嬩箣鍚?#38171;?#28729;?#28051;?#38003;藉鐢?#27996;庣浜?#28051;?#37818;?#37721;殑瀹?#28186;?#37510;?/span>
?#31483;鍗?. Lambda?#27537;灞闄?/span>
private interface A {
    
public int valueA(String s);
}
private interface B {
    
public int valueB(String s);
}
public static void main(String[] args) {
    A a 
= String::length;
    B b 
= String::length;

    
// compiler error!
    
// b = a;

    
// ClassCastException at runtime!
    
// b = (B)a;

    
// works, using a method reference
    b = a::valueA;
    System.out.println(b.valueB(
"abc"));
}
    浠讳綍瀵笿ava鎺?#37721;?#22994;傚康鏈?#37813;?#31777;瑙殑浜?#38318;?#28051;?#28028;氬?#35268;竻鍗?涓殑绋嬪?#24532;?#29111;埌鎯?#29825;?#38171;?#37733;?#28051;?#38317;?#28751;?#37828;疛ava鎺?#37721;?#28051;?#27967;鎵鍋氱殑浜嬫儏(闄簡鏈鍚?#28051;?#20579;锛?#38317;?#37828;疛ava 8鏂板紩鍏殑鏂规?#26334;紩鐢?銆備絾鏄娇鐢?#37711;?#28729;?#37713;芥暟寮?#32514;栫璇?#29815;锛屼緥濡係cala锛岀殑寮?#24386;鑰?#28000;?#37714;?#28028;?#29825;?#28051;?#37818;?#37721;殑杩?#32457;?#38340;?#37714;舵槸涓嶈嚜鐒剁殑銆?/span>
    鍑芥暟寮?#32514;栫璇?#29815;鏄?#37922;?#37713;芥暟绫诲瀷锛岃?#28051;?#37828;?#37818;?#37721;?#38171;?#37720;诲畾?#31647;?#24393;閲?#37510;傚湪杩?#27996;涚?#26667;璇?#29815;涓?#28028;氬緢?#27352;閬嶇殑?#23047;鐢珮绾?#37713;芥暟锛氭妸鍑芥暟浣?#28051;?#37721;傛暟浼?#38315;?#32513;?#37711;?#28729;冪殑鍑芥暟锛屾垨鑰?#37814;?#37713;芥暟?#32139;浣滃?#37720;?#26473;?#37733;?#37510;傚叾缁?#37835;滃氨鏄?#28003;?#28028;氬?#26944;埌?#30254;Lambda?#27967;涓虹?#22443;?#33636;殑缂栫?#23342;?#24226;牸锛岃繖?#23512;?#23277;?#23047;鐢?#37713;芥暟鍘荤粍鍚?#37711;?#28729;?#37713;芥暟浠?#37835;勫缓璇?#37721;?#37735;楃殑鑳藉姏銆?#37733;?#28051;篔ava 8娌?#37832;?#28729;氫箟鍑芥暟绫诲瀷锛屼綘涓?#38003;戒娇鐢?#26473;?#32457;?#37826;规硶鍘荤粍鍚圠ambda琛?#26440;?#23534;?#37510;備?#29362;彲浠粍鍚?#37818;?#37721;?濡傛竻鍗?鎵绀?锛屼?#21975;彧鑳?#37828;?#28051;嶫ava 8涓?#23480;?#37712;欏?#30028;殑閭?#27996;?#37818;?#37721;浉鍏崇殑鐗?#28729;?#37818;?#37721;?#37510;?#28000;?#37734;?#37826;扮殑java.util.function?#23512;鍐?#38171;屽氨鐗?#23048;?#29825;?#28729;氫簡43涓?#37818;?#37721;?#37720;讳娇鐢Lambda銆傛妸瀹?#28000;?#37716;?#37711;埌鏁?#28000;?#37927;?#29825;殑宸?#37832;?#37818;?#37721;?#28051;?#38171;屼?#29362;皢鐪嬪埌杩?#32457;?#37826;规硶鍦粍鍚?#37818;?#37721;?#37827;舵?#37828;?#28028;?#37832;?#28051;?#38322;嶇殑闄?#37714;?#37510;?/span>
    ?#23047;鐢?#37818;?#37721;?#38000;?#28051;?#37828;?#37734;?#37722;慗ava涓紩鍏?#37713;芥暟绫诲瀷鏄?#28051;涓?#32494;?#28641;欑殑閫夋嫨銆傝繖鏍?#28751;?#37734;?#38339;?#23005;?#28725;笿ava绫诲簱杩?#29723;岄?#23941;鏀瑰姩?#27537;鍚?#37827;?#28052;?#38003;藉?#29111;?#29808;凡鏈夌?#35826;?#25778;娇鐢Lambda琛?#26440;?#23534;?#37510;傚?#20906;殑鍧?#28003;?#37922;?#28751;?#37828;?#28725;笿ava 8閫?#37812;愪簡鏋?#28598;殑闄?#37714;?#38171;?#28729;?#37721;?#38003;?#32457;?#28051;?鎺?#37721;?#32514;栫"?#22440;鏄?#32491;诲?#33445;暟寮?#32514;栫锛岃?#28051;?#37828;?#37930;熸?#27537;鍑芥暟寮?#32514;栫銆備絾渚?#38344;燡VM涓?#37711;?#28729;?#29831;?#29815;锛?#28052;熷寘?#23277;鍑芥暟寮?#29831;?#29815;锛岀殑浼樼偣锛岃繖浜?#38340;?#37714;跺苟涓嶅彲鎬?#37510;?/span>

缁?#29825;?/span>
    Lambda鏄疛ava璇?#29815;?#27537;鏈涓?#29781;佹墿?#30541;锛?#28028;寸潃瀹?#28000;殑鍏勫紵鏂?#37911;?#37804;?-鏂规?#26334;紩鐢?#38171;岄殢?#28483;绋嬪?#24538;绉绘?#23941;埌Java 8锛孡ambda灏嗗?#22557;揩鎴?#28051;?#37813;鏈塉ava寮?#24386;鑰?#28051;嶅彲?#22440;缂虹殑宸?#37711;?#37510;傚綋涓嶫ava 8娴佺粨鍚?#29863;锋潵鏃?#38171;孡ambda灏?#37911;瑰埆鏈?#37922;?#37510;傛煡鐪?#37826;囩珷"JVM骞跺彂: Java 8骞跺彂?#29056;纭"锛屽彲浠簡瑙埌?#30370;Lambda鍜?#23092;佺粨鍚?#29863;锋潵?#23047;鐢?#37828;?#28641;備?#26336;畝?#23538;骞跺彂缂栫浠強鎻愰珮绋嬪簭鏁堢?#22249;殑銆?/span>


]]>Java娴佺殑8涓?#37911;?#37804;?璇?http://www.8634070.com/jiangshachina/archive/2014/02/28/410455.htmlJohn JiangJohn JiangFri, 28 Feb 2014 07:25:00 GMThttp://www.8634070.com/jiangshachina/archive/2014/02/28/410455.htmlhttp://www.8634070.com/jiangshachina/comments/410455.htmlhttp://www.8634070.com/jiangshachina/archive/2014/02/28/410455.html#Feedback0http://www.8634070.com/jiangshachina/comments/commentRss/410455.htmlhttp://www.8634070.com/jiangshachina/services/trackbacks/410455.html
Java娴佺殑8涓?#37911;?#37804;?/span>
鏈?#37826;?#37828;?#32459;?#37827;?#37715;java.net鎺?#38013;愮殑涓?#30226;
鍗?#37826;?/span>锛屾?#24538;堪?#31777;Java娴佺殑8涓?#37832;?#37922;殑鐗?#37804;?#37510;?2014.03.07鏈鍚庢洿鏂?

Lamba琛?#26440;?#23534;?#37828;疛ava8?#22476;鐩?#37715;?#28051;烘鏈妫掔殑鐗?#37804;?#37510;備絾鎴?#29825;?#28051;?#37832;?#28051;涓?#32457;?#28725;嗘鍣睍绀轰簡Lambda杩?#28051;"璇?#23049;?#32495;?鍦?#37819;愰珮浠?#37934;佸彲璇?#37804;?#37724;屽彲鍐?#37804;?#37826;?#38344;?#37828;?#28003;?#37711;剁殑寮?#28598;?#37510;傚綋浣?#37734;?#37824;?#26473;?#28000;?#37934;佺殑琛幇鍔?#37827;?#38171;?#38317;?#28052;?#37734;?#28725;?#28000;?#37934;佺殑?#24714;瑙?#37826;?#38344;?#28003;?#28751;?#28051;?#37719;囧埌?#31777;鏂扮殑澧冪晫锛岃繖鑳戒娇鏈绗?#37815;欑殑宸?#28003;滃彉寰?#32480;鍗?#37510;?/span>

鏄?#28000;涔?#32457;?#28725;嗘鍣?#37723;?#38171;熷氨鏄疛ava Stream API銆?#37832;杩?#37812;?#37721;?#28051;庝簡涓涓?#37734;?#32510;挎瘮璧?#38171;屽氨鏄?#37734;?#28051;涓?#28725;?#37804;?#38003;借?#20345;?#20637;緝?#29678;?#27537;鐜?#28583;?#28051;?#32480;鍗?#37734;颁娇鐢Java Stream銆?#28000;?#37812;?#37807;?#29825;剁殑鏄?#38171;岃繖涓狝PI璁?#32514;栧啓涓?#29781;佺殑寰?#37916;?#32459;嬪?#24531;彉寰?#37719;佸垎绠鍗?#38171;岃?#28051;?#38003;藉?#22557;鍦?#38315;傚簲鎴?#37813;鍋氬?#34425;殑浼?#28598;氬彉?#23538;銆?#28051;?#38344;?#28751;?#37828;?#37812;?#37813;瀛埌?#27537;8涓?#37911;?#37804;?#37510;?/span>

1. Java娴佷笉?#28214;瑕丩ambda琛?#26440;?#23534;?/strong>
灏?#32480;?#26473;?#28051;狝PI纭?#28729;?#28000;嶭ambda琛?#26440;?#23534;?#28051;?#38014;?#37713;嗚壇澶?#38171;屼絾浣?#39582;?#28051;嶅繀闈?#23536;椾娇鐢Lambda銆備?#29362;彲浠?#37733;?#26473;?#37720;讳娇鐢?#37718;?#37722;嶅?#21620;儴?#34987;锛屼絾涓?#28000;涔?#29781;?#26473;?#28052;堝?#27692;憿锛?#26440;冨彲鑳界殑鍦烘櫙鏄?#38171;屼娇鐢?#28051;涓?#37826;规?#26334;紩鐢?渚?#28641;Integer::valueOf)锛屾垨鑰?#28051;涓?#28729;?#28186;嬪璞?#37510;備娇鐢?#37826;规?#26334;紩鐢彲?#30370;澶?#37833;傜殑澶氳閫?#26440;戠疆浜庡惊鐜綋?#31651;澶?#38171;屽氨濡備綘鍦?#28028;樺寲涓涓猦ash set鏌?#37813;?#37827;舵墍鐪嬪埌?#27537;銆傝?#28729;?#28186;嬪璞彲鐢?#27996;?#28729;炵幇"鍥?#27996;?#29999;??#27537;绛?#37923;?#22959;?#23534;?#37510;備絾璇?#28051;?#29781;佷娇鐢?#37718;?#37722;嶅?#21620;儴?#34987;锛岄櫎闈?#28003;?#28051;嶅緱涓?#26473;?#28052;堝仛銆?br />
2. 绐?#37711;?#23092;佸唴杩?#29723;岃皟璇?/strong>
浣犲彲浠?#37734;?#23092;佺殑浠讳綍浣嶇疆鏀?#37711;?#28003;?#37813;鎯?#37716;?#26473;?#37720;荤殑濯掕川锛岃繖涓?#28655;掕川绉?#28051;簆eek銆?#29831;?#37823;?#28003;滀娇鐢簡涓涓?#23057;堣垂鑰呭璞?#38171;屽?#33333;湡鏈?#28051;?#27996;?#37922;?#28000;讳綍缁撴灉锛?#37733;?#28051;篖ambda涓鑸?#37721;?#26473;?#37733;?#32460;?#37510;?#37812;戝枩娆?#37814;妏eek鐢?#27996;?#37722;?#32495;荤?#29111;彂閫佽皟璇?#28103;伅锛屽氨濡?br />
.peek(System.out::println)
.peek(it 
-> System.out.printf("it is %s%n", it)

3. 娴佸寲?#27554;鏈?#37812;?#37723;?/strong>
娴佸苟涓嶅眬闄?#27996;?#38342;?#37722;堟垨鏁扮粍锛?#37922;氳嚦鏄?#37733;?#28729;氶摼琛?#37510;傚?#20635;灉浣?#38003;藉?#28056;缓涓涓狪terator?#22440;Supplier Lambda?#28533;鍒涘缓娴佷腑?#27537;鍊?#38171;岀劧鍚?#28003;?#28751;卞彲浠娇鐢被java.util.stream.StreamSupport涓殑鏂规?#26335;潵鍒涘缓涓涓?#23092;佷簡銆傚彲浠?#29825;炬兂涓涓娇鐢?#37816;佺画娴嬮噺鍊?#38171;?#28641;傚?#21613;?#27195;?#22563;?#38322;忔垨?#32137;缁?#37722;為噺锛屾潵椹?#37716;殑娴?#37510;?br />
4. 娴佸寲?#27554;鏈烘暟
濡傛灉浣?#23005;?#37734;?#28725;?#37813;?#28051;涓?#32480;鍗曠殑?#27554;鏈烘暟锛屼緥濡傚彲浠?#38315;?#26473;java.util.Random锛岃繖涓?#32491;荤幇鍦?#37832;変簡涓?#28051;?#37826;扮殑set鏂规硶ints()锛宭ongs()鍜宒oubles()?#28533;鍒涘缓娴?#37510;傝繖浜?#37826;规?#26336;殑?#22136;杞?#37911;?#37832;彲浠?#29825;?#28003;?#29825;剧疆杈?#37923;?#38171;岄殢鏈?#32457;嶅瓙浠強娴佷腑?#27554;鏈烘?#25198;殑鎬?#38322;?#37510;?br />
5. 娴佸寲I/O Reader
Java绋嬪簭鍛樼殑鍙?#28051;涓?#29999;?#29785;佸伐浣滃氨鏄?#28051;琛?#28051;琛?#37734;?#29785;?#37835;?#37826;?#28000;?#37510;傜幇鍦java.io.BufferedReader鏈変簡涓涓?#37826;?#37826;规硶lines()锛?#28729;?#28028;氬皢I/O娴?#26462;寲涓?#28051;涓?#28699;楃涓?#23092;?#38171;?#28000;?#28186;?#27996;?#23092;佺殑澶勭悊銆?br />
6. 娴佸寲鏂?#28000;舵爲
濡傛灉璁?#38338;殑鏂?#28000;跺苟涓?#37828;?#28003;犵殑鑿?#38171;?#38317;?#28751;?#29831;?#29831;?#29825;?#38338;?#28051;涓?#37826;?#28000;舵爲浼氭?#28052;?#37837;?#38171;熺被java.nio.file.Files涓?#37832;?#37713;?#28051;?#37826;规硶閮藉彲浠?#26473;?#37733;?#23092;?#37510;俵ist()鏂规硶灏嗗垪鍑?#28051;涓?#37929;?#35120;?#28051;嬬殑鎵鏈?#37826;?#28000;?#38171;walk()鏂规?#26334;皢浼?#38315;?#35120;?#37734;板?#27692;埌杩?#28051;?#20579;锛岃宖ilter()鏂规硶涔?#28028;?#38315;?#35120;?#37734;?#29825;?#38338;?#26473;?#27996;?#37826;?#28000;?#38171;屼絾浼氫娇鐢?#28051;浜涘睘鎬潵杩?#29723;岃繃?#25252;(?#32139;浣?#37832;?#28051;涓狿ath瀵?#29854;?#38171;?#37832;変簺浜嬫儏浼氬彉寰?#28598;?#37833;傝?#38155;潵)銆備綘渚濈?#36346;彲浠娇鐢lines(Path)鏂规?#26335;潵閫?#26473;?#23092;?#37720;昏幏鍙栧唴瀹?#37510;?br />
7. 娴佸寲澶?#37833;?#37826;?#37832;?/strong>
濡傛灉浣?#28186;濈?#36346;?#38747;康涓嶅繕鏂?#37832;?#28598;勭悊锛屼?#21975;唴瀹瑰苟涓?#37828;熀浜庤锛?#38317;?#28052;?#28751;卞彲浠?#37734;java.util.regex.Pattern瀹?#28186;?#28051;娇鐢splitAsStream(CharSequence)鏂规硶銆傝繖瀵?#27996;?#28598;勭悊鏈?#37825;扮櫨涓囧垪?#27537;CSV鏂?#28000;舵垨CLASSPATH鍗佸垎鏈?#37922;?#37510;?br />
8. 娴佸寲ZIP鏂?#28000;?/strong>
璇村埌瀵?#38336;CLASSPATH?#27537;鎼滅储锛屼綘涔熷彲浠?#23536;堢畝鍗?#37734;拌皟鐢?#37722;?#28051;stream?#27537;鏂规?#26335;潵娴佸寲java.util.zip.ZipFiles鍜宩ava.util.jar.JarFiles锛?#28729;?#28028;氱浉?#31794;鍦?#26473;?#37733;?#28051;涓猌ipEntry?#22440;JarEntry瀹?#28186;?#37510;?br />
濡傛灉浣?#38318;藉凡缁?#39582;?#26473;?#26473;?#27996;?#27996;嬩簡锛?#38317;?#28052;?#28003;?#38002;?#28729;氱煡?#20158;瀹?#28000;?#39582;?#28051;?#37828;疛ava娴佺殑?#29056;鏈?#37922;?#38315;?#37510;?#28051;?#26473;囧皢?#28533;浼?#37832;夎冻澶熷?#27697;殑鍗?#37826;?#37720;绘?#22794;強Java娴佺殑?#29056;纭銆?#37812;?#37721;?#37828;?#29825;?#28051;?#28051;婅堪杩?#27996;?#38318;?#37828;?#29722;?#37818;?#38040;?#29863;锋潵?#27537;瀹濊棌锛?#28729;?#28000;?#37819;?#32448;轰簡Java娴佺殑?#32148;璐?#37510;?/div>

]]>?#23047;鐢Gradle鏋勫缓Java Web?#31794;鐢?璇?http://www.8634070.com/jiangshachina/archive/2014/01/23/409285.htmlJohn JiangJohn JiangThu, 23 Jan 2014 13:22:00 GMThttp://www.8634070.com/jiangshachina/archive/2014/01/23/409285.htmlhttp://www.8634070.com/jiangshachina/comments/409285.htmlhttp://www.8634070.com/jiangshachina/archive/2014/01/23/409285.html#Feedback8http://www.8634070.com/jiangshachina/comments/commentRss/409285.htmlhttp://www.8634070.com/jiangshachina/services/trackbacks/409285.html
?#23047;鐢Gradle鏋勫缓Java Web?#31794;鐢?/span>
鏈?#37826;?#37828;彂甯?#37734;?a href="http://www.java.net/">java.net
涓婄殑涓?#30226;鎽樿嚜浜?lt;Gradle in Action>涓涔?#28051;殑鑺?#38315;?/a>锛?#28000;?#32513;嶄簡?#23047;鐢?a href="http://www.gradle.org/">Gradle鏋勫缓Java Web?#31794;鐢殑杩囩銆傚?#27692;垰鎺?#29785;Gradle锛岀?#23338;埌?#31777;杩欑瘒灏?#37826;?#38171;岄殢鎵?#29831;戜簡鍑烘潵:-) (2014.01.23鏈鍚庢洿鏂?

?#32139;浠?#28051;?#37923;?#38171;?#28051;娲剧?#20344;繖銆傚湪鑱?#28051;?#37922;熸动鍜?#32457;佷汉鐢熸?#35763;腑锛?#37812;?#28000;?#28051;?#38338;寸殑璁?#28598;氫?#40511;鍚?#37827;剁?#24714;澶?#28051;?#26916;?#37929;?#37510;備?#29362;彲鑳?#29999;?#29999;稿彂?#24135;?#22172;宸?#28598;?#27996;?#28051;嶇煡鎵鎺強澶?#37818;殑鐘舵?#37510;備繚?#23508;瑙勬暣骞?#28051;撴敞浜?#28000;?#37706;肩殑鍏?#38335;?#37828;?#28051;涓?#32513;?#37814;?#38009;?#28610;界殑宸?#28003;滄竻鍗?#37510;傚綋?#21159;锛屼?#29362;彲鑳?#37804;?#37828;?#37814;?#28003;犵殑浠诲姟鍐?#37734;?#28051;?#32054;绾?#28051;?#38171;屼絾鏄?#28003;?#28052;?#29825;?#28051;嶅彲鑳?#37734;?#28003;?#37813;澶勭殑浠讳綍鍦?#37826;?#38318;藉彲鏂?#28186;?#37734;拌幏寰?#26473;?#27996;?#23480;?#28003;?#37833;?#37929;?#38171;熷浜掕?#26050;綉?#27537;璁?#38338;?#37713;?#28052;庢槸鏃?#28598;?#28051;嶅湪?#27537;锛?#37827;?#29825;?#37828;?#38315;?#26473;?#28003;犵殑绉诲姩鐢?#29831;?#38171;岃繕鏄?#37711;?#37711;辩殑?#32137;缁?#37818;?#37711;偣銆傚湪<Gradle in Action>涓涔?#28051;?#38171;?#28641;?#37733;?鎵绀虹殑璇?#37828;庢?#32448;?#28186;?#37828;?#28051;涓?#23536;?#37832;?#37722;稿紩鍔涚殑?#24434;瑙嗗寲Web?#31794;鐢?#37510;?/span>

鍥? To Do?#31794;鐢彲浠?#38315;?#26473;?#27996;掕?#26050;綉杩?#29723;岃闂?#38171;屽?#26421;娇鐢?#28729;?#37720;荤鐞嗘?#29256;嵁瀛樺偍涓殑宸?#28003;?#37833;?#37929;?/strong>


Gradle?#24387;浠惰鐜扮殑濡?#37722;?#28051;涓娇鑳?#37731;?#38171;?#28729;?#28028;氳嚜鍔?#37734;?#37813;?#29723;岃繖浜?#28000;诲姟銆?#28051;涓彃浠?#38315;?#26473;囧紩鍏?#37911;?#28729;?#26864;嗗?#29114;殑瑙勮寖浠強瀵?#32514;虹?#20344;?#37825;忔?#29114;殑浠诲姟鍘诲?#29808;伐绋?#26473;?#29723;?#37813;睍銆傞殢Gradle?#24386;甯冪殑?#24387;浠朵箣涓灏?#37828;疛ava?#24387;浠?#37510;?#29831;Java?#24387;浠?#32513;?#28051;?#28000;?#28000;?#37828;?#37819;?#28186;涗簡婧愮?#20346;紪璇?#37724;?#37813;撳寘杩?#37837;风殑?#29056;纭鍔?#38003;?#37510;傚畠涓?#23480;?#32459;嬪缓绔嬩簡涓?#26275;濂楁爣鍑嗙殑鐩?#35120;?#29999;冨眬锛?#28729;?#28028;氱?#32346;浠?#23005;?#32429;殑椤哄簭鍘?#37813;?#29723;?#28000;诲姟锛岃繖鏍?#38171;岃繖浜?#28000;诲姟鍦Java宸?#32459;嬬幆澧?#28051;?#37813;?#37828;?#37832;夋?#24526;箟?#27537;銆傜幇鍦?#37828;?#37827;跺?#28051;?#37812;?#28000;殑?#31794;鐢?#37720;诲?#28056;缓涓涓?#37835;勫缓?#21116;鏈?#39582;?#37720;讳娇鐢?#26473;?#28051;狫ava?#24387;浠朵簡銆?/span>

鏋勫缓Java?#31794;鐢?/span>
涓寮濮?#38171;屾瘡涓Gradle宸?#32459;?#38318;?#28028;氬?#28056;缓涓涓?#37722;?#28051;篵uild.gradle?#27537;鏋勫缓?#21116;鏈?#37510;?#28051;轰簡鍒涘缓璇剼鏈?#38171;屽苟鍛?#29831;?#29831;?#23480;?#32459;嬩娇鐢Java?#24387;浠?#38171;屽簲璇?#37709;忚繖鏍?#37720;诲仛锛?/span>
apply plugin: 'java'
涓轰簡鏋勫缓浣犵殑Java浠爜锛?#28051;琛?#28000;?#37934;佸氨澶熶簡銆備絾Gradle鎬?#28052;堢煡?#20158;鍘诲摢鍎?#37813;?#28003;犵殑婧?#37826;?#28000;?#37723;?#38171;?#29831;Java?#24387;浠跺紩鍏殑瑙勮?#20903;箣涓灏?#37828;?#23143;?#28000;?#37934;佺殑?#30718;寰?#37510;傞粯璁?#37734;?#38171;岃?#24387;浠?#28028;氬埌鐩?#35120;src/main/java涓?#37820;滃?#35763;骇鍝佺殑婧?#28000;爜銆?/span>

鏋勫缓Web?#31794;鐢?/span>
閫?#26473;War?#24387;浠?#38171;孏radle涔熸彁渚涗簡鏋勫缓Web?#31794;鐢殑鎵睍鏀寔銆俉ar?#24387;浠舵墿?#30541;?#31777;Java?#24387;浠?#38171;?#28729;?#37716;?#37711;簡閽堝?#31575;eb?#31794;鐢?#32459;嬪簭寮?#24386;?#27537;瑙勮寖锛屽?#33333;敮鎸佸綊闆哤AR鏂?#28000;?#37510;傝鎴?#28000;?#28052;?#37734;?#26473;?#28051;?#23480;?#32459;?#28051;?#37922;?#37922;War?#24387;浠?#38171;?/span>
apply plugin: 'war'
Web?#31794;鐢?#23143;?#37826;?#28000;剁殑榛?#29825;矾寰勬槸src/main/webapp銆傚亣璁?#28003;?#23480;?#32513;?#37828;庣?#31777;璇簲鐢?#37813;蹇?#29781;佺殑Java?#34987;銆?#38317;?#28052;?#29781;佷娇浜?#37725;佺殑鍏儴婧?#28000;爜鍜學eb璧勬簮鏂?#28000;?#28598;?#27996;庢纭矾寰?#28051;?#38171;岃宸?#32459;嬬殑鐩?#35120;?#29999;冨眬?#31794;璇?#37709;?#28051;?#38344;?#26473;?#37837;?#38171;?/span>
.
├── build.gradle
└── src
    └── main
        ├── java
        │   └── com
        │       └── manning
        │           └── gia
        │               └── todo
        │                   ├── model
        │                   │   └── ToDoItem.java
        │                   ├── repository
        │                   │   ├── InMemoryToDoRepository.java
        │                   │   └── ToDoRepository.java
        │                   └── web
        │                       └── ToDoServlet.java
        └── webapp                                               #A
            ├── WEB-INF
            │   └── web.xml                                      #B
            ├── css                                              #C
            │   ├── base.css
            │   └── bg.png
            └── jsp                                              #D
                ├── index.jsp
                └── todo-list.jsp

#A Web婧?#37826;?#28000;堕粯璁?#37929;?#35120;?br /> #B Web?#31794;鐢?#37819;忚堪绗?#37826;?#28000;?br /> #C 瀛樺偍鎻忚堪濡備綍灞曠幇HTML鍏?#32497;犵殑鏍?#23534;?#37719;?#37826;?#28000;剁殑鐩?#35120;?br /> #D 瀛?#37824;JSP褰?#23534;忕殑鍔?#37804;佽剼鏈寲瑙?#37733;剧粍浠?/span>

澹?#37828;?#28598;栭儴渚?#29863;?/span>
鍦?#28729;炵幇杩?#28051;猈eb?#31794;鐢殑杩囩锛?#37812;?#28000;娇鐢殑涓浜涚被锛屼緥濡俲avax.servlet.HttpServlet锛屽?#22549;潪Java?#29219;鍑?#37911;?Java SE)?#27537;涓?#20788;鍒?#37510;傚湪鏋勫缓宸?#32459;嬩箣鍓?#38171;?#37812;?#28000;渶瑕佺?#32346;宸?#32513;忓鏄庝簡杩?#27996;?#28598;栭儴渚?#29863;?#37510;傚湪Java绯荤粺涓?#38171;屼緷璧?#32491;诲簱鏄?#28000;JAR鏂?#28000;剁殑褰?#23534;?#37720;诲彂甯?#37724;屼娇鐢殑銆傝澶?#32491;诲?#25779;彲浠?#28000;?#28000;撳簱锛?#28641;?#28051;涓?#37826;?#28000;?#32495;荤?#29112;垨涓?#28598;?#37832;嶅姟鍣?#38171;?#28051;?#38014;?#23536;?#37510;?#28051;轰簡?#23047;鐢?#28186;?#29863;?#38171;孏radle瑕佹?#20633;綘鑷?#28751;?#28729;氫箟涓涓?#28000;撳簱銆傚嚭浜?#28051;浜涜?#38095;?#38171;?#37812;?#28000;皢?#23047;鐢?#37711;?#37711;辩殑?#24434;閫?#26473;?#27996;掕?#26050;綉杩?#29723;岃闂殑Maven Central浠撳簱銆?/span>
repositories {
   mavenCentral()                   #A
}
#A 閫?#26473;http://repo1.maven.org/maven2璁?#38338;Maven2涓?#28598;?#28000;撳?#25782;殑绠?#29037;?#29219;璁?/span>
鍦Gradle涓?#38171;屼緷璧?#37828;?#38315;?#26473;囬?#23943;疆椤规潵杩?#29723;屽垎缁勭殑銆?#37812;?#28000;皢?#28533;Servlet渚?#29863;栦娇鐢殑閰嶇疆椤?#37828;痯rovidedCompile銆?#29831;?#38320;嶇疆椤?#37922;?#27996;?#38317;?#27996;?#37734;?#32514;?#29831;?#37827;惰岄潪杩?#29723;?#37827;舵墍?#28214;?#27537;渚?#29863;?#37510;傚儚JSTL杩?#37837;风殑杩?#29723;?#37827;?#28186;?#29863;?#38171;?#37734;?#32514;?#29831;?#37827;?#28051;?#28028;氳鐢埌锛屼絾鍦?#26473;?#29723;?#37827;跺垯浼氳鐢埌銆傚畠浠?#38318;?#28028;?#37812;?#28051;篧AR鏂?#28000;剁殑涓?#20788;鍒?#37510;?#28051;?#38344;殑閰嶇疆璇?#37721;?#37735;楀鏄庝簡鎴?#28000;簲鐢?#37813;?#28214;?#27537;澶栭儴绫诲簱锛?/span>
dependencies {
   providedCompile 'javax.servlet:servlet-api:2.5'
   runtime 'javax.servlet:jstl:1.1.2'
}

鏋勫缓宸?#32459;?/span>
鎴?#28000;?#23480;?#32513;?#37713;?#28598;囧?#33445;?#21227;缓杩?#28051;?#23480;?#32459;嬩簡銆傚彟?#22476;宸?#32459;?#28051;殑涓涓狫ava?#24387;浠?#28000;诲姟鍚?#28051;篵uild銆?#29831;?#28000;诲姟?#30370;缂?#29831;?#23143;?#28000;爜锛岃繍琛?#23092;?#29831;曠?#23338;簭骞跺綊闆哤AR鏂?#28000;?-鎵鏈夌殑杩?#27996;?#28000;诲姟閮藉皢浠?#23005;?#32429;殑椤哄?#24538;鎵?#29723;?#37510;?#37813;?#29723;?#37723;?#28000;gradle build?#31651;鍚?#38171;屼?#29362;彲鑳?#28028;氬?#26944;埌褰?#28641;?#28051;?#38344;殑杈撳嚭锛?/span>
$ gradle build
:compileJava                                      #A
:processResources UP-TO-DATE
:classes
:war                                              #B
:assemble
:compileTestJava UP-TO-DATE                       #C
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test                                             #D
:check
:build

#A 缂?#29831;?#27996;?#37725;佺殑Java婧?#28000;爜
#B War?#24387;浠舵彁渚涚殑浠诲姟锛?#37922;?#27996;庡綊闆哤AR鏂?#28000;?br /> #C 缂?#29831;慗ava娴?#29831;曟簮浠爜
#D 杩?#29723;?#37719;?#37711;?#23092;?#29831;?/span>

涓婅堪杈撳?#34425;殑姣?#28051;琛?#38318;?#28000;?#29723;?#37813;?#29723;屼簡涓涓?#37922;盝ava?#22440;War?#24387;浠舵彁渚涚殑浠诲姟銆備?#29362;彲鑳?#28028;氭敞?#21104;?#22476;锛?#37832;?#28051;浜?#28000;诲姟琚爣璁?#28051;篣P-TO-DATE銆傚?#20906;殑?#21104;鎬?#37828;寚璇?#28000;诲姟琚?#29882;?#26473;?#37720;讳簡銆侴radle?#27537;澧為噺鏋勫?#28888;敮鎸佺瓥鐣?#28028;氳嚜鍔?#29831;嗗埆涓嶉渶瑕佹墽琛岀殑宸?#28003;?#37510;?#37911;瑰埆鏄?#37734;?#28598;?#37736;嬪晢涓?#26916;?#37929;?#28051;?#38171;岃鐗?#37804;?#28028;氭瀬澶?#37734;?#38010;傜?#20345;椂闂?#37510;?/span>
鍦?#29831;?#23480;?#32459;嬬殑鏍?#38010;傜洰褰?#28051;?#38171;屼?#29362;皢浼氬彂?#24135;涓涓?#37722;?#28051;篵uild?#27537;瀛愮洰褰?#38171;?#28729;冨寘鍚?#37832;?#37813;?#29723;屾?#21227;缓?#31651;鍚庣殑鍏儴杈撳嚭锛屽寘?#23277;?#34987;鏂?#28000;?#38171;?#23092;?#29831;?#37814;?#37723;?#38171;屽綊闆嗙殑WAR鏂?#28000;?#38171;?#28000;強鍍manifest杩?#37837;风殑鍦?#37813;撳寘鏃堕渶瑕佺殑涓?#37827;舵枃浠?#37510;傚涓嬪氨鏄?#37813;?#29723;屾?#21227;缓宸?#28003;滀箣鍚庣殑宸?#32459;嬬洰褰?#32513;?#37835;?#38171;?/span>
.
├── build
│   ├── classes
│   │   └── main                                          #A
│   │       └── com
│   │           └── manning
│   │               └── gia
│   │                   └── todo
│   │                       ├── model
│   │                       │   └── ToDoItem.class
│   │                       ├── repository
│   │                       │   ├── InMemoryToDoRepository.class
│   │                       │   └── ToDoRepository.class
│   │                       └── web
│   │                           ├── ToDoServlet$ToDoListStats.class
│   │                           └── ToDoServlet.class
│   ├── dependency-cache
│   ├── libs
│   │   └── todo-webapp.war                               #B
│   ├── reports
│   │   └── tests
│   │       ├── base-style.css
│   │       ├── css3-pie-1.0beta3.htc
│   │       ├── index.html
│   │       ├── report.js
│   │       └── style.css
│   ├── test-results
│   │   └── binary
│   │       └── test
│   │           └── results.bin
│   └── tmp
│       └── war
│           └── MANIFEST.MF                                #C
├── build.gradle
└── src

#A ?#23512;鍚Java?#34987;鏂?#28000;剁殑榛?#29825;?#37929;?#35120;?br /> #B 褰?#38342;嗙殑WAR鏂?#28000;?br /> #C 鐢?#27996;WAR?#27537;涓?#37827;秏anifest鏂?#28000;?/span>
浣?#23480;?#32513;忕煡?#20158;濡備綍浠?#28051;涓熀浜庢爣鍑?#37929;?#35120;?#32513;?#37835;勭殑Web宸?#32459;?#37720;?#37835;勫缓WAR鏂?#28000;?#37510;傜幇鍦?#37828;?#37827;跺欏皢瀹?#29999;冪?#25554;埌涓涓猄ervlet瀹?#37731;?#28051;?#37720;讳簡銆傚湪涓?#28051;鑺?#28051;?#38171;?#37812;?#28000;皢鍦?#37832;?#37734;?#23534;?#24386;鏈?#37731;?#28051;?#37722;?#37716;Jetty鍘?#26473;?#29723;岃繖涓猈eb?#31794;鐢?#37510;?/span>

杩?#29723;屽簲鐢?/span>
鍦?#37832;?#37734;?#37832;?#37731;?#28051;?#26473;?#29723;?#28051;涓猈eb?#31794;鐢簲璇?#23536;?#28729;?#37828;?#38171;岃?#34249;瀹?#29882;靛揩閫熷簲鐢?#23534;?#24386;(RAD)锛屽苟鑳藉?#29112;彁渚涘揩閫熺殑鍚?#37716;?#37827;?#38338;?#37510;?#37832;妫?#37734;?#37828;?#38171;?#28729;?#28051;?#29781;佹?#20633;?#29371;儴缃?#28051;涓猈eb瀹?#37731;?#26473;?#29723;?#37827;剁幆澧?#37510;侸etty涓涓?#23092;佽?#23680;殑杞?#38322;忕骇寮婧怶eb瀹?#37731;?#38171;?#28729;?#37824;?#37816;佸墠闈?#37819;愬埌?#27537;鎵鏈?#37911;?#37804;?#37510;傚湪杩?#28051;猈eb?#31794;鐢?#28051;?#37716;?#37711;?#28051;涓狧TTP妯?#37735;?#38171;?#28729;?#28751;卞彉鎴愪簡涓涓?#23677;叆寮?#28729;炵幇銆侴radle?#27537;Jetty?#24387;浠舵墿?#30541;?#31777;War?#24387;浠?#38171;?#28729;?#37819;?#28186;涚殑浠诲姟?#24434;浠皢涓涓猈eb?#31794;鐢儴缃插埌屽叆寮?#28729;?#37731;?#28051;?#38171;屽苟鑳藉鍚?#37716;?#29831;簲鐢?#37510;傚湪浣犵殑鏋勫缓?#21116;鏈?#28051;?#38171;屽彲浠?#37709;?#28641;?#28051;?#38317;?#37837;蜂娇鐢?#26473;?#28051;彃浠?#38171;?/span>
apply plugin: 'jetty'
杩?#28051;皢琚?#37812;?#28000;?#37922;?#27996;?#37722;?#37716;Web?#31794;鐢殑浠诲姟鍚?#28051;簀ettyRun銆傚畠鐢氳?#20914;彲浠?#37734;?#37827;犻渶鍒涘缓WAR鏂?#28000;剁殑鎯呭喌涓?#37722;?#37716;?#28051;涓狫etty瀹?#37731;?#37510;?#37813;?#29723;?#28051;婅堪鍛?#28000;?#37722;?#28028;氬?#26944;埌濡?#28051;嬪舰寮忕殑杈撳嚭锛?/span>
$ gradle jettyRun
:compileJava
:processResources UP-TO-DATE
:classes
> Building > :jettyRun > Running at http://localhost:8080/todo-webapp-jetty
鍦?#28051;婅堪杈撳?#34425;殑鏈鍚?#28051;琛?#28051;?#38171;岃?#24387;浠?#37723;?#29831;変簡浣燡etty鍗冲皢渚?#37722;殑璇锋?#20634;湴鍧銆?#37813;撳紑涓涓?#28003;犲枩娆殑娴忚鍣?#38171;屽苟杈撳叆涓婅堪鍦?#37735;銆?#37832;鍚?#38171;?#37812;?#28000;?#28028;氱?#23338;埌杩?#28051;猅o Do Web?#31794;鐢殑琛?#28051;?#37510;?#37733;??#30541;绀?#37734;?#28051;涓?#23092;忚鍣?#28051;?#37836;?#37930;嬪埌璇簲鐢?#37923;岄潰?#27537;鎴?#28766;?#37510;?/span>

鍥? To Do?#31794;鐢殑Web鐣岄潰?#24375;鍏惰涓?/span>

鍦?#28003;?#38315;?#26473;囩粍鍚堥敭CTRL+C鍘诲仠姝?#26473;?#28051;簲鐢箣鍓?#38171;孏radle浼?#29825;?#28729;?#28051;?#27967;杩?#29723;?#37510;侸etty濡備?#26336;煡閬撲娇鐢?#37725;?#28051;?#32468;?#37721;?#37724;?#28051;?#28051;?#37826;囩幆澧?#37720;?#26473;?#29723;岃繖涓猈eb?#31794;鐢?#38171;熷啀璇?#28051;閬?#38171;岃繖灏?#37828;?#29785;勮寖銆侸etty杩?#29723;學eb?#31794;鐢?#37813;?#23047;鐢殑榛?#29825;?#32468;?#37721;?#28751;?#37828;?080銆?/span>

鎬荤粨
鍙渶瑕?#26440;?#28751;戠殑鍔?#37716;?#38171;屼綘灏卞彲浠娇鐢Gradle鍘?#37835;勫缓骞?#26473;?#29723;?#28051;涓狫ava Web?#31794;鐢?#37510;傚彧瑕佷綘涓?#37837;?#38316;?#23536;爣鍑?#37929;?#35120;?#32513;?#37835;?#38171;?#38317;?#28052;?#28003;犵殑鏋勫缓?#21116;鏈?#28000;呴渶瑕佷袱琛?#28000;爜銆?/span>


]]>Java骞跺彂?#29056;纭瀹?#29882;?-姝婚攣(鍘?http://www.8634070.com/jiangshachina/archive/2013/12/29/408180.htmlJohn JiangJohn JiangSun, 29 Dec 2013 12:19:00 GMThttp://www.8634070.com/jiangshachina/archive/2013/12/29/408180.htmlhttp://www.8634070.com/jiangshachina/comments/408180.htmlhttp://www.8634070.com/jiangshachina/archive/2013/12/29/408180.html#Feedback1http://www.8634070.com/jiangshachina/comments/commentRss/408180.htmlhttp://www.8634070.com/jiangshachina/services/trackbacks/408180.html
Java骞跺彂?#29056;纭瀹?#29882;?-姝婚攣
鏈?#37826;?#37828;?a href="http://www.8634070.com/jiangshachina/category/53896.html">Java骞跺彂?#29056;纭瀹?#29882;?/a>绯诲垪涓殑涓?#30226;锛?#28000;?#32513;嶄簡鏈绠鍗曠殑姝婚攣鍦烘櫙锛屽?#26421;娇鐢jstack浜?#37922;熺殑thread dump?#28533;鏌?#37813;?#23005;婚攣銆?2013.12.29鏈鍚庢洿鏂?

1. 姝婚攣
涓轰簡鑳藉缁?#37814;?#32510;跨?#23340;殑瀹?#37711;?#37804;?#38171;孞ava鎻?#28186;涚殑閿?#37832;?#37714;?#38171;屼絾涓嶆?#26495;綋鍦颁娇鐢?#38335;佸垯?#24434;鑳?#27996;?#37922;熸?#23130;攣銆傛?#23130;攣鏄?#39582;跺彂缂栫涓?#28051;涓?#37827;犳硶缁?#23534;?#27537;闂?#26864;?#37510;傚彧瑕?#37734;?#28051;涓?#28000;诲姟涓娇鐢簡涓涓?#28000;?#28051;婄殑閿?#38171;?#38317;?#28052;?#28751;?#28699;?#37734;?#23005;婚?#20346;殑椋?#38340;?#37510;?br />姝婚?#20343;骇鐢熺殑?#27967;鎺?#37720;?#37733;?#38344;?#29999;?#32480;鍗?#38171;?#37719;?#28051;?#28051;?#32510;跨鍦浉浜?#32475;?#23536;呭鏂?#37813;鎵?#37832;夌殑閿?#37510;?br />
2. 閿侀?#21700;簭姝婚攣
鍦?#23005;婚攣鍦烘櫙涓?#38171;?#37832;鍏?#37736;嬬殑灏?#37828;?#38335;侀?#21700;簭姝婚攣锛?#28000;?#37934;佹竻鍗?灏?#37828;?#28051;涓?#23536;?#29999;?#29785;佺殑绀?#28186;?#37510;?br />
?#31483;鍗?
public class DeadLock {

    
private Object leftLock = new Object();
    
private Object rightLock = new Object();

    
public void leftRight() {
        
synchronized (leftLock) {
            
try {
                TimeUnit.SECONDS.sleep(
3);
            } 
catch (InterruptedException e) {
                e.printStackTrace();
            }

            
synchronized (rightLock) {
                System.out.println(
"leftRight");
            }
        }
    }

    
public void rightLeft() {
        
synchronized (rightLock) {
            
try {
                TimeUnit.SECONDS.sleep(
3);
            } 
catch (InterruptedException e) {
                e.printStackTrace();
            }

            
synchronized (leftLock) {
                System.out.println(
"leftRight");
            }
        }
    }

    
public static void main(String[] args) {
        
final DeadLock deadLock = new DeadLock();

        Thread t1 
= new Thread(new Runnable() {

            @Override
            
public void run() {
                deadLock.leftRight();
            }
        });

        Thread t2 
= new Thread(new Runnable() {

            @Override
            
public void run() {
                deadLock.rightLeft();
            }
        });

        t1.start();
        t2.start();
    }
}

3. Thread Dump
JDK鎻?#28186;涗簡涓缁勫懡浠?#29723;屽伐鍏?#38171;屽叾涓?#28751;卞寘?#23277;jstack銆?#38315;?#26473;jstack?#24434;浠?#38014;?#37721;栧綋鍓?#23005;?#26473;?#29723;岀殑Java杩涚?#23340;殑java stack鍜宯ative stack淇伅銆傚?#20635;灉Java杩涚?#23338;婧冧簡锛?#28052;熷彲浠?#38315;?#26473;?#28729;冩潵鑾?#37721;core file涓殑java stack鍜宯ative stack淇伅锛?#28000;?#37826;?#28186;?#37812;?#28000;?#28729;氫綅闂?#26864;?#37510;?br />涓轰簡鑳藉?#29110;娇鐢jstack鍘?#26440;撳嚭鐩爣Java杩涚?#23340;殑thread dump锛岄?#26663;?#22557;?#21620;瑕佸?#21228;?#21614;鍦?#37813;?#29723;屾竻鍗??#27537;绋嬪簭鏃?#38171;岃绋嬪?#24533;殑杩涚?#23338;彿銆侸DK鎻?#28186;涚殑鍙?#28051;涓?#37723;?#28000;?#29723;屽伐鍏jps?#24434;浠?#38014;?#37721;?#32495;荤粺涓?#37813;鏈塉ava杩涚?#23340;殑?#27977;鍏?#28103;伅銆?br />鍦?#37723;?#28000;?#29723;岀?#26944;彛涓?#37813;?#29723;?#37723;?#28000;?em>jps锛?#37719;冲彲浠?#23536;楀埌?#31483;鍗?鎵绀虹殑缁撴灉
?#31483;鍗?
C:\Documents and Settings\Administrator>jps
2848
4552 DeadLock
5256 Jps
鍏?#28051;?em>4552灏?#37828;?#37734;?#32471;?#38000;?#37832;?#37731;?#28051;?#37813;?#29723;岀?#23338;簭DeadLock鏃舵墍鐢?#37812;Java杩涚?#23340;殑杩涚?#23338;彿銆?br />?#21159;鍚庡啀鎵?#29723;?#37723;?#28000;?em>jstack 4552锛?#37734;?#32471;?#38000;呯殑鏈?#37731;?#28051;?#28751;?#28028;氬?#26944;埌?#31483;鍗?鎵绀虹殑缁撴灉
?#31483;鍗?
C:\Documents and Settings\Administrator>jstack 
4552
2013-12-29 18:45:41
Full thread dump Java HotSpot(TM) Client VM (
23.25-b01 mixed mode, sharing):

"DestroyJavaVM" prio=6 tid=0x00878800 nid=0xd00 waiting on condition [0x00000000]
   java.lang.Thread.State: RUNNABLE

"Thread-1" prio=6 tid=0x02b56c00 nid=0x14ec waiting for monitor entry [0x02fdf000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at concurrency.deadlock.DeadLock.rightLeft(DeadLock.java:
33)
        - waiting to lock <0x22be6598> (a java.lang.Object)
        - locked <0x22be65a0> (a java.lang.Object)
        at concurrency.deadlock.DeadLock$
2.run(DeadLock.java:53)
        at java.lang.Thread.run(Thread.java:
724)

"Thread-0" prio=6 tid=0x02b55c00 nid=0x354 waiting for monitor entry [0x02f8f000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at concurrency.deadlock.DeadLock.leftRight(DeadLock.java:
19)
        - waiting to lock <0x22be65a0> (a java.lang.Object)
        - locked <0x22be6598> (a java.lang.Object)
        at concurrency.deadlock.DeadLock$
1.run(DeadLock.java:45)
        at java.lang.Thread.run(Thread.java:
724)

"Service Thread" daemon prio=6 tid=0x02b34800 nid=0x133c runnable [0x00000000]
   java.lang.Thread.State: RUNNABLE

"C1 CompilerThread0" daemon prio=10 tid=0x02b13800 nid=0x10fc waiting on condition [0x00000000]
   java.lang.Thread.State: RUNNABLE

"Attach Listener" daemon prio=10 tid=0x02b11c00 nid=0x1424 waiting on condition [0x00000000]
   java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" daemon prio=10 tid=0x02b10800 nid=0x1100 runnable [0x00000000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" daemon prio=8 tid=0x02af4c00 nid=0x1238 in Object.wait() [0x02daf000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x22b60fb8> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:
135)
        - locked <0x22b60fb8> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:
151)
        at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:
189)

"Reference Handler" daemon prio=10 tid=0x02af0000 nid=0x12e8 in Object.wait() [0x02d5f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x22b60da0> (a java.lang.ref.Reference$Lock)
        at java.lang.Object.wait(Object.java:
503)
        at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:
133)
        - locked <0x22b60da0> (a java.lang.ref.Reference$Lock)

"VM Thread" prio=10 tid=0x02aee400 nid=0x129c runnable

"VM Periodic Task Thread" prio=10 tid=0x02b48000 nid=0x89c waiting on condition

JNI global references: 
117


Found one Java-level deadlock:
=============================
"Thread-1":
  waiting to lock monitor 0x02af4a3c (object 0x22be6598
, a java.lang.Object),
  which is held by 
"Thread-0"
"Thread-0":
  waiting to lock monitor 0x02af310c (object 0x22be65a0
, a java.lang.Object),
  which is held by 
"Thread-1"

Java stack information for the threads listed above:
===================================================
"Thread-1":
        at concurrency.deadlock.DeadLock.rightLeft(DeadLock.java:
33)
        - waiting to lock <0x22be6598> (a java.lang.Object)
        - locked <0x22be65a0> (a java.lang.Object)
        at concurrency.deadlock.DeadLock$
2.run(DeadLock.java:53)
        at java.lang.Thread.run(Thread.java:
724)
"Thread-0":
        at concurrency.deadlock.DeadLock.leftRight(DeadLock.java:
19)
        - waiting to lock <0x22be65a0> (a java.lang.Object)
        - locked <0x22be6598> (a java.lang.Object)
        at concurrency.deadlock.DeadLock$
1.run(DeadLock.java:45)
        at java.lang.Thread.run(Thread.java:
724)

Found 
1 deadlock.
鍦?#28051;婅堪杈撳嚭涓?#38171;?#37812;?#28000;彲浠?#23536;?#37828;庣鍦扮?#23338;埌涓涓?#23005;婚攣
"Thread-1":
  waiting to lock monitor 0x02af4a3c (object 0x22be6598
, a java.lang.Object),
  which is held by 
"Thread-0"
"Thread-0":
  waiting to lock monitor 0x02af310c (object 0x22be65a0
, a java.lang.Object),
  which is held by 
"Thread-1"
骞?#28051;?#28729;?#26473;樻爣鏄庝簡绋嬪簭鏄?#37734;?#37725;?#28051;?#37734;?#37826;?#37827;跺彂?#24135;?#31777;涓婅堪姝婚攣
"Thread-1":
        at concurrency.deadlock.DeadLock.rightLeft(DeadLock.java:
33)
        - waiting to lock <0x22be6598> (a java.lang.Object)
        - locked <0x22be65a0> (a java.lang.Object)
        at concurrency.deadlock.DeadLock$
2.run(DeadLock.java:53)
        at java.lang.Thread.run(Thread.java:
724)
"Thread-0":
        at concurrency.deadlock.DeadLock.leftRight(DeadLock.java:
19)
        - waiting to lock <0x22be65a0> (a java.lang.Object)
        - locked <0x22be6598> (a java.lang.Object)
        at concurrency.deadlock.DeadLock$
1.run(DeadLock.java:45)
        at java.lang.Thread.run(Thread.java:
724)

4. 灏?#32513;?/span>
姝婚?#20343;骇鐢熺殑?#27967;鎺?#37720;?#37733;?#38344;?#29999;?#32480;鍗?#38171;?#37719;?#28051;?#28051;?#32510;跨鍦浉浜?#32475;?#23536;呭鏂?#37813;鎵?#37832;夌殑閿?#37510;傞?#20352;?#21700;簭姝婚攣鏄?#37711;?#28051;?#37832;缁?#37711;哥殑鍦烘櫙锛屾澶?#26473;?#37832;?#37716;?#37804;佺殑閿侀?#21700;簭姝婚攣銆傝?#30028;劧琛幇褰?#23534;?#37832;?#37813;涓?#37722;?#38171;屼絾鏈?#29840;?#28051;?#38318;?#37828;?#28051;?#28051;?#32510;跨鍦?#28000;?#28051;?#37722;岀殑椤哄?#24532;潵鑾?#37721;栫浉鍚岄?#20345;椂锛屽彂鐢熶簡姝婚攣闂?#26864;?#37510;?br />?#23047;鐢thread dump?#24434;浠?#29999;?#37716;?#37812;?#28000;?#37714;嗘瀽姝婚?#20343;骇鐢熺殑鍘?#37733;?#37510;傞櫎?#31777;?#27967;鎺娇鐢jstack鍛?#28000;潵鑾?#37721;杢hread dump杈撳嚭浠?#28598;?#38171;孞DK杩樻彁渚涗簡jvisualvm宸?#37711;?#38171;?#28729;?#38003;?#28000;彲瑙嗗寲?#27537;鏂瑰紡?#30541;绀篔ava绋嬪?#24533;殑杩涚?#23338;彿骞跺鍑簍hread dump銆?/div>

]]>Java Concurrent Animated(璇?http://www.8634070.com/jiangshachina/archive/2013/12/07/407310.htmlJohn JiangJohn JiangSat, 07 Dec 2013 09:45:00 GMThttp://www.8634070.com/jiangshachina/archive/2013/12/07/407310.htmlhttp://www.8634070.com/jiangshachina/comments/407310.htmlhttp://www.8634070.com/jiangshachina/archive/2013/12/07/407310.html#Feedback1http://www.8634070.com/jiangshachina/comments/commentRss/407310.htmlhttp://www.8634070.com/jiangshachina/services/trackbacks/407310.html
    鍦?#37832;鏂?#28051;鏈熺殑Java Magazine涓?#37832;?#28051;?#30226;璁胯皥锛?#28000;?#32513;嶄簡涓涓?#28699;?#28052;燡ava骞跺彂缂栫?#23340;殑鍔?#37922;诲簲鐢?a href="http://sourceforge.net/projects/javaconcurrenta/">Java Concurrent Animated銆?#29831;簲鐢?#28000;?#37719;佸?#21977;洿瑙傜殑鏂瑰紡?#30541;绀轰簡Java骞跺彂宸?#37711;峰寘涓殑姣?#28051;涓噸瑕佺粍浠?#38171;岄檷浣庝簡瀛?#28052;燡ava骞跺彂缂栫?#23340;殑?#27622;搴?#37510;?2013.12.07鏈鍚庢洿鏂?

Java Magazine锛?#37832;?#28598;氬皯浜?#23480;?#32513;?#29831;?#37922;?#26473;囦簡浣犵殑Java Concurrent Animated?#31794;鐢?#38171;?/span>
Grazi锛?#29831;簲鐢?#37828;?#37734;?009骞?鏈?#29722;紩鍏殑锛?#28000;?#38317;?#37827;剁畻璧?#38171;屽凡缁?#37832;変簡澶害20000?#27537;涓?#26462;?#38322;?#37510;備?#21978;?#38095;戝埌宸?#37832;夌害涓鍗?#28051;囩殑Java寮?#24386;鑰?#38171;岃繖涓?#28051;?#26462;?#38322;?#37813;嶅彧鏄?#23534;濮?#37510;傛寜鍥?#28729;跺尯鍒?#38171;?#28051;?#26462;?#37832;澶氱殑鍒嗗埆鏄?#32519;?#37733;?23%)锛?#37719;?#25652;?14)鍜?#28051;?#37733;?7%)銆?/span>
    浣犲彲浠?#28051;?#26462;?#28051;涓彲浠?#37813;?#29723;岀殑JAR鏂?#28000;?#38171;岀劧鍚?#28000;呴渶鍙屽?#35826;畠灏卞彲浠?#26473;?#29723;屼簡銆?#29831;簲鐢?#37828;?#37922;辫鍗曢鍔殑锛屾垨鑰?#28052;熷彲浠娇鐢?#37722;?#28051;婃垨鍚?#28051;嬮敭鍦?#28051;?#37722;岀殑鍥惧儚鍜屽姩鐢讳箣闂?#26473;?#29723;屽?#33392;埅銆傚畠鑳?#26473;?#29723;?#37734;?#29831;?#28641;俉indows锛孧ac锛孡inux绛?#32475;?#37813;鏈夌殑骞?#37721;?#28051;?#37510;傚畠瑕佹?#20634;?#22798;Java SE 6?#22440;?#27967;?#29678;?#27537;鐗?#37832;?#37510;?/span>

Java Magazine锛氬杩?#28051;簲鐢?#37832;鍏?#37736;嬬殑鍙嶉鏄?#28000;涔?#38171;?/span>
Grazi锛氬瀹?#37723;?#29831;?#37812;?#26473;?#28051;?#23480;?#37711;?#23536;堝鐢?#37510;傝澶氫?#34425;瀹?#28725;?#23005;?#37808;熷埌鍏?#28610;?#38171;屽挨鍏舵槸閭?#27996;?#23005;?#29831;?#37733;?#37722;?#37733;?#38339;熸暀鎺?#37722;?#38315;傚?#36346;彂鎶鏈殑鑰?#29999;?#28051;?#26864;嗗浠?#37510;侸ava鏄?#37832;鏃?#37734;?#37837;稿?#20906;?#35826;簱涓紩鍏?#39582;跺彂?#27537;璇?#29815;?#31651;涓銆傚湪?#32139;鏃?#38171;岃繖鏄?#28051;涓?#23536;堝己澶殑鐗?#37804;?#38171;屼絾鎴?#28000;?#23536;堝揩灏卞彂?#24135;涓涓?#38344;?#29999;?#28028;?#32457;?#27537;绋嬪簭鍛?#28051;?#28028;氬啓鍑?#23536;?#32495;?#32495;曠殑骞跺彂浠爜銆傝繘琛屾?#26495;綋?#27537;骞跺彂缂栫鏄?#28051;浠?#37733;伴毦鐢氳嚦鏄?#28051;嶅彲鑳界殑浜嬫儏锛屼絾鏄?#28641;備綍浜?#28000;?#38003;借?#21464;簺鏃?#38338;?#37720;荤悊瑙?#28051;浜涚幇鏈夌殑妗嗘灦锛?#38317;?#28052;?#37734;?#26473;?#29723;屽?#36346;彂缂栫?#20345;?#33333;墍浜?#37922;熸綔鍦?#38335;?#29831;?#28751;?#28028;氬彉寰?#37835;佸皯銆?/span>
    渚?#28641;?#38171;?#37720;荤?#23340;湅Java鍐呭?#27195;鍨?#37510;傚紑?#24386;鑰?#32513;?#29999;稿拷瑙咼ava鍐呭?#27195;鍨?#38171;岃屽儚涓?#39582;哥?#24533;殑鍌荤摐涓鏍?#37734;?#32514;栫爜锛?#38317;?#28052;?#28000;?#28000;殑绋嬪簭浼?#28051;嶅姝?#29999;?#38171;?#37733;?#28051;篔ava铏氭嫙鏈?JVM)鍜?#37832;嶅姟鍣彲鑳?#37827;犳硶鍒?#37922;埌鐢盝ava鍐呭?#27195;鍨?#37813;鎻?#28186;涚殑浼樺寲銆?#37922;变?#24225;唴鏍?#37734;?#38315;熷害涓庢暟閲?#28051;?#38318;?#37832;変簡澧為暱锛?#37720;傚晢浠?#37832;?#37832;?#38003;藉楂樻晥鍦?#37714;?#37922;埌杩?#27996;涘唴鏍?#38171;岀劧鑰?#37922;变簬閿?#29831;殑骞跺彂绠悊锛?#37832;潵濡?#37832;?#26473;?#29723;岀殑绋嬪簭鍗?#23534;濮嬮?#22247;埌?#31777;涓浜?#38342;舵?#29114;殑閿?#29831;?#37510;?/span>

Java Magazine锛氫綘鏄?#29831;?#38171;岃繖涓簲鐢?#28028;?#28000;?#37812;?#28000;?#37813;铏氭?#21229;殑鏂瑰紡鍘讳娇寮?#24386;鑰?#28000;?#38003;藉?#29112;洿蹇?#28051;旂洿瑙傚湴鎺屾彙Java骞跺彂?#27537;鍘熺悊涓?#28729;?#29882;?#38171;?/span>
Grazi锛?#38317;?#37828;?#26440;惧埌杩?#28051;鐩殑涓涓?#37832;夎叮?#27537;閫?#23536;?#37510;備?#29365;煡?#20158;?#27537;锛孞ava Concurrent Animated骞?#28051;?#37828;?#28051;涓狥lash鍔?#37922;?#37510;傚畠鏄?#28051;缁勫彲浜?#27996;掔殑Java绋嬪簭锛?#28052;?#37719;?#38171;屾瘡涓?#37716;?#37922;?#38318;?#37828;?#37930;?#37734;?#37734;娇鐢?#28729;?#37813;瑕佹紨绀虹殑骞跺彂?#31885;浠?#37510;傚湪灞?#39582;曠殑鍙?#26440;?#37828;?#28051;涓睍绀?#28000;?#37934;佺墖鏂殑闈?#37833;?#38171;?#37922;变?#24225;姩鐢荤殑杩?#29723;?#38171;?#28729;?#28028;氬姩鎬?#37734;伴珮浜?#37828;?#32448;哄強鎭?#28598;?#23005;?#37734;?#37813;?#29723;岀殑浠爜銆?/span>
    璁?#28003;?#32513;?#28003;?#28051;涓?#28186;嬪瓙锛岃繖涓?#28186;嬪?#24876;彂鐢?#37734;ReadWriteLock杩?#28051;?#37716;?#37922;讳腑銆俁eadWriteLock鐢?#27996;庣?#32346;鏁版嵁?#27537;涓鑷?#37804;?#37510;傚畠?#21393;璁?#28051;嶅?#26945;暟閲?#38340;?#37714;剁殑绾跨鍘昏幏鍙?#29831;婚攣锛屽苟鑳藉?#36346;彂鍦?#28725;?#26473;?#28051;?#38335;?#26473;?#29723;屾搷浣?#37510;備絾鏄?#38171;屽?#27409;?#36328;鍦?#38014;?#37721;?#26473;?#28051;?#38335;佷箣鍓嶅彧鑳?#32475;?#23536;?#37813;鏈夌殑璇荤?#36328;鎵?#29723;?#32513;?#37833;?#37510;?#28051;鏃?#28051;涓?#37712;欑?#36328;?#23341;幏寰椾簡杩?#28051;?#38335;?#38171;?#38317;?#28052;?#37711;?#28729;冪殑璇荤?#36328;?#23339;垨鍐欑?#36328;?#23338;皢鏃犳?#26337;幏鍙?#28729;?#37510;?/span>
    鍋?#29825;?#28051;涓?#37712;欑?#36328;姝?#37734;?#32475;?#23536;?#23005;?#37734;?#37813;?#29723;?#28051;殑璇荤?#36328;鍘?#38322;?#37824;?#26473;?#28051;?#29831;婚攣锛屼絾绐佺劧涓涓?#37826;扮殑璇荤?#36328;?#23341;杩囨潵?#31777;銆?#38317;?#28052;堣?#20344;簲璇?#38014;?#23536;?#26473;?#28051;?#38335;佷?#27693;瘮杈?#28610;?#37723;?#38171;?#26473;?#28051;?#37826;扮殑璇荤?#36328;?#23338;簲璇?#29882;戝埌鍐欑?#36328;?#23338;墠闈?#37720;诲悧锛熸瘯绔?#38171;?#28641;傛?#28355;叾瀹冪殑璇荤?#36328;?#23338;凡缁忚幏寰椾簡杩?#28051;?#38335;?#38171;?#38317;?#28052;?#37826;版潵?#27537;璇荤?#36328;涓?#28000;涔?#29781;?#37720;?#32475;?#28051;涓?#28751;?#37734;?#32475;?#23536;?#28051;殑鍐欑?#36328;鍛?#38171;熻岃繖瀹?#38340;?#28051;?#26473;?#23005;?#37828;疛ava 5鎵骞茬殑浜嬪効銆備?#21976;煇娆?#37812;?#37734;Java 6涓?#26473;?#29723;岃繖涓?#37716;?#37922;?#37827;?#38171;?#37812;?#23049;剰?#22476;琛?#28051;哄彂鐢熶簡鏀瑰彉銆傚嵆锛岄殢鍚庤屾潵?#27537;璇荤?#36328;鍦?#38014;?#37721;栧埌杩?#28051;?#38335;佷箣鍓嶅彲鑳借?#20346;瓑寰?#37813;鏈夌殑鍐欑?#36328;?#23338;厛閲?#37824;?#38335;?#37510;?/span>
    鎴?#29825;?#28051;?#26473;?#28051;?#37826;扮殑琛?#28051;?#37828;?#28051;涓狟UG锛?#28051;?#37722;?#39582;跺彂涓?#28729;禜einz Kabutz鍗氬鎻愬強?#31777;姝?#27996;?#37510;傚?#27692;瑙?#38322;婇亾锛岃繖涓?#37828;?#28051;涓?#38335;?#29831;?#38171;岃?#28051;涓?#37911;?#37804;?#37510;傚?#20635;?#28355;厑璁?#37826;板埌?#27537;璇荤?#36328;?#23341;?#20914;埌姝?#28598;?#27996;?#32475;?#23536;?#28051;殑鍐欑?#36328;?#23340;殑鍓?#38344;?#37720;?#38171;岃繖灏?#28699;?#37734;?#27996;?#37922;熺?#36328;?#23342;楗?#37833;?#28000;剁殑楂橀闄?#37510;?#37733;?#28051;?#38171;屽瓨鍦?#28051;绉嶅緢澶殑?#24434;鑳?#37804;?#38171;屽彲鑳芥病鏈?#28000;讳綍鍐欑?#36328;鑳借幏寰?#26473;?#28051;?#38335;?#38171;?#28729;?#28000;皢姘?#26473;?#32475;?#23536;呯潃銆傝繖灏?#37828;?#28051;涓?#28641;備綍?#23047;鐢?#37716;?#37922;?#37720;昏绀?#28186;?#29863;?#27996;嶫VM杩?#29723;?#37827;?#37911;?#37832;殑绾跨琛?#28051;虹殑渚嬪瓙銆?/span>

Java Magazine锛?#28000;?#37716;?#37922;?#37825;欑?#23340;殑褰?#23534;忔潵?#30541;绀?#37911;?#23048;?#37706;?#38171;?#37734;Java骞跺彂缂栫涓?#37832;変綍涓?#28028;?#28051;?#37722;?#37722;?#38171;?/span>
Grazi锛歁iller瀹氬?#23339;暀浼?#37812;?#28000;?#38171;?#37812;?#28000;殑澶?#38004;?#37734;煇涓鏃跺埢鑳藉?#21229;?#21977;殑鎬?#32513;寸殑鏁?#38322;?#37828;?#37832;?#38340;愮殑銆備汉绫诲鑴?#37706;?#37722;?#27996;?#26473;?#29723;岄?#21700;?#24533;殑鎬?#32513;?#28598;勭悊锛?#38317;?#28052;?#37719;?#28186;?#37812;?#28000;?#38003;藉?#29111;厠鏈嶈韩?#32139;涓婄殑鏉熺細锛屽苟鑳藉鍘?#23005;?#32429;?#37734;?#26473;?#29723;岀悊瑙?#38171;?#37734;?#28000;?#37722;?#28052;熷緢?#27622;杩?#37733;?#38007;?#37715;?#37720;婚噸鏂?#37835;?#38315;犲墠闈殑鎬?#32513;?#28598;勭悊銆傚彲浠?#38002;?#28729;?#37734;?#37828;?#38171;?#28641;傛?#28355;彟涓涓?#23534;?#24386;鑰?#37734;?#28000;?#37722;?#38003;芥繁鍏?#28725;瑰叾杩?#29723;岀?#26050;锛?#38317;?#28052;?#28000;嶇劧闈?#29999;搁毦浠?#28000;?#37720;?#37832;夌殑鎬?#32513;?#37812;愭灉涓?#37712;嶆鎹曟?#22794;埌璁煡杞?#26473;?#37510;傝繖鏍风殑璇?#38171;岃?#21975;?#36777;殑浠?#37934;佸氨浼氬?#22562;?#20346;劧鍦?#28051;?#38003;芥甯?#23480;?#28003;滀簡銆?/span>
    閫?#26473;囦娇鐢?#22935;嗘灦锛?#37812;?#28000;?#28051;?#28000;呭?#21975;?#36346;彂缂栫?#23338;鎵?#32513;欎簡鍒涘缓鍜?#32513;?#37814;?#29831;?#22935;嗘?#21057;殑鑱?#37828;庡紑?#24386;鑰?#28000;?#38171;岃?#28051;?#26473;?#28051;烘閫?#29825;?#29825;?#37827;跺紩鍏簡涓涓?#29831;嶅吀銆?#37813;浠?#38171;?#37812;戝彲浠?#29831;?#38171;?#8220;涓?#38344;殑浠?#37934;佷?#27692;綋浣CyclicBarrier鍘?#37813;?#29723;?#8221;锛岃?#27996;?#28000;?#28028;?#37828;庣櫧閭?#37828;?#28000;涔堟剰鎬?#37510;?#38315;?#26473;?#28051;簀ava.util.concurrent涓殑鎵鏈夌粍浠?#38318;藉紩鍏?#28051;涓彲浜?#27996;掑寲?#27537;鍔?#37922;诲簲鐢?#38171;屽紑?#24386;鑰?#28000;?#37904;圭潃?#32054;?#29219;灏?#38003;藉緢鏂?#28186;?#37734;板皢浠?#28000;?#37813;鎺?#32460;剁殑鍔?#38003;?#26473;?#29723;屽彲瑙嗗寲锛屼娇?#24714;瑙?#26473;?#27996;涚?#26945;?#26334;彉寰楃?#29111;?#20906;畝鍗曚簡銆?/span>

Java Magazine锛氫?#29362;綋鏃舵鍦?#37934;旂?#33333;煇浜涚洿瑙?#38171;岃繖浜涚洿瑙夊彲浠?#29999;?#37716;洿鏂?#28186;?#37734;?#28699;?#28052;?#39582;跺彂缂栫銆?#28000;庡紑?#24386;鑰呯殑鍙嶉?#22559;潵鐪?#38171;岃繖浜涚洿瑙夌湅璧锋潵鏄?#37832;?#37825;堢殑銆?/span>
Grazi锛?#37828;殑銆備緥濡?#38171;?#37812;?#37715;?#38344;?#29785;?#38322;婄殑ReadWriteLock?#29056;鏈?#37716;?#38003;?#37510;?#29831;昏?#28000;彲鑳界悊瑙簡锛?#28052;熷彲鑳芥病鏈?#37510;傜幇鍦?#29825;?#37812;?#28000;?#37930;嬬湅杩?#28051;?#28051;庡?#33333;湁鍏崇殑鍔?#37922;?#38171;?#28641;?#37733;?#29723;?鎵绀?#37510;?/span>

    缁胯?#33580;?#36328;鏄?#29831;荤?#36328;锛?#37832;涓?#38344;殑鐧借?#33580;?#36328;(甯潃鑿?#35120;?#32480;?#28598;?鏄?#28051;涓?#37712;欑?#36328;锛?#28729;?#28051;?#38344;殑鐧借?#33580;?#36328;鏄?#28051;涓?#37826;扮殑璇荤?#36328;锛岃绾跨鍦?#38014;?#37721;栭?#20343;箣鍓嶅?#21620;瑕佺瓑寰?#37813;鏈夌殑璇荤?#36328;涓庡?#27409;?#36328;鎵?#29723;?#28729;屾瘯銆傚?#20635;灉浣犵?#29808;?#32472;?#22800;挳骞?#29785;傜湅杩?#27996;涘姩鐢?#38171;?#28028;氭瘮閫?#26473;?#23092;忚?#22562;?#20344;?#26947;殑瑙?#38322;?#37804;?#37826;囧瓧鍘?#26473;?#29723;岀悊瑙?#29781;佺畝鍗?#23536;?#28598;氫簡銆?/span>

Java Magazine锛欻einz Kabutz璇?#29825;洪亾锛孞ava琚?#37835;勫缓鎴?#38003;藉涓娆?#37804;?#37707;?#29825;?#28598;氫?#23339;儏锛岃岃繖姝?#28051;庡?#36346;彂瀹屽叏?#27977;鍏?#37510;備?#29365;殑瀛?#28052;?#32495;荤粺鏄?#28641;備綍鎻愰珮绋嬪簭鍛樼殑鎶鑳?#38171;?#28000;?#28186;?#28000;?#28000;?#38003;?#38340;?#28003;庡?#36346;彂閿?#29831;殑椋?#38340;?#37510;?/span>
Grazi锛?#32513;?#29999;?#37734;?#38171;屽綋鎴戣?#20344;鍔涘厠鏈?#28051;涓?#39582;跺彂闂?#26864;?#37827;?#38171;?#37812;戠煡?#20158;瑙?#37712;?#37826;规灏?#28699;?#37734;?#27996;庢煇涓?#29825;?#29825;?#22959;?#23534;?#28051;?#38171;屼絾鏄?#37725;?#28051;涓?#37723;?#38171;?#37734;?#23534;?#24386;鑰?#37818;?#28725;讳竴涓?#23005;?#32429;?#29785;?#37712;?#37826;规鏃?#38171;孞ava Concurrent Animated涓?#28000;?#28000;?#37819;?#28186;涗簡涓涓?#37813;鏈?#37826;规?#22562;殑鐩?#35120;?#38171;?#37734;?#23157;?#24386;鍑烘纭?#37826;规?#22562;殑杩囩涓?#38171;?#28729;?#37813;?#23125;旂潃鍚?#28725;肩殑瑙?#38009;?#37510;?/span>

Java Magazine锛氬綋浣?#32480;?#37918;嗙殑鍥?#38339;熸鍦娇鐢Java骞跺彂锛屽苟涓?#28003;?#37724;屼?#29365;殑鍥?#38339;?#38318;芥?#34411;洿濂?#37734;?#37720;荤悊瑙Java骞跺彂锛孞ava Concurrent Animated鏈夌潃瀹冪殑鍑哄彂?#20579;銆?#37828;?#28000;涔堝鑷?#28003;犱娇鐢?#37716;?#37922;诲憿锛岃?#33445;?#24538;堪涓?#26473;?#28051;?#26473;囩鍚?#38171;?/span>
Grazi锛?#37812;戠殑鍩?#29825;?#37828;?#38333;堝鎶?#29863;勯儴闂殑鏈嶅姟鍣?#32468;疛ava?#31794;鐢?#38171;?#37734;?#38317;?#38322;?#38171;屽?#36346;彂鏄?#28051;涓?#38315;?#29999;?#38318;?#28028;氬?#26944;埌鍏?#23049;殑闂?#26864;?#37510;備氦鏄?#37723;?#28000;?#29781;佹?#20634;欢杩?#29781;佷綆锛岃繖鏍峰彲浠?#32429;繚浠?#28000;?#37734;?#26473;?#28051;渶瑕佷簬涓姣?#32457;掔?#26944;彛鏃?#38338;?#37712;?#37817;曟?#22793;氦鏄?#37832;?#28028;氱殑?#30254;璧?#28051;?#28051;?#28028;?#37812;?#28051;?#28598;辫|鑰?#37510;傛壒閲?#28598;勭悊涔?#29781;佹?#20634;揩閫?#28729;?#37812;?#38171;?#32475;?#32475;?#37510;?#37813;浠?#37812;?#23534;濮嬬?#23338;埌閭?#27996;涘彲鎬曠殑鍙?#37712;?write-only)?#31885;浠?#38171;岃繖浜涚粍浠朵娇浜?#28000;?#37734;?#39582;跺彂缂栫鎸?#37813;庣潃銆?#37812;戣嚜宸?#28052;熻韩澶勫叾涓?#37510;?/span>
    ?#28999;澶?#28051;?#37719;?#38171;?#37812;?#23005;?#37735;?#37734;?#37832;?#37734;?#37712;?#38171;屽皢瑕佸?#23941;線鑺?#37716;?#37725;?#28051;?#37812;戠殑鍥?#38339;熷仛涓涓?#37711;?#27996;庡?#36346;彂?#27537;璁叉紨銆?#37812;?#23005;?#28725;?#29825;叉?#26050;殑PPT杩?#29723;?#37832;鍚庣殑澶勭悊锛?#38317;?#32513;勫?#33636;伅鐗囩潃閲嶆紨绀轰簡姣?#28051;涓噸瑕佺殑?#31885;浠?#37510;?#28051;轰簡?#32041;瀵?#37812;?#23092;忚java.util.concurrent涓?#23011;?#28051;?#39582;跺彂?#31885;浠剁殑鐘舵?#38171;?#37812;?#37712;欎簡涓浜?#37912;舵?#37832;?#38171;?#28729;?#28000;睍绀轰簡涓浜?#28186;?#37812;?#37721;傝?#37922;殑绠鍗?#37826;?#37832;?#23057;堟伅銆傚湪?#31651;鍓嶇殑鐢熸动涓?#38171;?#37812;戞浘鍦?#28051;瀹?#27996;掕?#26050;綉鍒?#28051;氬叕鍙?#28051;?#23534;?#24386;浜?#27996;?#23534;忕殑?#29238;鎴?#38171;?#37813;浠?#37812;戞?#20634;緱璁?#28598;?#28051;庡姩鐢荤浉鍏崇殑?#29025;璇?#37510;傝?#27406;娇鎴?#37807;冲埌?#24434;浠皢PPT鏇?#37817;?#37812;?#28051;?#31885;浜?#27996;?#23534;忕殑鍔?#37922;诲簲鐢?#38171;?#38317;?#28028;氭洿涓虹洿瑙?#37510;?/span>
    鍦?#32475;夐鏈虹殑杩囩涓?#38171;?#37812;?#37712;欎簡涓涓?#37714;?#23005;殑鍔?#37922;诲紩鎿?#38171;岀劧鍚?#37734;?#37812;戠殑鐘舵?#37832;?#28051;?#29835;?#37922;簡杩?#28051;紩鎿?#37510;傚埌?#31777;绗?#27996;?#28598;?#37827;櫒锛?#37812;?#23480;?#32513;?#37832;?#28051;涓彲鐢殑鍘熷?#23340;?#23338;簭銆傚?#27692;?#23384;潵锛?#37812;?#28051;?#27967;鑷?#37716;?#27996;?#26473;?#28051;?#22935;嗘灦锛屽苟涓?#37722;稿紩?#31777;鍏?#28000;?#28051;?#28729;剁殑?#32531;璁?#37510;?#37812;?#28028;?#38315;?#26473;?#28051;浠?#37827;?#37832;?#37911;?#37832;?#32513;Brian Goetz锛?#28000;?#27996;烘儕璁剁殑鏄?#38171;?#28000;?#28051;烘瘡涓?#37716;?#37922;荤?#23338;簭閮?#32513;?#37713;轰簡?#32531;璁?#37510;?#37812;戝皢浠栫殑鎵鏈夊缓璁埌鍚告敹?#22476;?#31777;璇?#22935;嗘灦涓?#37510;傚湪鎴戠殑绗?#28051;娆JavaOne璁叉紨涓?#38171;Kirk Pepperdine鍔?#37711;簡杩涙潵銆?#28000;栧缓璁?#28051;?#37716;?#37922;诲簲鐢?#37734;?#37930;熸?#27537;PPT涓?#37716;?#37711;?#37819;忚堪锛?#28000;?#28186;?#29825;叉紨鑰?#38003;?#29825;?#28003;?#23005;?#37734;?#29825;?#29825;虹殑鍐?#28729;?#37510;傞殢鍚庢垜鍔?#28051;?#38317;?#27996;?#37819;忚堪锛岃?#27409;瀹?#38344;?#29999;?#37832;?#37922;?-涓嶅彧鏄?#28725;?#29825;叉紨鑰?#37832;?#37922;?#38171;屽浜?#32513;?#32468;?#37922;埛涔熷緢鏈?#37922;?#37510;侶einz Kabutz涔熷姞鍏簡閭?#37734;?#29825;叉紨锛屽?#36346;缓璁?#28103;?#37824;规煇浜涘姩鐢?#38171;?#28000;娇瀹?#28000;洿涓虹洿瑙?#37510;?/span>
    鍦?#37721;?#28051;鍦?#29825;叉紨涓?#38171;?#28051;涓?#23536;?#37832;夋縺?#20751;?#27537;杞?#28000;?#37724;?#29831;?#29999;圤liver Zeigermann鎸囧嚭锛屽緢鏄?#37906;剁己灏戜簡閽堝?#31540;oncurrentHashMap?#27537;鍔?#37922;?#37510;?#37812;?#38338;?#28000;?#37828;?#37722;?#37832;?#37711;磋叮璐尞杩?#28051;?#37716;?#37922;?#38171;岄殢鍚?#28000;?#23075;诲?#29361;簡閭?#28051;?#23536;?#37832;?#28000;?#37706;肩殑鍔?#37922;荤?#23338;簭銆?/span>

Java Magazine锛氫綘鑳?#29999;潃鎴?#28000;?#26473;?#28051;閬岼ava骞跺彂宸?#37711;峰寘涓殑绫诲悧锛熷苟鑳?#37722;?#29785;?#38322;?#28051;涓?#26473;?#27996;涘姩鐢荤?#23338;簭鏄?#28641;備綍?#23047;寮?#24386;鑰?#28000;洿鏄?#27996;庢繁鍏悊瑙?#26473;?#27996;涚被锛?/span>
Grazi锛氬?#30028;殑锛屼絾鍦?#23052;?#37832;?#37716;?#37922;荤?#23338;?#24533;殑鎯呭喌涓嬬瀹?#23536;堥毦鍔炲埌銆傝鎴?#28000;?#37930;嬬湅CyclicBarrier锛?#28729;?#37832;?#28051;?#28051;噸瑕佺殑鐘舵?#38171;?#28641;?#37733;?鍜?#37733;?鎵绀?#38171;?#28729;?#28000;睍绀轰簡涓涓?#38341;滅?#23941;拰鍥?#28051;?#37812;?#37723;?#37510;傚湪鍥?涓?#38171;?#37812;?#28000;彲浠?#37930;嬪埌鏈?#28051;?#28051;?#37812;?#37723;?#23480;?#32513;忓埌?#31777;锛?#37813;浠?#28729;?#28000;?#29722;?#38339;?#23005;?#32513;?#32513;?#37715;?#26473;?#37510;?#37733;??#30541;绀轰簡锛?#28051;鏃?#32471;?#37733;?#28051;?#37812;?#37723;?#28052;熷埌杈句簡锛屾瘡涓?#37812;?#37723;?#37721;堝彲浠?#37722;?#37715;?#29863;颁簡銆?/span>



    杩?#28751;?#35120;?#29854;?#37734;?#29831;犻?#23105;簡闅滅?#23943;殑姒傚康锛?#27996;?#37719;?#38171;?#37734;?#37813;鏈?#37812;?#37723;樺埌杈鹃?#28357;?#23943;?#36891;箣鍓?#38171;屾瘡涓?#37812;?#37723;樺?#21620;绛?#23536;?#37510;傞殢?#28483;骞跺彂?#31885;浠?#28598;?#37833;傚害?#27537;澧?#37716;?-渚?#28641;侳ork/Join?#27537;鍔?#37922;?#38171;?#28000;強閭?#27996;涙紨绀?#37720;?#37922;熺殑wait鍜宯otify鏈?#37714;剁殑鍔?#37922;?-?#23047;鐢?#37716;?#37922;荤?#23338;?#24533;殑濂藉?#21227;氨?#27967;涓嶈璇翠簡銆?/span>

Java Magazine锛氳?#22563;皥鍦?#37714;涘缓杩?#27996;涘姩鐢荤?#23338;?#24533;殑杩囩涓?#37813;閬囧埌?#27537;涓浜?#37816;?#37812;?#37510;?/span>
Grazi锛?#37832;?#28051;浜?#37816;?#37812;?#37510;傚紑濮?#37827;?#38171;岀?#36328;琚?#29723;?#32448;?#37812;愮澶?#37510;傚浜?#28598;氭暟骞跺彂?#31885;浠?#38171;岃繖绉?#29723;?#32448;烘硶鏄?#37832;?#37825;堢殑銆?#37722;庢潵鎴?#28000;?#36423;呴?#32472;彁渚?#28051;涓彲瑙嗗寲鏂规锛?#28051;?#28000;?#29781;佽绀虹?#36328;锛岃繕瑕佽绀築lockingQueue涓殑瀵?#29854;?#37510;?#37813;浠?#38171;?#37812;?#28051;嶅緱涓嶅紩鍏?#28051;涓?#32457;颁箣涓?绮剧伒绫诲瀷(sprite-type)"?#27537;姒傚康锛岀劧鍚庢垜浠?#37832;変簡涓涓?#32480;?#28598;?#37736;嬬殑绮剧伒绫诲瀷鍜?#28051;涓?#37826;扮殑"瀵?#29854;?鍨嬬殑绮剧伒绫诲瀷銆?#37722;庢潵锛孋oncurrentHashMap鍜孉tomicInteger鍙堥渶瑕?#37826;扮殑绮剧伒绫诲瀷锛?#37733;?#28051;?#37812;?#28000;?#29831;?#37733;?#29781;佸浠?#28000;殑璁?#32480;?#28051;?#27996;?#37817;?#29723;?#28051;?#26473;?#29723;屽彲瑙嗗寲銆?/span>
    鍚?#38344;?#37721;堟潵?#31777;Fork/Join锛?#37826;扮殑鎸?#37812;?#28751;?#37828;?#28641;備綍鍘?#29723;幇閭?#27996;?#28729;屽叏涓?#37722;?#27996;庣幇鏈夋?#21976;?#33333;墍琛?#37916;扮殑?#24434;瑙嗗寲?#20788;浠?#37510;傝繕鏈?#28051;涓?#37816;?#37812;?#38171;?#37719;矲ork/Join鍔?#37922;婚渶瑕?#29785;?#37712;?#28051;涓?#28729;?#38340;呯殑闂?#26864;?#38171;屼?#21978;繖涓?#37716;?#37922;诲簲璇?#29785;?#37712;?#28051;涓?#28000;涔?#37837;风殑闂?#26864;?#37723;?#38171;?/span>
    寮濮?#37827;?#38171;?#37812;?#29825;?#26473;?#28051;?#37716;?#37922;荤?#23338;簭鍘绘眰Fibonacci鏁板垪锛屼絾鍗?#29723;?#28051;?#38315;?#37510;?#37812;?#37734;?#26473;?#28051;?#38338;?#26864;?#28051;婄缁撲簡涓?#28598;?#37827;?#38338;?#38171;岀洿?#22476;鎴?#29825;?#29831;嗗埌Fibonacci鏁板垪(Fn+1=Fn+Fn-1)鏃犳硶楂樻晥鍦?#39582;惰?#23677;寲锛?#37733;?#28051;烘瘡涓?#37706;?#38318;?#28186;?#29863;?#27996;?#28729;?#37715;?#38344;殑鍊?#37510;?#37813;浠?#38171;?#37827;?#29825;?#28003;?#28641;備綍璇?#37733;?#28725;瑰叾瀹?#37826;藉?#24816;?#23677;寲锛?#28729;?#28598;?#37922;熷氨鏄?#28051;涓?#26916;哄?#24531;寲?#27537;璁?#32480;?#37510;?#37813;浠?#37812;?#37817;?#37812;愪簡鍙?#28051;涓?#38338;?#26864;?-鏌?#37813;?#37825;扮粍涓殑鏈澶?#37711;?#32497;?#38171;岃繖鏍?#28751;?#28610;戒簡銆傚湪杩?#28051;?#37716;?#37922;讳腑锛屼?#29362;彲浠?#23536;堢?#21095;鍦扮?#23338;埌濡備綍?#23047;鐢?#28051;涓殢鏈虹殑鏁板垪鍘?#29785;?#37712;?#26473;?#28051;?#38338;?#26864;?濡?#37733;?鎵绀?銆?/span>


Java Magazine锛氫綘閮?#37734;?#37725;?#38322;岃?#21449;紨杩?#26473;?#27996;涘姩鐢荤?#23338;簭锛?/span>
Grazi锛?#37734;JavaOne涓?#29825;叉紨杩囧?#29363;锛?#37734;?#37711;?#28729;冪殑璁?#28598;?#28028;?#29825;?#38171;?#28641;傚鏂?#38340;?#28051;殑JavaZone锛岃?#24539;粠涓栫殑Jazoon锛岀?#30028;害?#27537;QCon锛?#28000;強璁?#28598;歋IG(鐗瑰埆鍏磋叮?#31885;)鍜孞UG(Java鐢埛?#31885;)涓?#28052;?#38318;?#29825;叉紨杩?#37510;?#37812;戝枩娆?#29825;叉紨锛?#37812;?#28052;熷枩娆?#37723;父涓?#37923;?#38171;岃孞ava Concurrent Animated涓?#37812;?#37819;?#28186;涗簡涓涓?#37835;佸?#30028;殑鏈?#28028;?#37720;诲仛杩?#28051;?#28000;?#27996;嬫儏銆傚畠鎬?#38003;借幏寰?#37835;侀珮?#27537;璇?#28000;?#37510;?/span>
    Java Concurrent Animated?#27537;璁叉紨鎻?#28186;涗簡涓绉嶆剰璇?#38171;屽苟涓?#28729;?#28000;?#28052;?#37722;?#37713;?#29999;殑寮?#24386;鑰?#28000;睍绀轰簡涓?#26462;?#26473;?#28051;妗嗘?#21057;殑浠?#37706;?#38171;岃?#28051;?#28729;?#26473;樺睍绀轰簡锛?#28641;傛灉浣犳嫢鏈変簡妗嗘灦鍜岀?#22443;劅鍚?#26473;?#38171;屽涔?#39582;跺彂缂栫浼?#37828;?#28598;氫?#22562;殑瀹?#37828;?#37510;?/span>


]]>Java骞跺彂?#29056;纭瀹?#29882;?-鍒嗚屾?#35763;箣(鍘?http://www.8634070.com/jiangshachina/archive/2013/10/23/405577.htmlJohn JiangJohn JiangWed, 23 Oct 2013 15:27:00 GMThttp://www.8634070.com/jiangshachina/archive/2013/10/23/405577.htmlhttp://www.8634070.com/jiangshachina/comments/405577.htmlhttp://www.8634070.com/jiangshachina/archive/2013/10/23/405577.html#Feedback0http://www.8634070.com/jiangshachina/comments/commentRss/405577.htmlhttp://www.8634070.com/jiangshachina/services/trackbacks/405577.html闃?#29831;诲叏鏂?/a>

]]>Java骞跺彂?#29056;纭瀹?#29882;?-閫鍑?#28000;诲姟II(鍘?http://www.8634070.com/jiangshachina/archive/2013/10/07/404690.htmlJohn JiangJohn JiangMon, 07 Oct 2013 08:55:00 GMThttp://www.8634070.com/jiangshachina/archive/2013/10/07/404690.htmlhttp://www.8634070.com/jiangshachina/comments/404690.htmlhttp://www.8634070.com/jiangshachina/archive/2013/10/07/404690.html#Feedback3http://www.8634070.com/jiangshachina/comments/commentRss/404690.htmlhttp://www.8634070.com/jiangshachina/services/trackbacks/404690.html
Java骞跺彂?#29056;纭瀹?#29882;?-閫鍑?#28000;诲姟II
鍦?a href="http://www.8634070.com/jiangshachina/category/53896.html">鏈?#32495;诲垪
?#27537;涓?#28051;?#30226;涓?#37813;?#22570;?#27537;閫鍑?#39582;跺彂浠诲姟?#27537;鏂瑰紡閮?#37828;熀浜嶫DK 5?#31651;鍓嶇殑API锛?#37832;?#37826;囧皢浠?#32513;嶄娇鐢?#37922;盝DK 5?#32041;鍏殑骞跺彂宸?#37711;峰寘涓殑API?#28533;閫鍑?#28000;诲姟銆?2013.10.08鏈鍚庢洿鏂?

    鍦?#37832;?#32495;诲垪?#27537;鍓?#28051;?#30226;涓?#29825;茶堪?#31777;涓?#32457;?#38315;鍑?#39582;跺彂浠诲姟?#27537;鏂瑰紡--鍋?#23005;?#32510;跨锛涘彲鍙栨?#22562;殑浠诲姟锛?#28051;?#37826;?#38171;屼絾閮?#37828;熀浜嶫DK 5?#31651;鍓嶇殑API銆?#37832;瘒?#30370;浠?#32513;?#37922;盝DK 5?#32041;鍏殑java.concurrent?#23512;涓殑Future?#28533;鍙栨秷浠诲姟?#27537;鎵?#29723;?#37510;?br />
1. Future妯?#23534;?/strong>
    Future鏄?#39582;跺彂缂栫涓殑涓绉嶅父瑙?#29825;?#29825;?#22959;?#23534;?#38171;?#28729;冪浉?#32139;浜庢槸Proxy妯?#23534;?#28051;嶵hread-Per-Message妯?#23534;忕殑缁?#37722;?#37510;傚嵆锛屾?#24532;閮藉?#28056;缓涓涓?#37719;曠嫭?#27537;绾跨鍘?#37813;?#29723;?#28051;涓?#38000;?#37827;剁殑浠诲姟锛屽苟涓?#37714;涘缓涓涓狥uture瀵?#29854;?#37720;绘寔鏈?#28729;?#38340;呯殑浠诲姟瀵?#29854;?#38171;?#37734;皢?#28533;?#28214;瑕佺殑鏃跺?#37712;?#37720;昏幏鍙?#28729;?#38340;?#28000;诲姟?#27537;鎵?#29723;?#32513;撴灉銆?br />渚濈?#36346;厛鍒涘缓涓涓?#37922;?#27996;庢壂?#24383;鏂?#28000;剁殑浠诲姟FileScannerTask锛?#28641;?#28000;?#37934;佹竻鍗?鎵绀?#38171;?br />
?#31483;鍗?
public class FileScannerTask implements Runnable {

    
private File root = null;

    
private ArrayList<String> filePaths = new ArrayList<String>();

    
public FileScannerTask(File root) {
        
if (root == null || !root.exists() || !root.isDirectory()) {
            
throw new IllegalArgumentException("root must be directory");
        }

        
this.root = root;
    }

    @Override
    
public void run() {
        travleFiles(root);
    }

    
private void travleFiles(File parent) {
        String filePath 
= parent.getAbsolutePath();
        filePaths.add(filePath);

        
if (parent.isDirectory()) {
            File[] children 
= parent.listFiles();
            
if (children != null) {
                
for (File child : children) {
                    travleFiles(child);
                }
            }
        }
    }

    
public List<String> getFilePaths() {
        
return (List<String>) filePaths.clone();
    }
}
姝?#28598;勭殑鏂?#28000;舵壂?#24383;浠诲姟锛屾彁渚涗簡涓涓猤etFilePaths()鏂规硶浠厑璁搁殢鏃?#38318;藉彲浠?#37721;栧?#21700;綋鍓嶅凡鎵?#37819;忚?#22249;殑鏂?#28000;剁殑?#30718;寰??#27977;?#32139;浜?#28051;涓?#28000;诲姟蹇?#37907;?銆傜劧鍚?#38171;屽?#28056;缓涓涓?#38333;堝璇?#28000;诲姟?#27537;Future?#34987;锛?#28641;?#28000;?#37934;佹竻鍗?鎵绀?#38171;?br />
?#31483;鍗?
public class FileScannerFuture {

    
private FileScannerTask task = null;

    
public FileScannerFuture(FileScannerTask task) {
        
new Thread(task).start();
        
this.task = task;
    }

    
public List<String> getResult() {
        
return task.getFilePaths();
    }
}
FileScannerFuture?#23508;鏈FileScannerTask?#27537;?#32041;鐢?#38171;屽?#36346;?#28056;缓涓涓嫭绔嬬殑绾跨?#23339;潵鎵?#29723;岃浠诲姟銆傚湪浠诲姟?#27537;鎵?#29723;岃?#22249;涓?#38171;屽簲鐢?#32459;嬪?#24531;彲浠?#37734;?鏈潵"?#27537;?#28999;涓?#37827;跺埢鍘昏幏鍙?#28051;涓?#28000;诲姟?#27537;蹇?#37907;?#38171;?#28641;?#28000;?#37934;佹竻鍗?鎵绀?#38171;?br />
?#31483;鍗?
public static void main(String[] args) throws Exception {
    FileScannerFuture future 
= new FileScannerFuture(new FileScannerTask(new File("C:")));

    TimeUnit.SECONDS.sleep(
1);
    List
<String> filePaths1 = future.getResult();
    System.out.println(filePaths1.size());

    TimeUnit.SECONDS.sleep(
1);
    List
<String> filePaths2 = future.getResult();
    System.out.println(filePaths2.size());
}

2. ?#23047;鐢?#39582;跺彂宸?#37711;峰寘涓殑Future瀹炵幇
    鍓?#38344;?#37813;?#30541;绀虹殑Future瀹炵幇鍗佸?#21977;殑绠闄?#38171;屾病鏈?#28729;?#38340;呭簲鐢殑鎰忎箟銆備娇鐢FileScannerFuture锛屽簲鐢?#32459;嬪簭鍦?#38014;?#37721;filePaths鏃?#38171;?#37827;犳硶寰楃煡鍏惰幏鍙栫殑鏄?#37722;?#28051;?#37832;缁?#32513;撴灉锛?#37719;?#37827;犳硶鍒?#37826;璅ileScannerTask鏄?#37722;?#23480;?#32513;?#28729;?#37812;?#37510;傝?#28051;?#38171;?#28052;?#28051;?#38003;?#37734;?#36423;?#29781;佹?#36346;仠姝FileScannerTask?#27537;鎵?#29723;?#37510;傛鏃犵枒闂?#38171;?#37922;盝DK 5?#32041;鍏殑骞跺彂宸?#37711;峰寘鑲?#28729;?#28028;氭彁渚?#23005;?#32491;诲疄鐢?#23480;?#37711;?#38171;?#28641;侳utureTask銆?#28051;轰簡?#23047;鐢?#39582;跺彂宸?#37711;峰寘涓殑Future锛岄渶瑕佷慨鏀瑰?#23944;堪?#27537;FileScannerTask瀹炵幇锛岃鍏?#28729;炵幇Callable鎺?#37721;?#38171;?#28641;?#28000;?#37934;佹竻鍗?鎵绀?#38171;?br />
?#31483;鍗?
public class FileScannerTask implements Callable<List<String>> {

    
private File root = null;

    
private List<String> filePaths = new ArrayList<String>();

    
public FileScannerTask(File root) {
        
if (root == null || !root.exists() || !root.isDirectory()) {
            
throw new IllegalArgumentException("root must be directory");
        }

        
this.root = root;
    }

    @Override
    
public List<String> call() {
        travleFiles(root);
        
return filePaths;
    }

    
private void travleFiles(File parent) {
        String filePath 
= parent.getAbsolutePath();
        filePaths.add(filePath);

        
if (parent.isDirectory()) {
            File[] children 
= parent.listFiles();
            
if (children != null) {
                
for (File child : children) {
                    travleFiles(child);
                }
            }
        }
    }

    
public List<String> getFilePaths() {
        
return (List<String>) filePaths.clone();
    }
}
?#31794;鐢?#32459;嬪簭涔?#29781;佺浉?#31794;?#27537;淇?#37824;?#37812;?#28641;?#28000;?#37934;佹竻鍗?鎵绀?#38171;屼娇鐢ExecutorService?#28533;鎻?#27996;?#28000;诲姟锛屽?#36346;?#28056;缓涓涓狥uture/FutureTask瀹?#28186;?#37510;?br />
?#31483;鍗?
public static void main(String[] args) {
    ExecutorService executorService 
= Executors.newCachedThreadPool();
    Future
<List<String>> future = executorService.submit(new FileScannerTask(new File("C:")));

    
try {
        List
<String> filePaths = future.get();
        System.out.println(filePaths.size());
    } 
catch (InterruptedException e) {
        e.printStackTrace();
    } 
catch (ExecutionException e) {
        e.printStackTrace();
    }

    executorService.shutdown();
}
姝?#28598;勫氨鏄?#29835;?#37922;Future.get()鏂规?#26335;潵鑾?#37721;?#28000;诲姟?#27537;鎵?#29723;?#32513;撴灉锛?#28641;傛灉浠诲姟娌?#37832;?#37813;?#29723;?#28729;屾瘯锛?#38317;?#28052;?#29831;?#37826;规?#26334;皢浼氳闃诲銆?#29831;Future瀹炵?#25198;殑濂藉?#21227;氨鏄?#38171;屾甯告?#21613;喌涓?#38171;屽彧鏈?#37734;?#28000;诲姟鎵?#29723;?#28729;屾?#26330;箣鍚庢墠鑳借幏鍙栧叾缁撴灉锛?#28000;繚璇?#29831;?#32513;撴灉鏄?#37832;缁?#37813;?#29723;?#32513;撴灉銆?br />
3. ?#23047;鐢Future鍙栨秷浠诲姟
    Future闄簡瀹氫箟鏈夊彲鑾?#37721;?#37813;?#29723;?#32513;撴灉?#27537;get鏂规硶(get()浠強get(long timeout, TimeUnit unit))锛岃繕瀹氫箟?#31777;涓?#28051;?#37826;规硶锛cancel()锛宨sCancelled()浠強isDone()锛?#37922;?#27996;庡?#26664;秷浠诲姟锛?#28000;強鍒?#28729;?#28000;诲姟鏄?#37722;?#23480;?#29722;?#37721;栨秷銆佸凡鎵?#29723;?#28729;屾瘯銆傚浠?#37934;佹竻鍗?鎵绀?#38171;?br />
?#31483;鍗?
public interface Future<V> {

    
boolean cancel(boolean mayInterruptIfRunning);
    
boolean isCancelled();
    
boolean isDone();
    
}
鍏?#28051;?#38171;宑ancel()鏂规硶涓殑boolean鍙傛暟?#23266;涓簍rue锛岃绀?#37734;?#37721;栨秷璇?#28000;诲姟鏃?#38171;岃嫢鎵?#29723;岃浠诲姟?#27537;绾跨浠嶅湪杩?#29723;?#28051;?#38171;屽垯瀵瑰叾杩?#29723;?#28051;?#37826;?#37510;傚浠?#37934;佹竻鍗?鎵绀?#38171;岃嫢浠诲姟鎵?#29723;岃秴鏃朵簡锛?#38317;?#28052;?#28751;?#37721;栨秷瀹?#37510;?br />
?#31483;鍗?
public static void main(String[] args) {
    ExecutorService executorService 
= Executors.newCachedThreadPool();
    Future
<List<String>> future = executorService.submit(new FileScannerTask(new File("C:")));

    
try {
        List
<String> filePaths = future.get(1, TimeUnit.SECONDS);
        System.out.println(filePaths.size());
    } 
catch (InterruptedException e) {
        e.printStackTrace();
    } 
catch (ExecutionException e) {
        e.printStackTrace();
    } 
catch (TimeoutException e) {
        e.printStackTrace();
    } 
finally {
        future.cancel(
true);
    }

    executorService.shutdown();
}
鍦?#28729;?#38340;呭簲鐢?#28051;?#38171;屽?#26664;秷浠诲姟?#27537;鍘?#37922;?#38002;?#28729;?#28051;?#28000;?#28000;呭彧鏄秴鏃?#26473;?#28052;堢畝鍗?#38171;岃?#27194;彲鑳?#37828;?#37922;变簬鎺?#37721;楀埌?#31777;鐢埛?#27537;?#23514;浠?#37510;傛鏃?#38171;屽垯?#24434;鑳?#28028;?#28000;庡彟涓涓嫭绔嬬?#36328;鍘诲?#26664;秷璇?#28000;诲姟銆傞櫎?#31777;鍙栨秷浠诲姟?#31651;澶?#38171;?#37832;?#37827;?#26473;橀渶瑕佸?#26663;嚭浠诲姟涓?#23480;?#32513;?#37922;?#37812;愮殑?#20788;鍒?#32513;撴灉銆備絾涓轰簡鑳藉?#29111;?#23941;簲浠诲姟?#27537;閫鍑?#38171;岄?#26663;?#22565;渶瑕佷慨鏀笷ileScannerTask锛屼娇寰楀綋浠诲姟琚?#37721;栨秷(涓?#37826;?鏃?#38171;?#28000;诲姟鑳藉?#29114;?#29112;?#27537;蹇?#38315;熷仠姝?#39582;?#26473;?#37733;?#38171;?#28641;?#28000;?#37934;佹竻鍗?鎵绀?#38171;?br />
?#31483;鍗?
public class FileScannerTask implements Callable<List<String>> {

    

    
private void travleFiles(File parent) {
        
if (Thread.currentThread().isInterrupted()) {
            
return;
        }

        String filePath 
= parent.getAbsolutePath();
        filePaths.add(filePath);

        
if (parent.isDirectory()) {
            File[] children 
= parent.listFiles();
            
if (children != null) {
                
for (File child : children) {
                    travleFiles(child);
                }
            }
        }
    }

    
}
?#27977;?#31794;鍦?#28103;?#37824;瑰簲鐢?#32459;嬪?#24533;殑浠爜锛?#28641;?#28000;?#37934;佹竻鍗?鎵绀?#38171;?/div>
?#31483;鍗?
public static void main(String[] args) {
    ExecutorService executorService 
= Executors.newCachedThreadPool();
    FileScannerTask task 
= new FileScannerTask(new File("C:"));
    
final Future<List<String>> future = executorService.submit(task);
    
    
new Thread(new Runnable() {
        
        @Override
        
public void run() {
            
try {
                TimeUnit.SECONDS.sleep(
1);
            } 
catch (InterruptedException e) {
                e.printStackTrace();
            }
            future.cancel(
true);
        }
    }).start();
    
    
try {
        List
<String> filePaths = future.get();
        System.out.println(filePaths.size());
    } 
catch (InterruptedException e) {
        e.printStackTrace();
    } 
catch (ExecutionException e) {
        e.printStackTrace();
    } 
catch (CancellationException e) {
        List
<String> filePaths = task.getFilePaths();
        System.out.println(
"Partly result: " + filePaths.size());
    }
    
    executorService.shutdown();
}
鐢?#28051;婂彲?#29025;锛屾澶勪娇鐢Future.cancel(true)?#27537;鏈?#29840;?#28186;濈?#33333;槸鍒?#37922;簡绾跨?#23340;殑涓?#37826;?#37832;?#37714;?#37510;?br />
4. 灏?#32513;?/strong>
    ?#23047;鐢Future?#24434;浠?#37734;?#28000;诲姟鍚?#37716;箣鍚庣殑鐗?#28729;氭?#33333;満鍐?#37720;昏幏鍙?#28000;诲姟?#27537;鎵?#29723;?#32513;撴灉銆?#37922;盝DK 5?#32041;鍏殑骞跺彂宸?#37711;峰寘涓?#37819;?#28186;涚殑Future瀹炵幇涓?#28000;呭彲浠?#38014;?#37721;?#28000;诲姟?#27537;鎵?#29723;?#32513;撴灉锛岃?#27194;彲浠?#37922;?#27996;庡?#26664;秷浠诲姟?#27537;鎵?#29723;?#37510;?/div>

]]>Java骞跺彂?#29056;纭瀹?#29882;?-閫鍑?#28000;诲姟I(鍘?http://www.8634070.com/jiangshachina/archive/2013/09/21/404269.htmlJohn JiangJohn JiangSat, 21 Sep 2013 11:11:00 GMThttp://www.8634070.com/jiangshachina/archive/2013/09/21/404269.htmlhttp://www.8634070.com/jiangshachina/comments/404269.htmlhttp://www.8634070.com/jiangshachina/archive/2013/09/21/404269.html#Feedback0http://www.8634070.com/jiangshachina/comments/commentRss/404269.htmlhttp://www.8634070.com/jiangshachina/services/trackbacks/404269.html
Java骞跺彂?#29056;纭瀹?#29882;?-閫鍑?#28000;诲姟I
璁?#37714;?#37712;?#28051;涓?
Java骞跺彂?#29056;纭瀹?#29882;?/a>"绯诲垪锛岀畻浣?#37832;?#27996;?#28725;笿ava骞跺彂瀛?#28052;?#28051;?#28729;?#29882;电殑绠鍗?#37804;荤粨銆?#37832;?#37826;?#37828;?#29831;?#32495;诲垪?#27537;绗?#28051;?#30226;锛?#28000;?#32513;嶄簡閫鍑?#39582;跺彂浠诲姟?#27537;鏈绠鍗?#37826;规硶銆?2013.09.25鏈鍚庢洿鏂?

鍦?#28051;涓?#39582;跺彂浠诲姟琚?#37722;?#37716;箣鍚?#38171;?#28051;?#29781;?#37832;?#37832;?#28729;?#37804;?#37828;?#28028;氭墽琛?#28729;?#37812;?#37510;?#37922;变?#24226;椂闂撮檺鍒?#38171;岃?#21228;簮闄?#37714;?#38171;?#37922;埛鎿?#28003;?#38171;?#37922;氳嚦鏄?#28000;诲姟涓殑寮傚父(灏?#37711;舵槸杩?#29723;?#37827;跺?#20634;父)锛?..閮藉彲鑳?#38315;?#37812;?#28000;诲姟涓?#38003;?#37813;?#29723;?#28729;?#37812;?#37510;傚?#20633;綍鎭板綋鍦?#38315;鍑?#28000;诲姟鏄?#28051;涓?#23536;?#29999;?#29785;佺殑闂?#26864;?#38171;岃?#28051;?#28729;炵幇鏂规硶涔?#28051;?#28051;鑰岃冻銆?/span>

1. 浠诲姟
鍒涘缓涓涓?#39582;跺彂浠诲姟锛?#38315;?#35120;?#37734;拌幏鍙栨寚瀹?#37929;?#35120;?#28051;嬬殑鎵鏈?#28699;愮洰褰?#28051;?#37826;?#28000;剁殑缁?#28725;硅矾寰?#38171;?#37832;鍚庡?#23941;皢杩?#27996;涜矾寰勪俊?#20229;?#32346;瀛樺埌涓涓?#37826;?#28000;?#28051;?#38171;?#28641;?#28000;?#37934;佹竻鍗?鎵绀?#38171;?/span>
?#31483;鍗?
public class FileScanner implements Runnable {

    
private File root = null;

    
private List<String> filePaths = new ArrayList<String>();

    
public FileScanner1(File root) {
        
if (root == null || !root.exists() || !root.isDirectory()) {
            
throw new IllegalArgumentException("root must be legal directory");
        }

        
this.root = root;
    }

    @Override
    
public void run() {
        travleFiles(root);
        
try {
            saveFilePaths();
        } 
catch (Exception e) {
            e.printStackTrace();
        }
    }

    
private void travleFiles(File parent) {
        String filePath 
= parent.getAbsolutePath();
        filePaths.add(filePath);

        
if (parent.isDirectory()) {
            File[] children 
= parent.listFiles();
            
for (File child : children) {
                travleFiles(child);
            }
        }
    }

    
private void saveFilePaths() throws IOException {
        FileWriter fos 
= new FileWriter(new File(root.getAbsoluteFile()
                
+ File.separator + "filePaths.out"));
        
for (String filePath : filePaths) {
            fos.write(filePath 
+ "\n");
        }
        fos.close();
    }
}

2. 鍋?#23005;?#32510;跨
鏈?#28051;涓?#23536;堢洿鎺?#38171;?#28052;熷?#22557;?#33590;?#21977;殑鏂瑰紡?#28533;鍋?#23005;?#32510;跨锛屽氨鏄?#29835;?#37922;Thread.stop()鏂规硶锛?#28641;?#28000;?#37934;佹竻鍗?鎵绀?#38171;?/span>
?#31483;鍗?
public static void main(String[] args) throws Exception {
    FileScanner task 
= new FileScanner(new File("C:"));
    Thread taskThread 
= new Thread(task);
    taskThread.start();

    TimeUnit.SECONDS.sleep(
1);
    taskThread.stop();
}
浣?#37828;?#38171;?#37734;扮悆浜?#38318;界煡?#20158;Thread.stop()鍦?#23536;?#28052;呭緢涔呬箣鍓嶅氨涓?#37818;?#38013;愪娇鐢簡銆?#37837;规嵁
瀹?#37826;?#37826;囨?#27537;浠?#32513;?#38171;岃鏂规硶瀛?#37734;潃鍥?#37832;夌殑涓?#28729;?#37711;?#37804;?#37510;傚綋鍋?#23005;?#32510;跨鏃?#38171;屽皢浼?#38322;?#37824;?#29831;?#32510;跨鎵鍗?#37832;夌殑鍏儴鐩?#29785;嗛攣锛岃繖灏?#28028;?#38315;?#37812;?#37721;?#26473;?#27996;?#38335;佷繚鎶殑瀵?#29854;殑涓?#28051;鑷?#37804;?#37510;傚湪鎵?#29723;屾竻鍗??#27537;?#31794;鐢?#32459;嬪簭鏃?#38171;?#28729;冪殑杩?#29723;?#32513;撴灉鏄?#28051;嶇瀹氱殑銆傚畠?#24434;鑳?#28028;?#26440;撳嚭涓涓?#37826;?#28000;?#38171;屽叾涓寘鍚儴鍒嗙殑琚?#37813;?#37819;忚?#22249;殑鐩?#35120;?#37724;?#37826;?#28000;?#37510;備絾瀹?#28052;熷緢鏈夊彲鑳?#28000;涔?#28052;?#28051;?#26440;撳嚭锛?#37733;?#28051;?#37734;?#37813;?#29723;孎ileWriter.write()?#27537;杩囩涓?#38171;屽彲鑳?#37922;变簬绾跨?#23338;仠姝?#38000;?#38315;?#37812;愪簡I/O寮傚父锛屼娇寰?#37832;缁?#37827;犳硶寰楀埌杈撳嚭鏂?#28000;?#37510;?/span>

3. ?#24434;鍙栨?#22562;殑浠诲姟
鍙?#28598;?#28051;绉嶅?#20344;垎甯?#29785;佺殑閫?#23536;勬槸锛?#37734;?#29825;?#29825;箣鍒?#38171;?#37812;?#28000;?#28751;变娇浠诲姟鏄彲琚?#37721;栨?#22562;殑銆?#28051;鑸?#37734;?#38171;屽氨鏄?#37819;?#28186;?#28051;涓?#37721;栨?#22559;爣蹇楁垨璁?#28729;?#28051;涓?#37721;栨秷鏉?#28000;?#38171;?#28051;鏃?#28000;诲姟閬囧埌璇爣蹇楁垨?#24359;瓒充簡鍙栨秷鏉?#28000;?#38171;屽氨浼?#32513;?#37833;?#28000;诲姟?#27537;鎵?#29723;?#37510;傚浠?#37934;佹竻鍗?鎵绀?#38171;?/span>
?#31483;鍗?
public class FileScanner implements Runnable {

    
private File root = null;

    
private List<String> filePaths = new ArrayList<String>();

    
private boolean cancel = false;

    
public FileScanner(File root) {
        
    }

    @Override
    
public void run() {
        
    }

    
private void travleFiles(File parent) {
        
if (cancel) {
            
return;
        }

        String filePath 
= parent.getAbsolutePath();
        filePaths.add(filePath);

        
if (parent.isDirectory()) {
            File[] children 
= parent.listFiles();
            
for (File child : children) {
                travleFiles(child);
            }
        }
    }

    
private void saveFilePaths() throws IOException {
        
    }

    
public void cancel() {
        cancel 
= true;
    }
}
鏂扮殑FileScanner瀹炵幇鎻?#28186;?#28051;涓猚ancel?#29219;蹇?#38171;宼ravleFiles()浼氶亶鍘?#37826;扮殑鏂?#28000;朵箣鍓嶆娴?#29831;爣蹇?#38171;岃嫢璇爣蹇?#28051;簍rue锛屽垯浼?#32468;?#37719;?#26473;?#37733;?#37510;?#28000;?#37934;佹竻鍗?鏄娇鐢?#37826;?#28000;诲姟?#27537;?#31794;鐢?#32459;嬪簭銆?/span>
?#31483;鍗?
public static void main(String[] args) throws Exception {
    FileScanner task 
= new FileScanner(new File("C:"));
    Thread taskThread 
= new Thread(task);
    taskThread.start();

    TimeUnit.SECONDS.sleep(
3);
    task.cancel();
}
浣?#37832;変簺鏃跺欎娇鐢彲鍙栨?#22562;殑浠诲姟锛屽苟涓?#38003;藉揩閫?#37734;?#38315;鍑?#28000;诲姟銆?#37733;?#28051;?#28000;诲姟鍦?#22955;娴嬪?#26664;?#22559;爣蹇椾箣鍓?#38171;屽彲鑳芥澶?#27996;?#32475;?#23536;?#37912;舵?#38171;?#37922;氳?#20914;彲鑳?#29722;?#38339;诲?#28853;潃銆傚?#35268;竻鍗?涓殑FileScanner绋?#28003;?#28103;?#37824;?#38171;岃姣忔璁?#38338;?#37826;扮殑鏂?#28000;朵箣鍓嶅?#22562;潯鐪?0绉?#38333;?#38171;?#28641;?#28000;?#37934;佹竻鍗?鎵绀?#38171;?/span>
?#31483;鍗?
public class FileScanner implements Runnable {

    

    
private void travleFiles(File parent) {
        
try {
            TimeUnit.SECONDS.sleep(
10);
        } 
catch (InterruptedException e) {
            e.printStackTrace();
        }

        
if (cancel) {
            
return;
        }

        
    }

    
private void saveFilePaths() throws IOException {
        
    }

    
public void cancel() {
        cancel 
= true;
    }
}
鍐?#37813;?#29723;屾竻鍗?涓殑?#31794;鐢?#32459;嬪簭鏃?#38171;屽彲鑳藉彂?#24135;浠诲姟骞舵病鏈?#23536;堝揩閫熺殑閫鍑?#38171;岃?#37828;?#37721;?#32475;?#23536;呬簡澶害7绉?#38333;?#37813;?#38315;鍑?#37510;傚?#20635;灉鍦?#22955;鏌cancel?#29219;蹇椾箣鍓?#29781;佸?#22563;幏鍙栨煇涓?#37721;?#38335;佷繚鎶殑璧勬簮锛?#38317;?#28052;?#29831;?#28000;诲姟灏?#28028;氳闃诲锛屽苟涓?#37827;犳?#26336;瀹氫綍鏃?#38003;藉閫鍑?#37510;傚浜?#26473;?#32457;嶆?#21613;喌锛屽氨?#28214;瑕佷娇鐢?#28051;?#37826;簡銆?/span>

4. 涓?#37826;?/span>
涓?#37826;?#37828;?#28051;绉嶅崗浣?#37832;?#37714;?#38171;?#28729;?#39582;?#28051;?#28028;氱?#29112;鍦板仠姝?#28051;涓?#32510;跨锛岃屽彧鏄?#37819;愰?#25492;?#36328;?#23342;渶瑕佽涓?#37826;?#38171;屽?#36346;皢绾跨?#23340;殑涓?#37826;?#37912;舵?#29825;剧疆涓簍rue銆傚?#20635;灉绾跨姝?#37734;?#37813;?#29723;?#28051;浜涘彲鎶涘嚭InterruptedException?#27537;鏂规硶锛?#28641;俆hread.sleep()锛孴hread.join()鍜孫bject.wait()锛?#38317;?#28052;堝綋绾跨琚?#28051;?#37826;?#37827;?#38171;?#28051;婅堪鏂规硶灏?#28028;氭?#28056;嚭InterruptedException锛屽苟涓?#28051;?#37826;?#37912;舵佷細琚噸鏂?#29825;剧疆涓false銆?#28000;诲姟绋嬪簭鍙?#29781;佹?#26495;綋澶勭悊璇?#23534;傚父锛屽氨?#24434;浠?#23005;?#29999;?#37734;?#38315;鍑?#28000;诲姟銆傚?#35268;竻鍗?鍐嶇浣?#28103;?#37824;?#38171;?#37719;?#38171;?#28641;傛灉浠诲姟鍦潯鐪?#37827;堕亣涓婁簡InterruptedException锛?#38317;?#28052;?#28751;?#37721;栨秷浠诲姟銆傚浠?#37934;佹竻鍗?鎵绀?#38171;?/span>
?#31483;鍗?
public class FileScanner implements Runnable {

    

    
private void travleFiles(File parent) {
        
try {
            TimeUnit.SECONDS.sleep(
10);
        } 
catch (InterruptedException e) {
            cancel();
        }

        
if (cancel) {
            
return;
        }

        
    }

    
}
鍚?#37827;跺皢?#31483;鍗?涓殑?#31794;鐢?#32459;嬪簭锛屾鏃跺皢璋?#37922;Thread.interrupt()鏂规硶鍘讳腑鏂?#32510;跨锛?#28641;?#28000;?#37934;佹竻鍗?鎵绀?#38171;?/span>
?#31483;鍗?
public static void main(String[] args) throws Exception {
    FileScanner3 task 
= new FileScanner3(new File("C:"));
    Thread taskThread 
= new Thread(task);
    taskThread.start();

    TimeUnit.SECONDS.sleep(
3);
    taskThread.interrupt();
}
?#22440;鑰呮洿杩?#28051;姝?#38171;?#28000;呬娇鐢?#28051;?#37826;?#37912;舵佹潵鎺?#37714;剁?#23338;?#24533;殑閫鍑?#38171;岃?#28051;嶅?#23940;娇鐢彲鍙栨?#22562;殑浠诲姟(鍗?#38171;屽垹闄cancel?#29219;蹇?锛屽皢?#31483;鍗?涓殑FileScanner淇?#37824;?#37812;?#28641;?#28051;?#38171;?/span>
?#31483;鍗?
public class FileScanner implements Runnable {

    

    
private void travleFiles(File parent) {
        
try {
            TimeUnit.SECONDS.sleep(
10);
        } 
catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        
if (Thread.currentThread().isInterrupted()) {
            
return;
        }

        
    }

    
}
鍐嶆鎵?#29723;屾竻鍗??#27537;?#31794;鐢?#32459;嬪簭鍚?#38171;?#37826;扮殑FileScanner涔?#38003;?#37719;?#37827;剁殑閫鍑轰簡銆傚?#23536;楁敞鎰忕殑鏄?#38171;?#37733;?#28051;哄綋sleep()鏂规硶鎶涘嚭InterruptedException鏃?#38171;岃绾跨?#23340;殑涓?#37826;?#37912;舵佸?#21975;張浼氳璁剧疆涓false锛?#37813;浠?#36423;呴瑕佸?#23942;璋?#37922;interrupt()鏂规?#26335;潵?#32346;瀛?#28051;?#37826;?#37912;舵?#38171;岃繖鏍?#37734;?#37722;?#38344;?#37813;嶅彲浠?#37714;?#37922;?#28051;?#37826;?#37912;舵佹潵鍒?#28729;?#37828;?#37722;渶瑕?#26473;?#37733;瀟ravleFiles()鏂规硶銆傚綋?#21159;锛屽浜庢澶勭殑渚嬪瓙锛?#37734;敹?#22476;InterruptedException鏃?#28052;熷彲浠?#38315;夋嫨?#27967;鎺?#26473;?#37733;?#38171;?#28641;?#28000;?#37934;佹竻鍗?鎵绀?#38171;?/span>
?#31483;鍗?
public class FileScanner implements Runnable {

    

    
private void travleFiles(File parent) {
        
try {
            TimeUnit.SECONDS.sleep(
10);
        } 
catch (InterruptedException e) {
            
return;
        }

        
    }

    
}

5 灏?#32513;?/span>
鏈?#37826;?#28000;?#32513;嶄簡涓?#32457;嶇畝鍗曠殑閫鍑?#39582;跺彂浠诲姟?#27537;鏂规硶锛氬仠姝?#32510;跨锛涗娇鐢彲鍙栨秷浠诲姟锛涗娇鐢?#28051;?#37826;?#37510;傛鏃犵枒闂?#38171;屽仠姝?#32510;跨鏄?#28051;嶅彲鍙栫殑銆備娇鐢彲鍙栨?#22562;殑浠诲姟鏃?#38171;岃?#20352;伩鍏?#28000;诲姟鐢变簬琚?#38339;诲鑰?#37827;犳?#26334;強鏃?#38171;?#37922;氳嚦姘?#26473;?#37827;犳硶琚?#37721;栨秷銆?#28051;鑸?#37734;?#38171;屾?#26495;綋鍦颁娇鐢?#28051;?#37826;?#37828;?#37721;栨秷浠诲姟?#27537;棣?#38315;?#37826;瑰紡銆?/span>


]]>涓?#28003;曞枩娆?#37734;ThoughtWorks宸?#28003;?璇?http://www.8634070.com/jiangshachina/archive/2013/08/22/403156.htmlJohn JiangJohn JiangThu, 22 Aug 2013 06:43:00 GMThttp://www.8634070.com/jiangshachina/archive/2013/08/22/403156.htmlhttp://www.8634070.com/jiangshachina/comments/403156.htmlhttp://www.8634070.com/jiangshachina/archive/2013/08/22/403156.html#Feedback3http://www.8634070.com/jiangshachina/comments/commentRss/403156.htmlhttp://www.8634070.com/jiangshachina/services/trackbacks/403156.html
涓?#28003;曞枩娆?#37734;ThoughtWorks宸?#28003;?/span>
鏈?#37826;?#37828;疶houghtWorks棣?#29999;?#32457;?#28699;?#28729;禡artin Fowler鎺?#37721;InformIT閲?#29825;?#37827;剁殑
璁胯皥褰?/a>锛岃?#22557;埌?#31777;鍦ThoughtWorks宸?#28003;滅殑鎯呭喌锛屽?#31566;houghWorks鎰熷?#30923;叮?#27537;鏈嬪弸浠彲浠?#37930;嬬湅銆?2013.08.23鏈鍚庢洿鏂?

Martin Fowler鍒?#27996;簡浠栧叧浜嶵houghtWorks?#27537;鐪?#23049;?#38171;岃繖鏄?#28051;瀹跺叧娉彲鎸佺画鎬?#28000;強缁?#23092;?#37724;?#32448;?#28028;氬叕姝殑杞?#28000;跺紑?#24386;鍏?#37721;?#37510;侻artin璋堝埌?#31777;浠?#37828;?#28641;備綍寮濮嬩簡鍦ThoughtWorks?#27537;宸?#28003;?#38171;?#28000;?#28051;?#28000;涔?#28028;氬枩娆?#26473;?#28729;跺叕鍙哥殑鏂囧寲锛?#28000;強瀵?#37826;扮殑杞?#28000;跺紑?#24386;鑰呯殑?#32531;璁?#37510;?/span>

InformIT锛?/strong>浣?#37828;?#37804;庢牱寰楀埌鍦?a href="http://www.thoughtworks.com/">ThoughtWorks
?#27537;宸?#28003;?#38171;?/span>
Martin Fowler锛?/strong>鍥寸粫涓涓綋鏃?#28000;?#28000;?#23005;?#28000;?#27996;嬬殑椤?#37929;殑鍩熸鍨?#38171;?#28000;?#28000;?#29825;?#37812;戝?#27691;簺鍜?#29831;?#23480;?#28003;?#37510;?#37812;?#28000;?#26473;涘?#26336;殑涓嶉敊锛?#37832;鍚庢垜灏辫嚜?#21159;鑰岀劧鍦?#37716;?#37711;?#26473;涙潵?#31777;銆傚湪姝?#37832;?#38338;?#38171;?#28000;?#28000;?#28598;?#23005;?#37734;?#26462;?#37722;?#37835;侀?#24878;?#26667;杩?#28051;杞?#28000;跺紑?#24386;椋庢牸銆傜害涔?#28051;?#37832;?#37722;?#38171;?#28000;?#28000;?#37722;?#37812;?#37819;?#28186;涗簡涓浠給ffer銆?#37922;变簬浠?#28000;?#37828;?#37812;?#37832;?#26537;娆殑瀹埛锛?#37812;?#28751;?#37712;?#28729;氬姞鍏?#28000;?#28000;?#37510;?/span>

InformIT锛?/strong>浣?#37734;?#38317;?#37710;?#23480;?#28003;?#28598;?#38336;?#37827;?#38338;翠簡锛?/span>
Martin Fowler锛?/strong>12骞?#37510;?/span>

InformIT锛?/strong>ThoughtWorks涓庡叾瀹?#37711;?#37721;?#37832;変綍涓?#37722;?#38171;?/span>
Martin Fowler锛?/strong>鏍?#37832;?#28051;?#38171;岃繖绉?#28051;?#37722;岃?#20344;綊缁?#28051;?#27996;?#37510;?#28000;?#28000;杽浜?#38342;?#28003;?#37827;?#38001;?#37828;庡張涔?#27996;?#37722;?#28003;滅殑浜?#37510;?#37911;瑰埆鏄?#38171;岃繖閲?#28028;氭洿澶?#37734;?#28000;?#23005;?#37929;寸殑鎬佸害鍘诲叧娉?#27996;?#37510;?#37812;戝彂?#24135;锛岀浉?#30254;浜?#26473;?#37720;诲?#27692;勾涓?#37711;变簨杩囩殑澶?#28598;氭暟瀹埛锛?#37812;戞洿鍔?#28103;?#28000;?#37812;戠殑鍚?#27996;?#37510;傝繕鏈?#28725;?#28729;?#37812;愰?#27199;川閲?#23480;?#28003;滃?#33333;湡鏈涘?#27692;埌?#27967;濂界殑鐪熸?#27697;儹?#20751;锛屽浜庡儚鎴?#26473;?#37837;风殑浣?#38000;?#38171;岃繖鏄?#37835;佸?#30028;殑绱?#37833;?#37510;?/span>

InformIT锛?/strong>瀵?#27996;?#37734;?#38317;?#37710;?#23480;?#28003;?#38171;屼綘鏈?#26537;娆殑鏄?#28000;涔?#38171;?/span>
Martin Fowler锛?/strong>ThoughtWorks鏈?#29825;?#28598;?#28051;滆?#22446;彲浠?a href="http://martinfowler.com/">鍘诲啓銆傚彧鏄?#37733;伴?#21477;箣澶?#37734;?#27996;?#38171;?#37812;戣?#20345;寫鍝?#37735;楀効鍘?#29825;茶堪鍛?#37510;?/span>

InformIT锛?/strong>宸?#28003;?#37734;ThoughtWorks锛屼綘鏈?#22172;璞殑鏄?#28000;涔?#38171;?/span>
Martin Fowler锛?/strong>鎴?#37832;?#22172;璞殑鏄?#38171;?#37812;?#28000;?#28000;?#28051;瀹跺?#29365;櫨浜虹殑缇?#37733;藉叕鍙?#37812;?#38336;?#28051;?#37734;?#28051;?#37923;岃寖鍥?#37712;呮嫢鏈?#28051;?#37719;?#27996;虹殑鍏?#37721;?#38171;岃?#28051;?#37720;熷?#20343;?#23941;姩鍦颁繚鎸佷簡鍏?#37721;?#37826;囧寲涓殑绮?#26946;?#37510;?#37812;?#28051;嶇瀹?#37812;?#37734;?#37711;?#28051;?#37813;?#23125;斾簡浠涔?#29785;?#38009;?#38171;屼?#21978;繖閲?#28051;?#27967;鏄?#37812;戝枩娆?#23480;?#28003;滅殑鍦?#37826;?#38171;屽苟涓?#37711;?#37721;?#28051;?#27967;鍦?#28729;?#28028;?#37812;?#38171;屼娇澶?#28729;?#38003;?#37832;?#37711;磋叮涓嶵houghtWorks鍗?#28003;?#38171;?#37812;?#28725;?#23005;?#37808;熷埌?#29678;鍏?#37510;?/span>
灏?#32480;?#28641;傛锛?#37812;?#28000;?#28051;?#38003;借偗瀹?#26473;?#32457;嶆?#21613;喌鏄?#37722;?#29781;?#28598;?#28598;?#37734;?#35120;?#37716;?#27996;庢垜銆傚湪涓庢垜鏈?#27996;烘洿鏄庣?#27977;鍏崇殑宸?#28003;?#28051;?#38171;?#37812;戝?#21620;?#35826;緱璇?#38171;?#37812;?#23536;堥珮鍏?#37734;?#26473;?#37720;诲?#20344;勾涓缓绔嬩簡martinfowler.com銆?#28051;轰簡鍏?#23049;?#28000;?#37722;?#37734;?#37824;?#26473;?#26473;?#28051;?#32468;欑偣鏃舵墍鍋氱殑浜嬫儏锛岃绔欑?#29808;凡鎴?#28051;?#28051;涓?#28051;?#28725;岀殑璧勬簮鍜屾?#21578;?#25492;殑杩?#26864;?#37510;?/span>
鍦洿?#28214;瑕?#37719;?#28003;滅殑宸?#28003;滃?#23942;部锛?#37812;戠瀹?#23536;堥珮鍏?#37734;扮?#23338;埌鎴戠殑涓浜?#37722;?#27996;嬪凡缁?#37812;?#28051;?#28051;氬唴涓捐冻杞?#38322;嶇殑"澶槾"銆?#37812;?#28051;?#29825;?#28051;?#37812;?#37734;?#26473;?#37711;?#28051;?#37832;?#23536;?#28598;殑浣?#37922;?-鎴?#32513;?#27996;堢殑浠讳綍甯?#37716;?#38003;藉杞?#37828;?#37734;拌秴杩?#28000;?#28000;嚜?#38889;?#27537;鍔?#37716;?-浣嗚?#27409;瀹?#37828;?#37812;?#37832;鎯?#37707;氱殑璐尞銆?/span>

InformIT锛?/strong>涓?#28000;涔?#28028;?#37832;変?#28888;兂?#28483;鍘籘houghtWorks宸?#28003;?#38171;?/span>
Martin Fowler锛?/strong>瀵?#27996;?#32513;?#26976;屽?#25120;殑浜?#38171;?#37812;?#37807;?#37832;澶殑鍚稿紩鍔?#37828;?#38171;岃兘鍦?#29825;?#28598;?#28051;?#37722;岀?#35826;?#23340;殑椤?#37929;?#28051;?#28699;?#28028;氬?#27692;杞?#28000;跺紑?#24386;?#27537;鑳藉姏銆傚綋?#21159;锛孴houghtWorks?#27537;椤?#37929;?#39582;堕潪瀹岀編锛屼絾鎴?#29825;?#28051;?#28729;?#28000;瘮缁?#28598;?#28598;氭暟杞?#28000;堕鐩?#29781;佸?#34249;緱澶?#37510;?#37812;?#37722;?#26473;?#23536;?#28598;ThoughtWorks鍓嶅憳宸?#28000;?#29835;堝埌浠?#28000;?#37734;?#37711;?#37721;?#23480;?#28003;滅殑宀?#37832;?#28051;?#28725;?#27996;?#26462;?#28000;跺紑?#24386;瀛埌?#31777;璁?#28598;?#37510;?/span>
鑰?#37734;?#37813;鏈夌殑鍘?#37733;犲綋涓?#38171;?#37827;?#29723;?#37832;?#28028;氫篃鏄?#23536;堥噸瑕佺殑銆傚?#20635;灉浣犳兂鑺?#28051;?#28051;澶?#23048;?#37827;?#38338;?#37734;?#28051;?#37923;?#28051;?#28051;?#37722;岀殑鍦?#37718;?#26473;?#29723;屽伐浣?#38171;屼緥濡傚湪缇?#37733;?#27996;?#37734;?#37719;?#25652;?#23480;?#28003;?#38171;屾垨宸磋浜?#37734;?#28051;?#37733;藉伐浣?#38171;?#38317;?#28052;ThoughtWorks鎻?#28186;涗簡澶?#38322;忕殑姝被鏈?#28028;?#37510;傝繖涔?#28051;庢垜浠?#37827;?#37929;?#37711;?#36423;?#32448;?#28028;氭?#31647;?#27537;闂?#26864;?#37832;?#37711;?#38171;?#37812;?#29825;?#28051;?#28725;?#27996;?#29825;?#28598;?#37832;夌粡楠岀殑浜烘潵璇?#26473;?#28052;?#37828;?#28051;涓噸瑕佺殑鍥?#32497;?#37510;?/span>

InformIT锛?/strong>瀵?#27996;?#28051;涓?#37714;氬?#27692;紑濮?#37734;ThoughtWorks宸?#28003;滅殑鏂?#37723;?#23480;?#38171;屼綘鏈?#28000;涔堝缓璁?#37722;?#38171;?/span>
Martin Fowler锛?/strong>瀵?#27996;?#37734;ThoughtWorks宸?#28003;滅殑浜?#28000;?#38171;?#37832;娌?#28051;殑浜嬫儏?#31651;涓灏?#37828;?#37812;?#28000;?#28051;嶅?#27699;亴涓?#29785;勫垝锛岃繖灏?#28028;氬緢瀹?#37828;?#37719;?#38340;?#37734;?#37734;?#26916;?#37929;?#38338;存?#20635;潵婕?#37720;?#37510;傚浜?#37832;変簺浜烘潵璇?#26473;?#28051;?#37828;?#38338;?#26864;?#38171;屼絾濡傛灉浣犳兂璁?#28729;?#28051;涓?#38001;?#28051;?#37826;?#37722;?#38171;?#38317;?#28052;?#28003;犲?#21620;?#35826;緱?#22172;宸?#37720;诲仛銆傝繖?#21104;鍛崇潃澶?#38322;忕殑浜?#38340;?#38171;屽鎵?#37832;洪亣锛屽苟绉?#37835;?#37818;?#26473;?#37510;傝繖涓?#37828;?#28051;绉嶇洿鎺殑閫?#23536;?#38171;屼絾鍦?#37812;?#37812;?#28051;虹嫭绔?#37724;?#29831;?#29999;堜箣鍓?#38171;屾病鏈?#26473;?#32457;?#28103;?#26473;?#37812;?#37715;?#29723;岀殑鑱?#28051;?#29825;?#37714;?#38171;屽氨浼?#37832;夌浉鍙嶇殑鏁堟灉銆?/span>

InformIT锛?/strong>鍛?#29831;?#37812;?#28000;?#28051;涓?鍙?#28028;氬彂鐢?#37734;ThoughtWorks"?#27537;鏁?#27996;?#37510;?/span>
Martin Fowler锛?/span>鎴?#29825;?#23536;?#37832;夋琚?#37815;?#37720;?#29825;?#29825;?#28051;涓?#37724;孊igCo?#27977;鍏崇殑寰?#37832;?#37715;嶆櫙?#27537;椤?#37929;?#37510;傝?#27409;瑪浜?#37828;?#32429;?#28729;?#23536;?#28598;?#38171;?#37734;?#37714;?#28654;嬮?#33333;灏?#29781;佽楄?#22317;害50+浜?骞?#37510;備?#21975;瓨鍦潃涓浜涘BigCo鍦?#38316;撳d璁?#35120;?#37826;?#38344;殑鎷呭咖锛屽挨鍏舵槸鍦彂?#30541;涓?#37733;?#28729;?#37510;傚湪杩?#28051;璁?#29825;?#28051;?#38171;岀绮圭殑ThoughtWorks瑕佷箟鏄?#38171;孋FO鎱锋叏婵鏄傚湴鍙嶅鎺?#28051;?#26473;欑瑪?#29678;鍒?#23057;?#26864;濈殑宸?#28003;?#38171;岃?#37813;鏈夌殑?#29678;绾?#26864;嗗浠?#37714;?#37706;?#37722;潃涓浣嶆潵?#22172;鍗?#37826;?#37733;?#28729;舵渶杩?#37813;嶅彈闆?#27996;嶵houghtWorks?#27537;鍒濈骇寮?#24386;鑰?#38171;?#28000;?#37819;忚堪?#31777;BigCo?#27537;琛屽?#21228;槸鎬庢?#38155;瀹崇潃浠栫殑鍥?#28729;?#37510;?/span>


]]>Oracle OpenWorld 2013(涓?#23092;?(鍘?http://www.8634070.com/jiangshachina/archive/2013/08/02/402274.htmlJohn JiangJohn JiangFri, 02 Aug 2013 03:50:00 GMThttp://www.8634070.com/jiangshachina/archive/2013/08/02/402274.htmlhttp://www.8634070.com/jiangshachina/comments/402274.htmlhttp://www.8634070.com/jiangshachina/archive/2013/08/02/402274.html#Feedback0http://www.8634070.com/jiangshachina/comments/commentRss/402274.htmlhttp://www.8634070.com/jiangshachina/services/trackbacks/402274.html
Oracle OpenWorld 2013(涓?#23092;?
Oracle OpenWorld 2013(涓?#23092;?宸?#32513;?#32513;?#37833;?#38171;?#37812;?#37711;?#32459;嬪弬涓庝簡姝?#23046;?#37814;鏈?#28598;?#28028;?#37510;?#37832;?#37826;?#37828;?#37812;?#37721;?#28028;氱殑鎵瑙?#37510;佹墍闂?#37510;佹墍鎬?#28051;庢墍?#21125;锛?#37832;?#37826;?#38171;?#37832;?#37733;?#38171;?#28051;?#37827;犵爜銆?2013.08.03鏈鍚庢洿鏂?

Oracle OpenWorld 2013(Shanghai) lasted four days, and included over 300 keynotes, sessions and hands-on labs. 18000+ attendees, including company leaders, industries experts and developers, joined the event, which was said the biggest IT activity in Asia-Pacific area as never before.

Sessions
* Java strategy/technical keynote
Every JavaOne conference must has such keynote, which introduces and demonstrates the latest new JDK versions and features, and prospects upcoming JDK version and features. All parts of Java ecosystem, including JME, JSE, JEE and JavaFX, are involved in the keynote. I was deeply impressed by JavaFX demo by Jim Weaver. The Java champion played musical instruments with a JavaFX application; that's really cool. In this JavaOne, JavaFX has a separated track, and the technology is very highlighted by Oracle. The software giant expects JavaFX to enhance Java's performance on rich application. But outspokenly, it's difficult to take end users to install a JRE beforehand if they just want to run a desktop application.


* Why should I switch to Java SE 7?
JDK 6 has rested in peace(exactly, the version isn't completely frozen), and JDK 7 has been released for two years, and even JDK 8 will come next March. But do you migrate your JRE to Java 7? Strings in switch statements, diamond operator, try with resources, multi-catch statement, ... Do you like the compiler sugar? Anyway, the features could improve development productivity. But fork-join framework really touches my heart. Because of complete backward compatibility, you just need to run your application in the new platform, and the performance could be accelerated.

* The road to Lambda
Java has been being criticised for implementing modern language features, like closure. The argument around closure implementation has lasted for a long time. Finally, Lambda, the closure for Java, has been finished, and will be distributed with JDK 8. Lambda is regarded as the biggest change since Generics in Java 5. Function Programming (FP) is popular, and Java developers can utilise Lambda expression to apply FP. And JDK 8 extends Collection framework to take advantage of Lambda as powerful as possible. For example, currently we need an iterator or loop to iterate a Collection container, that's so-called external iteration. JDK 8 adds a new method forEach() for Collection interface, and we can iterate a Collection object via internal iteration, that means it's no need to care the iteration details by developers. In fact, it's impossible to debug the iterating process. And the process may apply fork-join if possible to improve performance. That sounds cool!

* Effective Scala
Dynamic programming languages are emerging, and more languages, such as Groovy, Scala and Clojure, can run on JVM, and JDK 7 introduced a new instruction "invokeDynamic" to support dynamic languages better. Therefore, I have to pay some attention to other JVM languages. The session "Effective Scala" was provided by TypeSafe, a company founded by Scala creators; Spring founder Rod Johnson joined the company as member of board of directors last year. The session simulated the style of Effective Java, and also introduced some best practices and coding regulations. Frankly, I don't know Scala syntax, like traits, totally, so I don't understand the lecture exactly. But I still obtained something new, like Cake pattern. Do you know the design pattern? I never hear it before this session, but the pattern is well-known in Scala world. It's time to learn another language, and I think Groovy is a good start-up due to it is closer to Java.

* Taobao GCIH

Taobao creates GCIH (GC Invisible Heap) based on Oracle VM Hotspot, and allows different VM to access a shared heap. In Taobao business scenario, the shared heap only stores read-only objects. All of the objects are initialized after the application is launched, and not be cleaned before the application shutdowns. Therefore, single VM can consume less memory, that means we can deploy more VMs in single machine. And GCIH cannot be accessed by GC so that reducing the garbage collection overhead.

* What do you mean, backwards compatibility?
10gen, the company behind MongoDB, presented the topic. Because old MongoDB driver isn't well designed, so it's difficult to understand and use the driver API. For example, the driver has a lot of find() methods, but how do I know which one is my want. According to the session, method chaining not only makes codes more readable, and the idiom benefits the backward compatibility as well. Generally, method chaining only handle one parameter at each invoking, and application codes don't care the sequence of invoking. In this case, it's no need to provide the methods with several arguments, and the methods are possible to be deprecated in later time due to their ambiguity. Of course, method chaining isn't designed for backward compatibility. And applying backward compatibility is still a hard job, because you don't know what would happen in the future. In fact, many and many APIs are deprecated in later JDK versions.

Appreciation Party
In the third evening, Oracle catered a small but exciting party to every attendee. Beer, snacks, professional band, beautiful girls, hot performances, ... were on the stage. All of performances were shown by Oracle employees, and we just rock!



I was on the scene
In OTN lounge, I communicated with Jim Weaver and his assistant about JavaFX and UI testing. In this event, my small dream, taking a photo with Java mascot -- Duke, came true ^_^ Additionally, don't forget iron man :-)


Event Organization
This is my sixth SunTechDays/OOW, and I must say that the conference was organized the best this time. Professional agenda, considerate attendee service, delicious dessert and drinking, all of them, except for lunch, must be delighted.



Finally, I must say the event is well worth participating; join it, and enjoy it :-)


]]>涓?#37923;?#28051;?#27967;鍦彉(鍘?http://www.8634070.com/jiangshachina/archive/2013/07/14/401546.htmlJohn JiangJohn JiangSun, 14 Jul 2013 03:15:00 GMThttp://www.8634070.com/jiangshachina/archive/2013/07/14/401546.htmlhttp://www.8634070.com/jiangshachina/comments/401546.htmlhttp://www.8634070.com/jiangshachina/archive/2013/07/14/401546.html#Feedback3http://www.8634070.com/jiangshachina/comments/commentRss/401546.htmlhttp://www.8634070.com/jiangshachina/services/trackbacks/401546.html
涓?#37923;?#28051;?#27967;鍦彉
    鏈杩?#37832;夌?#29808;効闂?#38171;岀湅?#31777;涓浜?#37826;囩珷锛?#37832;変簡涓浜涙劅瑙?#38171;屽啓鎴?#28051;?#30226;灏?#37826;?#37510;?#26462;?#28000;?#28051;?#37923;岀?#29114;殑?#24393;?#23538;寰?#28598;?#38171;屾病鏈?#23000;告?#25492;殑缁?#32513;?#38000;?#37510;?#28000;?#28598;殑缁?#32513;?#38000;?#38171;?#37828;?#28598;彲鑳藉氨浼氳鍒?#27996;?#32513;?#32513;?#37510;傞亾?#24714;澶?#28729;?#38318;芥锛屼絾?#24135;瀹?#28186;濈?#36346;緢娈嬮叿銆?2013.07.29鏈鍚庢洿鏂?

    This week, I read some articles about some API and tools that developers, especially Java guys, must know. Fortunately, I really know some of them, but unfortunately, I really miss something.

    Please let me introduce some cases at first:
    1. In our real projects, we only use JDK 6, but the version had been in End-Of-Life; we never touch JDK 7, but JDK 8 is upcoming. I don't know how much time we would spend on accepting
Lambda expression. In fact, at present, a lot of Java developers cannot understand Generics exactly, however the syntax has been introduced for more than 8 years. Of course, Java Generics is a bit ambiguous, so it may be difficult to understand.
    2. Ant was ever the standard for building, and it still being used by many projects, even new ones. Maven was designed to terminate Ant due to the older cannot make life easy. Some conceptions of Maven, such as build life cycle, dependency management, default directory structure, are very advanced. But Maven dependency and transitive dependency management is nightmare, you have to include/exclude this or that. And extending Maven is also a hard job. I have real experience on both of them, I even wrote some popular preliminary blogs about Maven several years ago. But what I really don't know? I don't know Maven is becoming legacy, and worse, a new super star Gradle is on stage. Outspokenly, I never hear of the artifact before this week :-( Outstanding Spring framework is a very case about the trend. At beginning, obviously Spring is built by Ant, then the framework switched to Maven some years ago, but last year Spring migrated to Gradle.
    3. Google-Collections was well-known if you used it or not, and I know Guava however I never use the API. But what I really don't know? I don't know google-collections was closed several years ago, and even it was combined by Guava, which is a new rock star in Java ecosystem.

銆銆OH, something is born, and then grows, and then rests in peace. That's nature, and we have to face it, but why I don't know? Exactly, I have no idea.
銆銆World has been changing, and is changing faster as never before. How to keep us up-to-date with new fashion? I think the question may be asked by every "old" developer. After a long term career life, some of us may become veteran, but absolutely, it's impossible that everyone become expert, particularly the expert in underlying fields. We just be proficient in some programming languages, frameworks, APIs, or tools. So we must update our brains continuously.
銆銆Maybe the issue is one of the middle life crisis problems, good luck for you and me :-)

]]>
˫ɫֱ
<div id="k0jz6"><tr id="k0jz6"></tr></div>
    <sup id="k0jz6"><menu id="k0jz6"><small id="k0jz6"></small></menu></sup>
    <div id="k0jz6"><tr id="k0jz6"><mark id="k0jz6"></mark></tr></div><em id="k0jz6"></em>
    <dl id="k0jz6"><ins id="k0jz6"></ins></dl><dl id="k0jz6"></dl>
      <div id="k0jz6"></div>

      <sup id="k0jz6"></sup><dl id="k0jz6"><ins id="k0jz6"><thead id="k0jz6"></thead></ins></dl>
        <div id="k0jz6"><tr id="k0jz6"></tr></div>
          <sup id="k0jz6"><menu id="k0jz6"><small id="k0jz6"></small></menu></sup>
          <div id="k0jz6"><tr id="k0jz6"><mark id="k0jz6"></mark></tr></div><em id="k0jz6"></em>
          <dl id="k0jz6"><ins id="k0jz6"></ins></dl><dl id="k0jz6"></dl>
            <div id="k0jz6"></div>

            <sup id="k0jz6"></sup><dl id="k0jz6"><ins id="k0jz6"><thead id="k0jz6"></thead></ins></dl>