publicclassSingletonConsumer{ privatefinalstatic Logger logger = Logger.getLogger(SingletonConsumer.class); privatestatic SingletonConsumer instance; privateSingletonConsumer(){ } public SingletonConsumer getInstance(){ if (instance == null) { logger.debug("instance is null, trying to instantiate a new one"); instance = new SingletonConsumer(); } else { logger.debug("instance is not null, return the already-instantiated one"); } return instance; } }
@Test publicvoidsingletonConsumerTest()throws InterruptedException { ExecutorService executors = Executors.newFixedThreadPool(10); Set<SingletonConsumer> set = new HashSet<>(); for(int i = 0; i < 10; i++){ executors.execute( () -> set.add(SingletonConsumer.getInstance()) ); } executors.shutdown(); executors.awaitTermination(1, TimeUnit.HOURS); Assert.assertEquals(10, set.size()); }
运行测试,输出结果如下
1 2 3 4 5 6 7 8 9 10 11
2017-02-26 13:50:52 DEBUG SingletonConsumer:20 - instance is null, trying to instantiate a new one 2017-02-26 13:50:52 DEBUG SingletonConsumer:20 - instance is null, trying to instantiate a new one 2017-02-26 13:50:52 DEBUG SingletonConsumer:20 - instance is null, trying to instantiate a new one 2017-02-26 13:50:52 DEBUG SingletonConsumer:20 - instance is null, trying to instantiate a new one 2017-02-26 13:50:52 DEBUG SingletonConsumer:20 - instance is null, trying to instantiate a new one 2017-02-26 13:50:52 DEBUG SingletonConsumer:20 - instance is null, trying to instantiate a new one 2017-02-26 13:50:52 DEBUG SingletonConsumer:20 - instance is null, trying to instantiate a new one 2017-02-26 13:50:52 DEBUG SingletonConsumer:20 - instance is null, trying to instantiate a new one 2017-02-26 13:50:52 DEBUG SingletonConsumer:20 - instance is null, trying to instantiate a new one 2017-02-26 13:50:52 DEBUG SingletonConsumer:20 - instance is null, trying to instantiate a new one set size:10
publicstatic SingletonConsumer getInstance(){ synchronized (SingletonConsumer.class) { if (instance == null) { logger.debug("instance is null, trying to instantiate a new one"); instance = new SingletonConsumer(); } else { logger.debug("instance is not null, return the already-instantiated one"); } return instance; } }
我们再次运行测试脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14
2017-02-26 14:01:12 DEBUG SingletonConsumer:21 - instance is null, trying to instantiate a new one 2017-02-26 14:01:12 DEBUG SingletonConsumer:24 - instance is not null, return the already-instantiated one 2017-02-26 14:01:12 DEBUG SingletonConsumer:24 - instance is not null, return the already-instantiated one 2017-02-26 14:01:12 DEBUG SingletonConsumer:24 - instance is not null, return the already-instantiated one 2017-02-26 14:01:12 DEBUG SingletonConsumer:24 - instance is not null, return the already-instantiated one 2017-02-26 14:01:12 DEBUG SingletonConsumer:24 - instance is not null, return the already-instantiated one 2017-02-26 14:01:12 DEBUG SingletonConsumer:24 - instance is not null, return the already-instantiated one 2017-02-26 14:01:12 DEBUG SingletonConsumer:24 - instance is not null, return the already-instantiated one 2017-02-26 14:01:12 DEBUG SingletonConsumer:24 - instance is not null, return the already-instantiated one 2017-02-26 14:01:12 DEBUG SingletonConsumer:24 - instance is not null, return the already-instantiated one java.lang.AssertionError: Expected :10 Actual :1 <Click to see difference>
if (instance == null) { synchronized (SingletonConsumer.class) { logger.debug("instance is null, trying to instantiate a new one"); instance = new SingletonConsumer(); } } else { logger.debug("instance is not null, return the already-instantiated one"); } return instance;
2017-02-26 14:14:18 DEBUG SingletonConsumer:28 - instance is null, trying to instantiate a new one 2017-02-26 14:14:18 DEBUG SingletonConsumer:28 - instance is null, trying to instantiate a new one 2017-02-26 14:14:18 DEBUG SingletonConsumer:28 - instance is null, trying to instantiate a new one 2017-02-26 14:14:18 DEBUG SingletonConsumer:28 - instance is null, trying to instantiate a new one 2017-02-26 14:14:18 DEBUG SingletonConsumer:28 - instance is null, trying to instantiate a new one 2017-02-26 14:14:18 DEBUG SingletonConsumer:28 - instance is null, trying to instantiate a new one 2017-02-26 14:14:18 DEBUG SingletonConsumer:28 - instance is null, trying to instantiate a new one 2017-02-26 14:14:18 DEBUG SingletonConsumer:28 - instance is null, trying to instantiate a new one 2017-02-26 14:14:18 DEBUG SingletonConsumer:28 - instance is null, trying to instantiate a new one 2017-02-26 14:14:18 DEBUG SingletonConsumer:28 - instance is null, trying to instantiate a new one set size:10
publicstatic SingletonConsumer getInstance(){ if (instance == null) { synchronized (SingletonConsumer.class) { if (instance == null) { logger.debug("instance is null, trying to instantiate a new one"); instance = new SingletonConsumer(); } else { logger.debug("instance is not null, return the already-instantiated one "); } } } else { logger.debug("instance is not null, return the already-instantiated one"); } return instance; }