??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;
}
}
双重查很通用,但是它引以ؓ傲的是性能的优?在getInstance被很多很多次调用的情况下).
呵呵,我就直接说结Z:在性能上最优的?单样? [当然也是在getInstance被很多很多次调用的情况下].
单样??strong>非惰性加?/strong>,所以有反驳?如果我不用到Singleton 的实例岂不是白占了内?
所以你选择 单样? q是 双重?/strong> 是要Ҏ你的实际情况?如果在程序中对单列类引用的频率是很高?那么应该选择 单样?,反之 双重?
2.请写Z个singleton模式的class.
你如果写Z面的2U样?我会问你:请问你如何在同一个jvm中ƈ且在同一个classLoader中得到它的多个实?(请不要奇?
样列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 的exception 分checked,unchecked.像RuntimeException,Error都不用显式try-catch,直接可以throw,
但是一般的exception是必catch?
throw new Exception("..."),如果q句不在try-catch体内,或者方法的声明没有throws,那么~译是通不q的.
ok,L如下的代?
public class TestClass {
public void testMethod()/*q里没有throws ?*/{
......
throw new Exception("force throw the exception...");
......
}
}
很明显上面的Ҏ如果q样的话是通不q编译的,但是如果非得要你在testMethod体中在运行时throw一个很一般的Exception,请问你有办法?
q?道题可不是sun出的考题?不信你搜?.....
0. 一?channal 对应一个SelectionKey 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() 每次q回的对同一channal的sk是否相同?
1.channel.register(...) may block if invoked concurrently with another registration[another.register(...)] or selection operation[selector.select(...)] involving *****the same selector*****.
q个是registerҎjdk src上的原文,
e.g:
如果一个selection thread已经在selectҎ上等待ing,那么q个时候如果有另一条线E调用channal.registerҎ的话,那么它将被blocking.
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***.
q个也是cancelҎjdk src上的原文,
e.g:
你先一个selectionKey.cancel(),然后随即再channel.register to the same selector,
在cancel和register之间,如果没有U程(包括当前U程)q行select操作的话,
那么 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].
管以前有些人分析了nio的wakeup性能及not block in linux的bug,但是java nio依然是高效的,那些c/c++的牛Zȝ看jre/bin目录下的nio.dll/nio.so?java nio是基于select模型(q个是c/c++中常用网l编E模型之一)?
Zjava nio的服务器:mina,girzzly[glassfish],jetty(Zgirzzly),tomcat6[可以配置Http11NioProtocol]...
其中从本人对girzzly,tomcat6的源码分析来?它们都还没有真正发挥出nio异步处理h的优?它们的读写还都是blocking的虽然用了selectorPool,此外tomcat6要剥dsocket通信q要p一定的功夫.?strong>mina却是?font class="" style="font-family: " color="#ff0000">?/font>W其?/strong>,q有bug?