2022 9/29 22:59
路漫漫其修远兮,吾将上下而求索
本文是根据尚硅谷学习所做笔记
仅供学习交流使用,转载注明出处
【尚硅谷经典Java面试题第一季(java面试精讲)-哔哩哔哩】
推荐:
单例模式【Java设计模式】
设计模式【java提高】
编程题:写一个Singleton实例
直接实例化饿汉式(简洁直观)
package singleton2;
/**
* 饿汉式
* 直接创建实例对象,不管你是否需要对象都会创建
* (1)构造器私有化
* (2)自行创建,并且用静态变量保存
* (3)向外提供这个实例
* (4)强调这是个单例,我们可以使用final修改
*/
public class Singleton1 {
public static final Singleton1 INSTANCE=new Singleton1();
private Singleton1(){
}
}
枚举式(最简洁)
package singleton2;
/**
* 枚举类型,表示给类型的对象是有限的几个
* 我们可以限定为一个,就成为了单例了
*/
public enum Singleton2 {
INSTANCE
}
静态代码块饿汉式(适合复杂实例化)
package singleton2;
import java.io.IOException;
import java.util.Properties;
public class Singleton3 {
public static final Singleton3 INSTANCE;
private String info;
static {
try {
Properties pro=new Properties();
//用类加载器加载src下的配置文件
pro.load(Singleton3.class.getClassLoader().getResourceAsStream("single.properties"));
INSTANCE=new Singleton3(pro.getProperty("info"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private Singleton3(String info){
this.info=info;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@Override
public String toString() {
return "Singleton3{" +
"info='" + info + '\'' +
'}';
}
}
single.properties
#key=value
info=atguigu
package singleton2;
import org.junit.Test;
public class TestSingletonEHan {
@Test
public void testSingleton1(){
Singleton1 s=Singleton1.INSTANCE;
System.out.println(s);//singleton2.Singleton1@1b9e1916
}
@Test
public void testSingleton2(){
Singleton2 s=Singleton2.INSTANCE;
System.out.println(s);//INSTANCE
}
@Test
public void testSingleton3(){
Singleton3 s=Singleton3.INSTANCE;
System.out.println(s);//Singleton3{info='atguigu'}
}
}
线程不安全(适用于单线程)
package singleton2;
/**
* 懒汉式
*
* (1)构造器私有化
* (2)用一个静态变量保存这个唯一的实例
* (3)提供一个静态方法,获取这个实例对象
*
*/
public class Singleton4 {
private static Singleton4 instance;
private Singleton4(){
}
public static Singleton4 getInstance(){
if (instance==null){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
instance=new Singleton4();
}
return instance;
}
}
线程安全(适用于多线程)
package singleton2;
/**
* 懒汉式
*
* (1)构造器私有化
* (2)用一个静态变量保存这个唯一的实例
* (3)提供一个静态方法,获取这个实例对象
*
*/
public class Singleton5 {
private volatile static Singleton5 instance;
private Singleton5(){
}
public static Singleton5 getInstance(){
if (instance==null) {
synchronized (Singleton5.class) {
if (instance==null){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
instance=new Singleton5();
}
}
}
return instance;
}
}
静态内部类形式(适用于多线程)
package singleton2;
/**
* 在内部类被加载和初始化时,才创建INSTANCE
* 静态类不会自动随着外部类的加载和初始化而初始化的,它是要单独去加载和初始化的。
* 因为是在内部类加载和初始化时,创建的,因此是线程安全的
*/
public class Singleton6 {
private Singleton6(){
}
private static class Inner{
private static final Singleton6 INSTANCE =new Singleton6();
}
public static Singleton6 getInstance(){
return Inner.INSTANCE;
}
}
package singleton2;
import org.junit.Test;
import java.util.concurrent.*;
public class TestSingletonLanHan {
@Test
public void testSingleton4() throws ExecutionException, InterruptedException {
/*
Singleton4 s1=Singleton4.getInstance();
Singleton4 s2=Singleton4.getInstance();
System.out.println(s1==s2);//true
System.out.println(s1);//singleton2.Singleton4@1b9e1916
System.out.println(s2);//singleton2.Singleton4@1b9e1916
*/
Callable<Singleton4> c=new Callable<Singleton4>() {
@Override
public Singleton4 call() throws Exception {
return Singleton4.getInstance();
}
};
ExecutorService es= Executors.newFixedThreadPool(2);
Future<Singleton4> f1 = es.submit(c);
Future<Singleton4> f2 = es.submit(c);
Singleton4 s1 = f1.get();
Singleton4 s2 = f2.get();
System.out.println(s1==s2);//false
System.out.println(s1);//singleton2.Singleton4@3b764bce
System.out.println(s2);//singleton2.Singleton4@759ebb3d
es.shutdown();
}
@Test
public void testSingleton5() throws ExecutionException, InterruptedException {
Callable<Singleton5> c=new Callable<Singleton5>() {
@Override
public Singleton5 call() throws Exception {
return Singleton5.getInstance();
}
};
ExecutorService es= Executors.newFixedThreadPool(2);
Future<Singleton5> f1 = es.submit(c);
Future<Singleton5> f2 = es.submit(c);
Singleton5 s1 = f1.get();
Singleton5 s2 = f2.get();
System.out.println(s1==s2);//true
System.out.println(s1);//singleton2.Singleton5@3b764bce
System.out.println(s2);//singleton2.Singleton5@3b764bce
es.shutdown();
}
@Test
public void testSingleton6(){
Singleton6 s=Singleton6.getInstance();
System.out.println(s);//singleton2.Singleton6@1b9e1916
}
}
2022 9/29 23:49
p2
Markdown 5062 字数 364 行数
HTML 4532 字数 249 段落