/**
* @Project:
* @Author: liming
* @Date: 2018年06月05日
*/
package com.withmes.demo.thread;
/**
* ClassName: TestSingle
* @Description:
* @author liming
* @date 2018年06月05日
*/
public class TestSingle {
private static class MyThread implements Runnable{
@Override
public void run() {
System.out.println( Single.getInstance().hashCode());
}
}
public static void main(String[] args) {
MyThread myThread = new MyThread();
Thread [] threads = new Thread[10];
for (int i = 0; i < threads.length; i++) {
new Thread(myThread).start();
}
}
}
class Single {
private Single (){}
volatile public static Single single ;
public static Single getInstance () {
if (null == single) {
synchronized (Single.class) {
if (null == single) {
single = new Single();
}
}
}
return single;
}
}
java-多线程--单例模式(双检查锁机制)
最新推荐文章于 2024-02-05 21:03:27 发布