单例模式学习:
单例模式只能产生一个对象。
那么这个对象就不能被其它对象创建,只能自己创建,要实现只能自己创建就要做到私有化构造方法;
现在别人不能创建了,那得让对象存在且能被使用吧,所以就要自己创建并提供一个别人访问的方法;
什么样的方法能被其他对象直接使用,所以需要将自己创建的对象用static修饰为静态的,既然对象是静态的,那么方法也应该跟着静态化了。
1、私有化构造方法
2、提供一个其他对象能够调用对象的方法
3、方法和自己创建的对象静态化
手写一个单例模式(一定要自己根据思路写下来,再简单也要写):
public class Singleton {
private static Singleton s = new Singleton();
private Singleton(){
System.out.println("new Singleton");
}
public static Singleton getInstance(){
return s;
}
public void run(){
System.out.println("run .....");
}
}
单例模式的另一种写法:懒汉式
public class LazySingleton {
private static LazySingleton ls = null;
private LazySingleton(){
System.out.println("new null");
}
public static synchronized LazySingleton getInstance(){
if(ls == null){
ls = new LazySingleton();
}
return ls;
}
public void run(){
System.out.println("run...........");
}
}
饿汉式在类一加载就创建实例,所以不用考虑线程安全,多线程的时候可以直接使用。
懒汉式是在调用getInstance方法的时候才会根据条件去生成,是线程不安全的,所以需要加上Synchronized来保证在多线程的时候线程是安全的。
单例模式还远不止这样就结束!!!!
用静态代码块
public class Mgr03 {
private static Mgr03 instance;
static{
instance = new Mgr03();
}
private Mgr03(){}
public static Mgr03 getInstance(){
return instance;
}
public void run (){
System.out.println("static singleton..");
}
public static void main(String[] args) {
// Mgr03 m1 = Mgr03.getInstance();
// Mgr03 m2 = Mgr03.getInstance();
// System.out.println(m1.getInstance().hashCode());
// System.out.println(m2.getInstance().hashCode());
// System.out.println(m1==m2);
for (int i = 0; i < 40; i++) {
new Thread(
new Runnable() {
@Override
public void run() {
System.out.println(Mgr03.getInstance().hashCode());
}
}
).start();
}
}
}
懒汉式后优化
public class Mgr04 {
private static Mgr04 instance;
private Mgr04(){}
public static synchronized Mgr04 getInstance(){
if(instance == null){
try {
Thread.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
instance = new Mgr04();
}
return instance;
}
public static void main(String[] args) {
for (int i = 0; i < 50; i++) {
new Thread(new Runnable(){
@Override
public void run(){
System.out.println(Mgr04.getInstance().hashCode());
}
}).start();
}
}
}
DCL再优化
public class Mgr05 {
private static volatile Mgr05 instance;
private Mgr05(){}
public static Mgr05 getInstance(){
if(instance == null ){
synchronized (Mgr05.class){
if(instance == null){
instance = new Mgr05();
}
}
}
return instance;
}
}
静态内部类
public class Mgr06 {
private Mgr06(){}
private static class Mgr06Other{
private static final Mgr06 instance = new Mgr06();
}
public static Mgr06 getInstance(){
return Mgr06Other.instance;
}
public void run (){
System.out.println("static class singleton department..");
}
public static void main(String[] args) {
for (int i = 0; i < 50; i++) {
new Thread(
new Runnable(){
@Override
public void run(){
System.out.println(Mgr06.getInstance().hashCode());
}
}
).start();
}
}
}
EFFECTIVE完美篇--枚举
public enum Mgr07 {
INSTANCE;
//INSTANCETWO;
public void run(){
System.out.println("enum class singleton..");
}
public static void main(String[] args) {
System.out.println(Mgr07.INSTANCE.hashCode());
//System.out.println(Mgr07.INSTANCETWO.hashCode());
/*for (int i = 0; i < 50; i++) {
new Thread(
new Runnable(){
@Override
public void run(){
System.out.println(Mgr07.INSTANCE.hashCode());
System.out.println(Mgr07.INSTANCETWO.hashCode());
}
}
).start();
}*/
}
}