锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
綆鍗曟牱鍒?:
public class Singleton {
private final static Singleton instance=new Singleton();
private Singleton(){}
public static Singleton getInstance(){
return instance;
}
}
鍙岄噸媯(gè)鏌ュ緢閫氱敤,浣嗘槸瀹冨紩浠ヤ負(fù)鍌茬殑鏄ц兘鐨勪紭鍖?鍦╣etInstance琚緢澶氬緢澶氭璋冪敤鐨勬儏鍐典笅).
鍛靛懙,鎴戝氨鐩存帴璇寸粨璁轟簡(jiǎn):鍦ㄦц兘涓婃渶浼樼殑鏄?綆鍗曟牱鍒? [褰撶劧涔熸槸鍦╣etInstance琚緢澶氬緢澶氭璋冪敤鐨勬儏鍐典笅].
綆鍗曟牱鍒?鏄?strong>闈炴儼鎬у姞杞?/strong>,鎵浠ユ湁浜鴻鍙嶉┏璇?濡傛灉鎴戜笉鐢ㄥ埌Singleton 鐨勫疄渚嬪矀涓嶆槸鐧藉崰浜?jiǎn)鍐呭?
鎵浠ヤ綘閫夋嫨 綆鍗曟牱鍒? 榪樻槸 鍙岄噸媯(gè)鏌?/strong> 鏄鏍規(guī)嵁浣犵殑瀹為檯鎯呭喌鐨?濡傛灉鍦ㄧ▼搴忎腑瀵瑰崟鍒楃被寮曠敤鐨勯鐜囨槸寰堥珮鐨?閭d箞搴旇閫夋嫨 綆鍗曟牱鍒?,鍙嶄箣 鍙岄噸媯(gè)鏌?
2.璇峰啓鍑轟竴涓猻ingleton妯″紡鐨刢lass.
浣犲鏋滃啓鍑轟笅闈㈢殑2縐嶆牱寮?鎴戜細(xì)闂綘:璇烽棶浣犲浣曞湪鍚屼竴涓猨vm涓茍涓斿湪鍚屼竴涓猚lassLoader涓緱鍒板畠鐨勫涓疄渚?(璇蜂笉瑕佸鎬?
鏍峰垪1:
public class Singleton {
private final static Singleton instance=new Singleton();
private Singleton(){}
public static Singleton newInstance(){
return instance;
}
}
鏍峰垪2:
public class Singleton {
private static volatile int instanceCounter=0;
private Singleton(){
if(instanceCounter>0)
throw new RuntimeException("can't create multi instances!");
instanceCounter++;
}
private final static Singleton instance=new Singleton();
public static Singleton newInstance(){
return instance;
}
}
3.java 鐨別xception 鍒哻hecked,unchecked.鍍廟untimeException,Error閮戒笉鐢ㄦ樉寮弔ry-catch,鐩存帴鍙互throw,
浣嗘槸涓鑸殑exception鏄繀欏籧atch鐨?
throw new Exception("..."),濡傛灉榪欏彞涓嶅湪try-catch浣撳唴,鎴栬呮柟娉曠殑澹版槑娌℃湁throws,閭d箞緙栬瘧鏄氫笉榪囩殑.
ok,璇風(fēng)湅濡備笅鐨勪唬鐮?
public class TestClass {
public void testMethod()/*榪欓噷娌℃湁throws 鍝?*/{
......
throw new Exception("force throw the exception...");
......
}
}
寰堟槑鏄句笂闈㈢殑鏂規(guī)硶濡傛灉榪欐牱鐨勮瘽鏄氫笉榪囩紪璇戠殑,浣嗘槸濡傛灉闈炲緱瑕佷綘鍦╰estMethod浣撲腑鍦ㄨ繍琛屾椂throw涓涓緢涓鑸殑Exception,璇烽棶浣犳湁鍔炴硶鍚?
榪?閬撻鍙笉鏄痵un鍑虹殑鑰冮鍝?涓嶄俊浣犳悳鎼?.....
0. 涓涓?channal 瀵瑰簲涓涓猄electionKey in the same selector.
e.g:
SelectionKey sk=sc.register(selector, SelectionKey.OP_READ, handler);
sk==sc.register(selector, SelectionKey.OP_WRITE, handler) true?
selector.select() 姣忔榪斿洖鐨勫鍚屼竴channal鐨剆k鏄惁鐩稿悓?
1.channel.register(...) may block if invoked concurrently with another registration[another.register(...)] or selection operation[selector.select(...)] involving *****the same selector*****.
榪欎釜鏄痳egister鏂規(guī)硶jdk src涓婄殑鍘熸枃,
e.g:
濡傛灉涓涓猻election thread宸茬粡鍦╯elect鏂規(guī)硶涓婄瓑寰卛ng,閭d箞榪欎釜鏃跺欏鏋滄湁鍙︿竴鏉$嚎紼嬭皟鐢╟hannal.register鏂規(guī)硶鐨勮瘽,閭d箞瀹冨皢琚玝locking.
2.selectionKey.cancel() : The key will be removed from all of the selector's key sets during *****the next selection operation[selector.select(...)]*****.
may block briefly if invoked concurrently with a cancellation[cancel()] or selection operation[select(...)] involving ***the same selector***.
榪欎釜涔熸槸cancel鏂規(guī)硶jdk src涓婄殑鍘熸枃,
e.g:
浣犲厛灝嗕竴涓猻electionKey.cancel(),鐒跺悗闅忓嵆鍐峜hannel.register to the same selector,
鍦╟ancel鍜宺egister涔嬮棿,濡傛灉娌℃湁綰跨▼(鍖呮嫭褰撳墠綰跨▼)榪涜select鎿嶄綔鐨勮瘽,
閭d箞 throws java.nio.channels.CancelledKeyException.
鎵浠?nbsp;cancel-->select-->re-register.
3.if don't remove the current selectedKey from selector.selectedKeys()[Set] 灝嗗鑷?selector.select(...) not block [may be cpu 100%,specially when client cut the current channel(connection)].
e.g:
Iterator<SelectionKey> it=selector.selectedKeys().iterator();
...for/while it.hasNext()...
it.remove();<------*****must do it. or Keys' Set.clear() finally;
if remove the current selectedKey from selector.selectedKeys()[Set] but don't sk.interestOps(sk.interestOps()& (~sk.readyOps()));灝嗗鑷?selector.select(...) not block [select() not block several times, or excepted exception]
4.op_write should not be registered to the selector. [may be cpu100%]
5. if involving wakeup() before select() [wakeup called several times >=1],the next select() not block [not block just once].
灝界浠ュ墠鏈変簺浜哄垎鏋愪簡(jiǎn)nio鐨剋akeup鎬ц兘鍙?qiáng)not block in linux鐨刡ug,浣嗘槸java nio渚濈劧鏄珮鏁堢殑,閭d簺c/c++鐨勭墰浜轟滑鍘葷湅鐪媕re/bin鐩綍涓嬬殑nio.dll/nio.so鍚?java nio鏄熀浜巗elect妯″瀷(榪欎釜鏄痗/c++涓父鐢ㄧ綉緇滅紪紼嬫ā鍨嬩箣涓)鐨?
鍩轟簬java nio鐨勬湇鍔″櫒:mina,girzzly[glassfish],jetty(鍩轟簬girzzly),tomcat6[鍙互閰嶇疆Http11NioProtocol]...
鍏朵腑浠庢湰浜哄girzzly,tomcat6鐨勬簮鐮佸垎鏋愭潵鐪?瀹冧滑閮借繕娌℃湁鐪熸鍙戞尌鍑簄io寮傛澶勭悊璇鋒眰鐨勪紭鐐?瀹冧滑鐨勮鍐欒繕閮芥槸blocking鐨勮櫧鐒朵嬌鐢ㄤ簡(jiǎn)selectorPool,姝ゅtomcat6瑕佸墺紱誨嚭socket閫氫俊榪樿鑺辮垂涓瀹氱殑鍔熷か.鑰?strong>mina鍗存槸鍚?font class="" style="font-family: " color="#ff0000">涓?/font>絎﹀叾瀹?/strong>,榪樻湁bug鍝?