方式一、
public class Singleton01{
private static final Singleton01 INSTANCE = new Singleton01 ();
private Singleton01() {
};
public static Singleton01 getInstance() {
return INSTANCE;
}
public void business() {
System.out.println("TODO");
}
public static void main(String[] args) {
Singleton01 s1= Singleton01.getInstance();
Singleton01 s2= Singleton01.getInstance();
System.out.println(s1== s2);
}
}
方式二
public class Singleton02 {
private static final Singleton02 INSTANCE;
static {
INSTANCE = new Singleton02();
}
private Singleton02 () {
};
public static Singleton02 getInstance() {
return INSTANCE;
}
public void business() {
System.out.println("TODO");
}
public static void main(String[] args) {
Singleton02 s1 = Singleton02 .getInstance();
Singleton02 s2 = Singleton02 .getInstance();
System.out.println(s1 == s2);
}
}
方式三
public class Singleton03{
private static Singleton03 INSTANCE;
private Singleton03 () {
}
public static Singleton03 getInstance() {
if (INSTANCE == null) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Singleton03();
}
return INSTANCE;
}
public void business() {
System.out.println("TODO");
}
public static void main(String[] args) {
for(int i=0; i<100; i++) {
new Thread(()->
System.out.println(Singleton03.getInstance().hashCode())
).start();
}
}
}
方式四
public class Singleton04{
private static Singleton04 INSTANCE;
private Singleton04() {
}
public static synchronized Singleton04 getInstance() {
if (INSTANCE == null) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Singleton04();
}
return INSTANCE;
}
public void business() {
System.out.println("TODO");
}
public static void main(String[] args) {
for(int i=0; i<100; i++) {
new Thread(()->{
System.out.println(Singleton04.getInstance().hashCode());
}).start();
}
}
}
方式五
public class Singleton05 {
private static Singleton05 INSTANCE;
private Singleton05 () {
}
public static Singleton05 getInstance() {
if (INSTANCE == null) {
synchronized (Singleton05.class) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Singleton05 ();
}
}
return INSTANCE;
}
public void business() {
System.out.println("TODO");
}
public static void main(String[] args) {
for(int i=0; i<100; i++) {
new Thread(()->{
System.out.println(Singleton05.getInstance().hashCode());
}).start();
}
}
}
方式六
public class Singleton06 {
private static volatile Singleton06 INSTANCE;
private Singleton06 () {
}
public static Singleton06 getInstance() {
if (INSTANCE == null) {
synchronized (Singleton06.class) {
if(INSTANCE == null) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Singleton06();
}
}
}
return INSTANCE;
}
public void business() {
System.out.println("TODO");
}
public static void main(String[] args) {
for(int i=0; i<100; i++) {
new Thread(()->{
System.out.println(Singleton06.getInstance().hashCode());
}).start();
}
}
}
方式七
public class Singleton07 {
private Singleton07 () {
}
private static class Singleton07Holder {
private final static Singleton07 INSTANCE = new Singleton07();
}
public static Singleton07 getInstance() {
return Singleton07Holder.INSTANCE;
}
public void business() {
System.out.println("TODO");
}
public static void main(String[] args) {
for(int i=0; i<100; i++) {
new Thread(()->{
System.out.println(Singleton07.getInstance().hashCode());
}).start();
}
}
}
方式八
public enum Singleton08 {
INSTANCE;
public void business() {
System.out.println("TODO");
}
public static void main(String[] args) {
for(int i=0; i<100; i++) {
new Thread(()->{
System.out.println(Singleton08.INSTANCE.hashCode());
}).start();
}
}
}