單例設計模式測試
阿新 • • 發佈:2019-02-08
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();
}
}
}
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();
}
}
}