Java単例モードプライベート静的内部クラス実装とテスト
2690 ワード
package org.vincent;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
* , 。
* @author pengrong
*
*/
public class Singleton {
private Singleton() {
};
public static Singleton getInstance() {
return Holder.instance;
}
private static class Holder{
private volatile static Singleton instance = new Singleton();
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService executorService=Executors.newCachedThreadPool();
for (int i = 0; i < 10000; i++) {
Future future=executorService.submit(new Callable() {
@Override
public Integer call() throws Exception {
return Singleton.getInstance().hashCode();
}
});
System.out.println(" "+(i+1)+" "+future.get());;
}
executorService.shutdown();
}
}