Singleton(单例模式)
单例一写法(无线程安全问题)
public class Singleton01 {
private static final Singleton01 INSTANCE = new Singleton01();
private Singleton01() {};
public static Singleton01 getInstance() { return INSTANCE;}
public void m() {
System.out.println("m");
}
public static void main(String[] args) {
Singleton01 m1 = Singleton01.getInstance();
Singleton01 m2 = Singleton01.getInstance();
System.out.println(m1.toString().equals(m2.toString()) );
System.out.println(m1.toString());
System.out.println(m2.toString());
}
}
单例二写法(存在问题)
public class Singleton02 {
private static Singleton02 INSTANCE;
private Singleton02(){
};
public static Singleton02 getInstance() throws InterruptedException {
if (INSTANCE == null) {
Thread.sleep(1);
INSTANCE = new Singleton02();
}
return INSTANCE;
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
new Thread(() -> {
Singleton02 instance = null;
try {
instance = Singleton02.getInstance();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(instance.hashCode());
},i+"").start();
}
}
}
单例三写法(存在问题)
package singleton;
public class Singleton03 {
private Singleton03(){};
private static Singleton03 INTSANCE;
public static synchronized Singleton03 getInstance() throws InterruptedException {
if(INTSANCE == null){
INTSANCE = new Singleton03();
Thread.sleep(1);
}
return INTSANCE;
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread(() ->{
try {
Singleton03 instance = Singleton03.getInstance();
System.out.println(instance.hashCode());
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
}
单例四写法(存在问题)
package singleton;
public class Singleton04 {
private Singleton04(){};
private static Singleton04 INSTANCE;
public static Singleton04 getInstance() throws InterruptedException {
if(INSTANCE == null){
synchronized (Singleton04.class){
Thread.sleep(1);
INSTANCE = new Singleton04();
}
}
return INSTANCE;
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread(() -> {
try {
Singleton04 instance = Singleton04.getInstance();
System.out.println(instance.hashCode());
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
}
单例五写法(存在问题)
package singleton;
public class Singleton05 {
private Singleton05(){};
private static Singleton05 INSTANCE;
public static Singleton05 getInstance() throws InterruptedException {
if(INSTANCE == null){
synchronized (Singleton05.class){
if(INSTANCE == null){
Thread.sleep(1);
INSTANCE = new Singleton05();
}
}
}
return INSTANCE;
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
new Thread(() ->{
try {
Singleton05 instance = Singleton05.getInstance();
System.out.println(instance.hashCode());
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
}
单例六写法(无线程安全问题)
package singleton;
public class Singleton06 {
private Singleton06(){
};
private volatile static Singleton06 INSTANCE;
public static Singleton06 getInstance() throws InterruptedException {
if(INSTANCE == null){
synchronized (Singleton06.class){
Thread.sleep(1);
if(INSTANCE == null){
INSTANCE = new Singleton06();
}
}
}
return INSTANCE;
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
new Thread(() -> {
try {
Singleton06 instance = Singleton06.getInstance();
System.out.println(instance.hashCode());
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
}
单例七写法(无线程安问题 利用jvm类加载写的单例)
package singleton;
public class Singleton07 {
private Singleton07 (){};
private static class SingletonNext{
private final static Singleton07 INSTNACE = new Singleton07();
}
public static Singleton07 getInstance(){
return SingletonNext.INSTNACE;
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
new Thread(() -> {
System.out.println(Singleton07.getInstance().hashCode());
}).start();
}
}
}
单例八写法(无线程安全问题 完美写法)
package singleton;
public enum Singleton08 {
INTSANCE;
private void m(){
System.out.println("我是m");
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
new Thread(() ->{
Singleton08.INTSANCE.m();
System.out.println(Singleton08.INTSANCE.hashCode());
}).start();
}
}
}