/**
* 线程安全,类加载时实例就会被初始化
*/
public class Singleton01 {
private static int threadNum = 10;
private static CountDownLatch countDownLatch = new CountDownLatch(1);
private static CountDownLatch countDownLatch2 = new CountDownLatch(threadNum);
private static final Singleton01 singleton = new Singleton01();
private Singleton01(){
}
public static Singleton01 getInstance(){
return singleton;
}
public static void main(String[] args) throws InterruptedException {
final Set<Singleton01> set = Collections.synchronizedSet(new HashSet<Singleton01>());
for(int index=0;index<threadNum;index++){
new Thread(new Runnable() {
public void run() {
try {
//所有线程都在此等候
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
set.add(Singleton01.getInstance());
countDownLatch2.countDown();
}
}).start();
}
//所有线程都启动后,再放行
countDownLatch.countDown();
//等候所有线程执行完add操作
countDownLatch2.await();
System.out.println(set.size());
}
}
/**
* 线程安全,类加载时实例就会被初始化
*/
public class Singleton02 {
private static int threadNum = 1000;
private static CountDownLatch countDownLatch = new CountDownLatch(1);
private static CountDownLatch countDownLatch2 = new CountDownLatch(threadNum);
private static final Singleton02 singleton;
static {
singleton = new Singleton02();
}
private Singleton02(){
}
public static Singleton02 getInstance(){
return singleton;
}
public static void main(String[] args) throws InterruptedException {
final Set<Singleton02> set = Collections.synchronizedSet(new HashSet<Singleton02>());
for(int index=0;index<threadNum;index++){
new Thread(new Runnable() {
public void run() {
try {
//所有线程都在此等候
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
set.add(Singleton02.getInstance());
countDownLatch2.countDown();
}
}).start();
}
//所有线程都启动后,再放行
countDownLatch.countDown();
//等候所有线程执行完add操作
countDownLatch2.await();
System.out.println(set.size());
}
}
/**
*非线程安全
*/
public class Singleton03 {
private static int threadNum = 5000;
private static CountDownLatch countDownLatch = new CountDownLatch(1);
private static CountDownLatch countDownLatch2 = new CountDownLatch(threadNum);
private static Singleton03 singleton;
private Singleton03(){
}
public static Singleton03 getInstance(){
if(singleton == null){
singleton = new Singleton03();
}
return singleton;
}
public static void main(String[] args) throws InterruptedException {
final Set<Singleton03> set = Collections.synchronizedSet(new HashSet<Singleton03>());
for(int index=0;index<threadNum;index++){
new Thread(new Runnable() {
public void run() {
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
set.add(Singleton03.getInstance());
countDownLatch2.countDown();
}
}).start();
}
countDownLatch.countDown();
countDownLatch2.await();
System.out.println(set.size());
}
}
/**
*线程安全,效率低
*/
public class Singleton04 {
private static int threadNum = 5000;
private static CountDownLatch countDownLatch = new CountDownLatch(1);
private static CountDownLatch countDownLatch2 = new CountDownLatch(threadNum);
private static volatile Singleton04 singleton;
private Singleton04(){
}
@Override
public int hashCode() {
return 1;
}
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
public static synchronized Singleton04 getInstance(){
if(singleton == null){
singleton = new Singleton04();
System.out.println("构造方法被执行!!!");
}
return singleton;
}
public static void main(String[] args) throws InterruptedException {
final Set<Singleton04> set = Collections.synchronizedSet(new HashSet<Singleton04>());
for(int index=0;index<threadNum;index++){
new Thread(new Runnable() {
public void run() {
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
set.add(Singleton04.getInstance());
countDownLatch2.countDown();
}
}).start();
}
countDownLatch.countDown();
countDownLatch2.await();
System.out.println(set.size());
}
}
/**
*非线程安全
*/
public class Singleton05 {
private static int threadNum = 5000;
private static CountDownLatch countDownLatch = new CountDownLatch(1);
private static CountDownLatch countDownLatch2 = new CountDownLatch(threadNum);
private static volatile Singleton05 singleton;
private Singleton05(){
}
public static Singleton05 getInstance(){
if(singleton == null){
synchronized(Singleton05.class){
singleton = new Singleton05();
}
}
return singleton;
}
public static void main(String[] args) throws InterruptedException {
final Set<Singleton05> set = Collections.synchronizedSet(new HashSet<Singleton05>());
for(int index=0;index<threadNum;index++){
new Thread(new Runnable() {
public void run() {
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
set.add(Singleton05.getInstance());
countDownLatch2.countDown();
}
}).start();
}
countDownLatch.countDown();
countDownLatch2.await();
System.out.println(set.size());
}
}
/**
*双检测,线程安全、懒加载
*/
public class Singleton06 {
private static int threadNum = 5000;
private static CountDownLatch countDownLatch = new CountDownLatch(1);
private static CountDownLatch countDownLatch2 = new CountDownLatch(threadNum);
private static Singleton06 singleton;
private Singleton06(){
}
public static Singleton06 getInstance(){
if(singleton == null){
synchronized(Singleton06.class){
if(singleton == null){
singleton = new Singleton06();
}
}
}
return singleton;
}
public static void main(String[] args) throws InterruptedException {
final Set<Singleton06> set = Collections.synchronizedSet(new HashSet<Singleton06>());
for(int index=0;index<threadNum;index++){
new Thread(new Runnable() {
public void run() {
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
set.add(Singleton06.getInstance());
countDownLatch2.countDown();
}
}).start();
}
countDownLatch.countDown();
countDownLatch2.await();
System.out.println(set.size());
}
}
/**
*静态内部类,线程安全、懒加载
*/
public class Singleton07 {
private static int threadNum = 5000;
private static CountDownLatch countDownLatch = new CountDownLatch(1);
private static CountDownLatch countDownLatch2 = new CountDownLatch(threadNum);
private Singleton07(){
}
static class SingletonHolder{
static final Singleton07 singleton = new Singleton07();
}
public static Singleton07 getInstance(){
return SingletonHolder.singleton;
}
public static void main(String[] args) throws InterruptedException {
final Set<Singleton07> set = Collections.synchronizedSet(new HashSet<Singleton07>());
for(int index=0;index<threadNum;index++){
new Thread(new Runnable() {
public void run() {
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
set.add(Singleton07.getInstance());
countDownLatch2.countDown();
}
}).start();
}
Thread.sleep(1000);
countDownLatch.countDown();
countDownLatch2.await();
System.out.println(set.size());
}
}
/**
*枚举,线程安全、防止反序列化(怪怪的)
*/
public enum Singleton08 {
INSTANCE;
}