1. 程式人生 > >單例設計模式測試

單例設計模式測試

public class ThreadSafeTest {
    public static void main(String[] args) {
        int count = 200;
        final CountDownLatch latch = new CountDownLatch(count);
        for(int i = 0; i < count;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        //等待
                        latch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    HungrySingleton instance = HungrySingleton.getInstance();      
                    System.out.println(System.currentTimeMillis() + ":" + instance);
                }
            }).start();
            latch.countDown();
        }
    }
}