单例模式

master
terry 3 years ago
commit bf4fe52eb3

29
.gitignore vendored

@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

@ -0,0 +1,22 @@
package singleton;
/*
* 饿
* JVM线
*
* */
public class Singleton1 {
private static final Singleton1 INSTANCE = new Singleton1();
private Singleton1() {
}
public static Singleton1 getInstance() {
return INSTANCE;
}
public static void main(String[] args) {
Singleton1 singleton = Singleton1.getInstance();
System.out.println(singleton);
}
}

@ -0,0 +1,23 @@
package singleton;
/*
* 1
* */
public class Singleton2 {
private static final Singleton2 INSTANCE;
static {
INSTANCE = new Singleton2();
}
private Singleton2() {
}
public static Singleton2 getInstance() {
return INSTANCE;
}
public static void main(String[] args) {
Singleton2 singleton = Singleton2.getInstance();
System.out.println(singleton);
}
}

@ -0,0 +1,27 @@
package singleton;
/*
* lazy loading
* 线
* */
public class Singleton3 {
private static Singleton3 INSTANCE;
private Singleton3() {
}
public static Singleton3 getInstance() {
if(INSTANCE == null){
INSTANCE = new Singleton3();
}
return INSTANCE;
}
public static void main(String[] args) {
Singleton3 singleton = Singleton3.getInstance();
//多线程 访问可能造成new出多个实例 第一个线程判读为null的时候还没new完对象 第二个线程进来也new了一个对象
for (int i=0; i< 100; i++){
new Thread(() -> System.out.println(singleton.hashCode())).start();
}
}
}

@ -0,0 +1,27 @@
package singleton;
/*
* lazy loading
* ,
* */
public class Singleton4 {
private static Singleton4 INSTANCE;
private Singleton4() {
}
public static synchronized Singleton4 getInstance() {
if(INSTANCE == null){
INSTANCE = new Singleton4();
}
return INSTANCE;
}
public static void main(String[] args) {
Singleton4 singleton = Singleton4.getInstance();
//多线程 访问可能造成new出多个实例 第一个线程判读为null的时候还没new完对象 第二个线程进来也new了一个对象
for (int i=0; i< 100; i++){
new Thread(() -> System.out.println(singleton.hashCode())).start();
}
}
}

@ -0,0 +1,29 @@
package singleton;
/*
* lazy loading
* ,
* */
public class Singleton5 {
private static Singleton5 INSTANCE;
private Singleton5() {
}
//线程不安全,同3类似
public static Singleton5 getInstance() {
if(INSTANCE == null){
synchronized (Singleton5.class){
INSTANCE = new Singleton5();
}
}
return INSTANCE;
}
public static void main(String[] args) {
Singleton5 singleton = Singleton5.getInstance();
for (int i=0; i< 100; i++){
new Thread(() -> System.out.println(singleton.hashCode())).start();
}
}
}

@ -0,0 +1,29 @@
package singleton;
/*
* lazy loading
* ,
* */
public class Singleton6 {
private static Singleton6 INSTANCE;
private Singleton6() {
}
public static synchronized Singleton6 getInstance() {
synchronized (Singleton6.class){
//双重检查
if(INSTANCE == null) {
INSTANCE = new Singleton6();
}
}
return INSTANCE;
}
public static void main(String[] args) {
Singleton6 singleton = Singleton6.getInstance();
for (int i=0; i< 100; i++){
new Thread(() -> System.out.println(singleton.hashCode())).start();
}
}
}

@ -0,0 +1,25 @@
package singleton;
/*
* jvm
*
* */
public class Singleton7 {
private Singleton7() {
}
private static class SingletonHolder{
private static Singleton7 INSTANCE = new Singleton7();
}
public static synchronized Singleton7 getInstance() {
return SingletonHolder.INSTANCE;
}
public static void main(String[] args) {
Singleton7 singleton = Singleton7.getInstance();
for (int i=0; i< 100; i++){
new Thread(() -> System.out.println(singleton.hashCode())).start();
}
}
}

@ -0,0 +1,15 @@
package singleton;
/*
* , 线
* */
public enum Singleton8 {
INSTANCE;
public static void main(String[] args) {
for (int i=0; i< 100; i++){
new Thread(() -> System.out.println(Singleton8.INSTANCE.hashCode())).start();
}
}
}
Loading…
Cancel
Save