坦克大战(一期)-设计模式版-单例8种设计模式

DesignPatterns
bingor_yhj 2 years ago
parent 9ad46d11e1
commit f38dcc7a03

@ -0,0 +1,30 @@
package com.msb.singleton;
/**
* @Author bingor
* @Date 2022-10-06 10:25
* @Description:
* 饿
* JVM线
* 使
*
*
* @Version: 1.0
*/
public class Singleton01 {
private static final Singleton01 INSTANCE = new Singleton01();
private Singleton01() {}
public static Singleton01 getInstance() {
return INSTANCE;
}
public static void main(String[] args) {
Singleton01 singleton1 = Singleton01.getInstance();
Singleton01 singleton2 = Singleton01.getInstance();
System.out.println(singleton1 == singleton2);
}
}

@ -0,0 +1,29 @@
package com.msb.singleton;
/**
* @Author bingor
* @Date 2022-10-06 10:25
* @Description: 01使
* @Version: 1.0
*/
public class Singleton02 {
private static final Singleton02 INSTANCE;
static {
INSTANCE = new Singleton02();
}
private Singleton02() {}
public static Singleton02 getInstance() {
return INSTANCE;
}
public static void main(String[] args) {
Singleton02 singleton1 = Singleton02.getInstance();
Singleton02 singleton2 = Singleton02.getInstance();
System.out.println(singleton1 == singleton2);
}
}

@ -0,0 +1,39 @@
package com.msb.singleton;
import java.util.Objects;
/**
* @Author bingor
* @Date 2022-10-06 10:25
* @Description:
*
* 线
* @Version: 1.0
*/
public class Singleton03 {
private static Singleton03 INSTANCE;
private Singleton03() {}
public static Singleton03 getInstance() {
if(Objects.isNull(INSTANCE)) {
try { //这里是为了测试线程
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Singleton03();
}
return INSTANCE;
}
public static void main(String[] args) {
for (int i=0; i<100; i++) {
new Thread(() -> { //同样的哈希码,可能是不同的对象。但不同的对象,有不同的哈希码
System.out.println(Singleton03.getInstance().hashCode());
}).start();
}
}
}

@ -0,0 +1,40 @@
package com.msb.singleton;
import java.util.Objects;
/**
* @Author bingor
* @Date 2022-10-06 10:25
* @Description:
*
* 线
* synchronized
* @Version: 1.0
*/
public class Singleton04 {
private static Singleton04 INSTANCE;
private Singleton04() {}
public static synchronized Singleton04 getInstance() {
if(Objects.isNull(INSTANCE)) {
try { //这里是为了测试线程
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Singleton04();
}
return INSTANCE;
}
public static void main(String[] args) {
for (int i=0; i<100; i++) {
new Thread(() -> { //同样的哈希码,可能是不同的对象。但不同的对象,有不同的哈希码
System.out.println(Singleton04.getInstance().hashCode());
}).start();
}
}
}

@ -0,0 +1,43 @@
package com.msb.singleton;
import java.util.Objects;
/**
* @Author bingor
* @Date 2022-10-06 10:25
* @Description:
*
* 线
* synchronized
* @Version: 1.0
*/
public class Singleton05 {
private static Singleton05 INSTANCE;
private Singleton05() {}
public static Singleton05 getInstance() {
if(Objects.isNull(INSTANCE)) {
//试图减少同步代码块的方式来提高效率,然而不可行,仍然会有线程不安全问题
synchronized (Singleton05.class) {
try { //这里是为了测试线程
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Singleton05();
}
}
return INSTANCE;
}
public static void main(String[] args) {
for (int i=0; i<100; i++) {
new Thread(() -> { //同样的哈希码,可能是不同的对象。但不同的对象,有不同的哈希码
System.out.println(Singleton05.getInstance().hashCode());
}).start();
}
}
}

@ -0,0 +1,45 @@
package com.msb.singleton;
import java.util.Objects;
/**
* @Author bingor
* @Date 2022-10-06 10:25
* @Description:
*
* 线
* synchronized
* @Version: 1.0
*/
public class Singleton06 {
private static Singleton06 INSTANCE;
private Singleton06() {}
public static Singleton06 getInstance() {
if(Objects.isNull(INSTANCE)) {
//试图减少同步代码块的方式来提高效率,然而不可行,仍然会有线程不安全问题
synchronized (Singleton06.class) {
if(Objects.isNull(INSTANCE)) {
try { //这里是为了测试线程
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Singleton06();
}
}
}
return INSTANCE;
}
public static void main(String[] args) {
for (int i=0; i<100; i++) {
new Thread(() -> { //同样的哈希码,可能是不同的对象。但不同的对象,有不同的哈希码
System.out.println(Singleton06.getInstance().hashCode());
}).start();
}
}
}

@ -0,0 +1,34 @@
package com.msb.singleton;
import java.util.Objects;
/**
* @Author bingor
* @Date 2022-10-06 10:25
* @Description:
*
* JVM
*
* @Version: 1.0
*/
public class Singleton07 {
private Singleton07() {}
private static class Singleton07Holder {
private static final Singleton07 INSTANCE = new Singleton07();
}
public static Singleton07 getInstance() {
return Singleton07Holder.INSTANCE;
}
public static void main(String[] args) {
for (int i=0; i<100; i++) {
new Thread(() -> { //同样的哈希码,可能是不同的对象。但不同的对象,有不同的哈希码
System.out.println(Singleton07.getInstance().hashCode());
}).start();
}
}
}

@ -0,0 +1,19 @@
package com.msb.singleton;
/**
* @Author bingor
* @Date 2022-10-06 11:20
* @Description: 线
* @Version: 1.0
*/
public enum Singleton08 {
INSTANCE;
public static void main(String[] args) {
for (int i=0; i<100; i++) {
new Thread(() -> { //同样的哈希码,可能是不同的对象。但不同的对象,有不同的哈希码
System.out.println(Singleton08.INSTANCE.hashCode());
}).start();
}
}
}
Loading…
Cancel
Save