设计模式-8种单例模式
第一种:饿汉式-1
package com.liuxd.singleton;
public class Mgr01 {
private static final Mgr01 INSTANCE= new Mgr01();
private Mgr01(){}
public static Mgr01 getInstance(){
return INSTANCE;
}
public void m(){
System.out.println("m");
}
public static void main(String[] args) {
for(int i=0;i<100;i++){
new Thread(()->{
System.out.println(Mgr01.getInstance().hashCode());
}).start();
}
}
}
第二种:饿汉式-2
package com.liuxd.singleton;
public class Mgr02 {
private static Mgr02 INSTANCE;
static {
INSTANCE= new Mgr02();
}
private Mgr02(){}
public static Mgr02 getInstance(){
return INSTANCE;
}
public void m(){
System.out.println("m");
}
public static void main(String[] args) {
for(int i=0;i<100;i++){
new Thread(()->{
System.out.println(Mgr02.getInstance().hashCode());
}).start();
}
}
}
第三种:懒汉式-多线程不安全
package com.liuxd.singleton;
public class Mgr03 {
private static Mgr03 INSTANCE;
private Mgr03(){}
public static Mgr03 getInstance(){
if(INSTANCE==null){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE=new Mgr03();
}
return INSTANCE;
}
public void m(){
System.out.println("m");
}
public static void main(String[] args) {
for(int i=0;i<100;i++){
new Thread(()->{
System.out.println(Mgr03.getInstance().hashCode());
}).start();
}
}
}
第四种:懒汉式-多线程安全
package com.liuxd.singleton;
public class Mgr04 {
private static Mgr04 INSTANCE;
private Mgr04() {
}
public static synchronized Mgr04 getInstance() {
if (INSTANCE == null) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Mgr04();
}
return INSTANCE;
}
public void m() {
System.out.println("m");
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread(() -> {
System.out.println(Mgr04.getInstance().hashCode());
}).start();
}
}
}
第五种:懒汉式-多线程不安全
package com.liuxd.singleton;
public class Mgr05 {
private static Mgr05 INSTANCE;
private Mgr05() {
}
public static Mgr05 getInstance() {
if (INSTANCE == null) {
synchronized (Mgr05.class) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Mgr05();
}
}
return INSTANCE;
}
public void m() {
System.out.println("m");
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread(() -> {
System.out.println(Mgr05.getInstance().hashCode());
}).start();
}
}
}
第六种:懒汉式-多线程安全
package com.liuxd.singleton;
public class Mgr06 {
private static volatile Mgr06 INSTANCE;
private Mgr06() {
}
public static Mgr06 getInstance() {
if (INSTANCE == null) {
synchronized (Mgr06.class) {
if(INSTANCE==null){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Mgr06();
}
}
}
return INSTANCE;
}
public void m() {
System.out.println("m");
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread(() -> {
System.out.println(Mgr06.getInstance().hashCode());
}).start();
}
}
}
第七种:静态内部类方式
package com.liuxd.singleton;
public class Mgr07 {
private Mgr07() {
}
private static class Mgr07Handler{
private static Mgr07 INSTANCE = new Mgr07();
}
public static Mgr07 getInstance() {
return Mgr07Handler.INSTANCE;
}
public void m() {
System.out.println("m");
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread(() -> {
System.out.println(Mgr07.getInstance().hashCode());
}).start();
}
}
}
第八种:枚举方式
package com.liuxd.singleton;
public enum Mgr08 {
INSTANCE;
public static Mgr08 getInstance() {
return INSTANCE;
}
public void m() {
System.out.println("m");
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread(() -> {
System.out.println(Mgr08.getInstance().hashCode());
}).start();
}
}
}