/**
* 描述:
* 静态代码块
*
* @author 小纸人
* @create 2019-03-11 19:05
*/
public class StaticCodeBlockSingleton {
private static StaticCodeBlockSingleton staticCodeBlockSingleton = null;
private StaticCodeBlockSingleton() {
}
static {
staticCodeBlockSingleton = new StaticCodeBlockSingleton();
}
public static StaticCodeBlockSingleton getInstance(){
return staticCodeBlockSingleton;
}
}
class MainStaticCodeBlockSingleton{
public static void main(String[] args){
ThreadStaticCodeBlockSingleton th = new ThreadStaticCodeBlockSingleton();
Thread thread1 = new Thread(th);
Thread thread2 = new Thread(th);
thread1.start();
thread2.start();
}
}
class ThreadStaticCodeBlockSingleton implements Runnable{
@Override
public void run() {
System.out.println(StaticCodeBlockSingleton.getInstance());
}
}
单例静态代码块
最新推荐文章于 2024-02-02 21:53:08 发布