- 恶汉模式Entity
package com.myibs.test.service.singleObject;
public class Singleton {
public String getsStr() {
return sStr;
}
public void setsStr(String sStr) {
this.sStr = sStr;
}
private String sStr="1";
private static Singleton instance = new Singleton();
private Singleton(){
System.out.println("SingleObject construction");
}
public static Singleton getInstance(){
return instance;
}
public void showMessage(){
System.out.println("Hello World! " + sStr);
}
}
- 懒汉模式Entity: 主要采用了Synchronized锁确保了新建时等待调用同一对象。
package com.myibs.test.service.singleObject;
public class SingletonLazy {
public String getsStr() {
return sStr;
}
public void setsStr(String sStr) {
this.sStr = sStr;
}
private String sStr="1";
private static SingletonLazy instance = new SingletonLazy();
private SingletonLazy(){}
public static synchronized SingletonLazy getInstance(){
if (instance == null) {
instance = new SingletonLazy();
}
return instance;
}
public void showMessage(){
System.out.println("Hello World! " + sStr);
}
}
- 恶汉模式Thread
package com.myibs.test.service.singleObject;
public class SingletonThread extends Thread {
public String getName1() {
return name1;
}
public void setName1(String name1) {
this.name1 = name1;
}
String name1;
private static SingletonThread instance = new SingletonThread();
private SingletonThread(){}
public static SingletonThread getInstance(){
return instance;
}
public void showMessage(){
System.out.println("Hello World!: " + name1 );
}
@Override
public void run() {
try {
Thread.sleep(100);
for (int i = 0; i < 10; i++) {
System.out.println(name1 + ": 运行,i = " + i);
}
} catch (InterruptedException e) {
System.out.println(name1 +" Thread is terminated.....");
} catch (Exception e) {
e.printStackTrace();
}
}
}
- Demo主程序
package com.myibs.test.service.singleObject;
import java.util.Objects;
public class SingletonPatternDemo {
public static void main(String[] args) {
Singleton object2 = Singleton.getInstance();
object2.showMessage();
object2.setsStr("2");
object2.showMessage();
Singleton object21 = Singleton.getInstance();
object21.setsStr("3");
object2.showMessage();
object21.showMessage();
if (object2 == object21) {
System.out.println("These two objects are the same");
}
if (object2.equals(object21)) {
System.out.println("These two objects are the same");
}
if (Objects.equals(object2,object21)) {
System.out.println("These two objects are the same");
}
SingletonLazy object3 = SingletonLazy.getInstance();
object3.showMessage();
object3.setsStr("2");
object3.showMessage();
SingletonLazy object1 = SingletonLazy.getInstance();
object1.showMessage();
object3.setsStr("3");
object1.showMessage();
if (object1 == object3) {
System.out.println("These two objects are the same");
}
if (object3.equals(object1)) {
System.out.println("These two objects are the same");
}
if (Objects.equals(object1,object3)) {
System.out.println("These two objects are the same");
}
SingletonThread objectThread1 = SingletonThread.getInstance();
SingletonThread objectThread2 = SingletonThread.getInstance();
SingletonThread objectThread3 = SingletonThread.getInstance();
objectThread1.setName1("111");
objectThread2.setName1("222");
objectThread3.setName1("333");
System.out.println(objectThread1 + "|" + objectThread1.hashCode() + "|" + System.identityHashCode(objectThread1));
System.out.println(objectThread2 + "|" + objectThread2.hashCode() + "|" + System.identityHashCode(objectThread2));
System.out.println(objectThread3 + "|" + objectThread3.hashCode() + "|" + System.identityHashCode(objectThread3));
objectThread1.showMessage();
objectThread2.showMessage();
objectThread3.showMessage();
try {
objectThread1.start();
objectThread2.start();
objectThread3.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}