/*** @author Alina* @date 2021年12月26日 1:48 上午*/
public class SingleLanhan {private SingleLanhan() { }private static SingleLanhan s = null;public static SingleLanhan getInstance(){if (s == null){synchronized (SingleLanhan.class){if (s == null){s = new SingleLanhan();}}}return s;}
}/*** @author Alina* @date 2021年12月26日 1:29 上午* 多线程,并发访问懒汉式* 安全高效*/class ThreadSinle implements Runnable{@Overridepublic void run() {for(int x = 0;x<10;x++){SingleLanhan sl = SingleLanhan.getInstance();System.out.println(Thread.currentThread().getName()+"...."+sl);}}
}
public class ThreadSingleDemo {public static void main(String[] args) {ThreadSinle ts = new ThreadSinle();Thread t0 = new Thread(ts);Thread t1 = new Thread(ts);Thread t2 = new Thread(ts);Thread t3 = new Thread(ts);t0.start();t1.start();t2.start();t3.start();}
}