kotlin单例&java单例

书写单例基本要求:线程安全和懒加载

  1. 基本单例

Java写法:

/**
 * 最基本单例,拖慢系统启动速度,非懒加载
 */
public class PlainOldSingleton {
    private static PlainOldSingleton INSTANCE = new PlainOldSingleton();

    private PlainOldSingleton() {

    }

    public static PlainOldSingleton getInstance() {
        return INSTANCE;
    }
}

对应的kotlin实现:

object PlainOldSingleton {

}

2.懒加载单例

Java写法:

/**
 * 懒加载单例,非线程安全
 */
public class LazyNotThreadSafe {
    private static LazyNotThreadSafe INSTANCE;

    private LazyNotThreadSafe() {

    }

    public static LazyNotThreadSafe getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new LazyNotThreadSafe();
        }
        return INSTANCE;
    }
}

对应kotlin实现:

class LazyNotThreadSafe private constructor() {
    companion object {
//方法1
        val instance: LazyNotThreadSafe by lazy(LazyThreadSafetyMode.NONE) {
            LazyNotThreadSafe()
        }

//方法2
        private var instance2: LazyNotThreadSafe? = null
        fun getInstance(): LazyNotThreadSafe {
            if (instance2 == null) {
                instance2 = LazyNotThreadSafe()
            }
            return instance2!!
        }
    }


}

3.Synchronized关键字实现单例

Java写法:

/**
 * 同步锁单例 每次加锁资源浪费(实际上只有第一次需要加锁)
 */
public class LazyThreadSafeSynchronized {

    private static LazyThreadSafeSynchronized INSTANCE;


    private LazyThreadSafeSynchronized(){

    }

    public static synchronized LazyThreadSafeSynchronized getInstance(){
        if(INSTANCE==null){
            INSTANCE=new LazyThreadSafeSynchronized();
        }
        return INSTANCE;
    }
}

对应的kotlin实现:

class LazyThreadSafeSynchronized private constructor() {
    companion object {
        var instance2: LazyThreadSafeSynchronized? = null
        @Synchronized
        fun getInstance(): LazyThreadSafeSynchronized {
            if (instance2 == null) {
                instance2 = LazyThreadSafeSynchronized()
            }
            return instance2!!
        }
    }
}

4.doubleCheck形式

java写法:

/**
*书写繁琐
*/
public class LazyThreadSafeDoubleCheck {
    private static volatile LazyThreadSafeDoubleCheck instance;

    private LazyThreadSafeDoubleCheck() {

    }

    public static LazyThreadSafeDoubleCheck getInstance() {
        if (instance == null) {
            synchronized (LazyThreadSafeDoubleCheck.class) {
                if (instance == null) {
                    instance = new LazyThreadSafeDoubleCheck();
                }
            }
        }
        return instance;
    }
}

对应kotlin实现:

class LazyThreadSafeDoubleCheck private constructor() {
    companion object {
//方法1
        val instance: LazyThreadSafeDoubleCheck  by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
            LazyThreadSafeDoubleCheck()
        }
//方法2
        @Volatile
        var instance2: LazyThreadSafeDoubleCheck? = null
        fun getInstance():LazyThreadSafeDoubleCheck{
            if(instance2==null){
                synchronized(this){
                    if(instance2==null){
                        instance2= LazyThreadSafeDoubleCheck()
                    }
                }
            }
            return instance2!!
        }
    }


}

5.静态内部类形式(推荐)

Java写法:

public class LazyThreadSafeStaticInnerClass {
    private static class Holder{
        private static LazyThreadSafeStaticInnerClass holder=new LazyThreadSafeStaticInnerClass();
    }

    private LazyThreadSafeStaticInnerClass() {
    }

    public static LazyThreadSafeStaticInnerClass getInstance(){
        return Holder.holder;
    }
}

对应的kotlin实现:

lass LazyThreadSafeStaticInnerClass private constructor() {
    companion object {
        fun getInstance(): LazyThreadSafeStaticInnerClass {
            return Holder.holder
        }
    }

    private object Holder {
        val holder = LazyThreadSafeStaticInnerClass()
    }
}

资料来自:https://github.com/enbandari/Kotlin-Tutorials

发布了17 篇原创文章 · 获赞 4 · 访问量 1390
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览