另一鲜为人知的单例写法-ThreadLocal

另一鲜为人知的单例写法-ThreadLocal

源代码范例

当我阅读FocusFinder和Choreographer的时候,我发现这两类的单例实现和我们寻经常使用双重检查锁非常不一样。而是用来一个ThreadLocal。这个也能够实现单例啊,那这个与双重检查锁实现的单例有什么差别呢?

1.FocusFinder

/**
 * The algorithm used for finding the next focusable view in a given direction
 * from a view that currently has focus.
 */
public class FocusFinder {

    private static final ThreadLocal<FocusFinder> tlFocusFinder =
            new ThreadLocal<FocusFinder>() {
                @Override
                protected FocusFinder initialValue() {
                    return new FocusFinder();
                }
            };

    /**
     * Get the focus finder for this thread.
     */
    public static FocusFinder getInstance() {
        return tlFocusFinder.get();
    }

    // enforce thread local access
    private FocusFinder() {}
}

2.Choreographer

public final class Choreographer {
    // Thread local storage for the choreographer.
    private static final ThreadLocal<Choreographer> sThreadInstance =
            new ThreadLocal<Choreographer>() {
        @Override
        protected Choreographer initialValue() {
            Looper looper = Looper.myLooper();
            if (looper == null) {
                throw new IllegalStateException("The current thread must have a looper!");
            }
            return new Choreographer(looper);
        }
    };

    private Choreographer(Looper looper) {
        mLooper = looper;
        mHandler = new FrameHandler(looper);
        mDisplayEventReceiver = USE_VSYNC ?

new FrameDisplayEventReceiver(looper) : null;
        mLastFrameTimeNanos = Long.MIN_VALUE;

        mFrameIntervalNanos = (long)(1000000000 / getRefreshRate());

        mCallbackQueues = new CallbackQueue[CALLBACK_LAST + 1];
        for (int i = 0; i <= CALLBACK_LAST; i++) {
            mCallbackQueues[i] = new CallbackQueue();
        }
    }

    /**
     * Gets the choreographer for the calling thread.  Must be called from
     * a thread that already has a {@link android.os.Looper} associated with it.
     *
     * @return The choreographer for this thread.
     * @throws IllegalStateException if the thread does not have a looper.
     */
    public static Choreographer getInstance() {
        return sThreadInstance.get();
    }

}

理论分析

ThreadLocal会为每个线程提供一个独立的变量副本,从而隔离了多个线程对数据的訪问冲突。

对于多线程资源共享的问题,同步机制採用了“以时间换空间”的方式,而ThreadLocal採用了“以空间换时间”的方式。前者仅提供一份变量,让不同的线程排队訪问,而后者为每个线程都提供了一份变量。因此能够同一时候訪问而互不影响。

public class ThreadLocal{

    /**
     * Provides the initial value of this variable for the current thread.
     * The default implementation returns {@code null}.
     *
     * @return the initial value of the variable.
     */
    protected T initialValue() {
        return null;
    }

    /**
     * Returns the value of this variable for the current thread. If an entry
     * doesn‘t yet exist for this variable on this thread, this method will
     * create an entry, populating the value with the result of
     * {@link #initialValue()}.
     *
     * @return the current value of the variable for the calling thread.
     */
    @SuppressWarnings("unchecked")
    public T get() {
        // Optimized for the fast path.
        Thread currentThread = Thread.currentThread();
        Values values = values(currentThread);
        if (values != null) {
            Object[] table = values.table;
            int index = hash & values.mask;
            if (this.reference == table[index]) {
                return (T) table[index + 1];
            }
        } else {
            values = initializeValues(currentThread);
        }

        return (T) values.getAfterMiss(this);
    }

    /**
     * Gets Values instance for this thread and variable type.
     */
    Values values(Thread current) {
        return current.localValues;
    }

    /**
     * Sets the value of this variable for the current thread. If set to
     * {@code null}, the value will be set to null and the underlying entry will
     * still be present.
     *
     * @param value the new value of the variable for the caller thread.
     */
    public void set(T value) {
        Thread currentThread = Thread.currentThread();
        Values values = values(currentThread);
        if (values == null) {
            values = initializeValues(currentThread);
        }
        values.put(this, value);
    }

}

实现步骤

//1.initialValue,创建ThreadLocal对象
//2.get(),获取当前线程里的values
//3.假设不存在则初始化一个空的values
//4.假设存在,则复用values

另一处经典应用

在Looper中使用ThreadLocal,使之每个Thread都有一个Looper与之相应.

public class Looper{
    // sThreadLocal.get() will return null unless you‘ve called prepare().
    static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();
    /** Initialize the current thread as a looper.
     * This gives you a chance to create handlers that then reference
     * this looper, before actually starting the loop. Be sure to call
     * {@link #loop()} after calling this method, and end it by calling
     * {@link #quit()}.
     */
    public static void prepare() {
       prepare(true);
    }

    private static void prepare(boolean quitAllowed) {
       if (sThreadLocal.get() != null) {
           throw new RuntimeException("Only one Looper may be created per thread");
       }
       sThreadLocal.set(new Looper(quitAllowed));
    }
    /**
    * Return the Looper object associated with the current thread.  Returns
    * null if the calling thread is not associated with a Looper.
    */
    public static @Nullable Looper myLooper() {
       return sThreadLocal.get();
    }
}

自己也写

public class Manager {

    private static final ThreadLocal<Manager> sManager = new ThreadLocal<Manager>() {
        @Override
        protected Manager initialValue() {
            return new Manager();
        }
    };

    private Manager() {

    }

    public static Manager getInstance() {
        return sManager.get();
    }
}

參考

时间: 2024-11-05 10:26:51

另一鲜为人知的单例写法-ThreadLocal的相关文章

有一鲜为人知的单例写法-ThreadLocal

还有一鲜为人知的单例写法-ThreadLocal 源码范例 当我阅读FocusFinder和Choreographer的时候,我发现这两类的单例实现和我们平常用双重检查锁很不一样.而是用来一个ThreadLocal,这个也可以实现单例啊,那这个与双重检查锁实现的单例有什么区别呢? 1.FocusFinder /** * The algorithm used for finding the next focusable view in a given direction * from a view

单例写法 转

如何正确地写出单例模式 1.懒汉式,线程不安全 这段代码简单明了,而且使用了懒加载模式,但是却存在致命的问题.当有多个线程并行调用 getInstance() 的时候,就会创建多个实例.也就是说在多线程下不能正常工作 public class Singleton { private static Singleton instance; private Singleton (){} public static Singleton getInstance() { if (instance == nu

Egret中的三种单例写法

1 普通的单例写法 class Single{ private static instance:Single; public static getInstance():Single{ if(this.instance == null){ this.instance = new Single(); } return this.instance; } public run(){ } } Single.getInstance().run(); 2 Module写法.仿照的Egret中Res资源类写法.

java线程:单例隐藏ThreadLocal实现线程数据共享

问题: 给定的二叉查找树中,有两个节点不小心被调换了位置,现在需要将其修正,不改变树的结构. 分析: 二叉排序树的中序遍历是有序的,所以这个问题又是建立在中序遍历模板上的问题,所以我们可以对其进行中序遍历,并用一个pre指针指向当前遍历结果中的最后一个结点,即下次遍历前的前一个结点.然后就可以通过将当前结点与pre结点进行比较,来判断是否有序了.若乱序,就将这两个结点都放入到预先定义的容器中. 错误的形式就这两种,我们看到,在乱序容器中,最多就存了四个元素,所以空间复杂度还是满足O(n)的,当然

性能比较好的单例写法

2019/10/27, .Net c#代码片段 摘要:一种性能比较好的单例写法 参考来源 其他单例思路: 1.使用依赖注入,注册为单例模式 2.使用双重锁机制 public sealed class SingletonBase//应该使用密封类防止派生 { //写单例的方法 //public string Getxxx(){ } private SingletonBase() { } public static SingletonBase Instance { get { return Nest

iOS单例写法简析

官方文档这样写的: static AccountManager *DefaultManager = nil; + (AccountManager *)defaultManager { if (!DefaultManager) DefaultManager = [[self allocWithZone:NULL] init]; return DefaultManager; } 在iOS4之后有了另外一种写法: + (AccountManager *)sharedManager { static A

使用单例时的三种单例写法

单例:一个类只有一个实例,在外部创建对象时,不能用alloc.(只要alloc,就会在堆区开辟空间,就意味着有多个对象)所以我们要提供一个创建对象的方法: 1.加号方法 2.返回值类型为当前类 3.方法名以default ,standared,main,shared等开头 + 当前类名 下面以Person类为例 在.h文件中声明 + (Person *)sharePerson; 在.m文件实现 第一种模式(单线程使用这种模式) + (Person *)sharePerson { 声明为stati

Unity 单例写法

https://www.cnblogs.com/SHOR/p/5192046.html 借鉴自:http://www.cnblogs.com/CodeCabin/p/unity_global_manager.html 实现复杂一些的全局控制,如切换游戏关卡等操作,更常用的方式是使用单例类.单例类的实现又分为两种: 继承自MonoBehaviour的单例类 纯C#的单例类 前者的优点是: 可以在Inspector中显示,便于赋值和查看变量等: 可以利用MonoBehaviour的接口: 可以使用C

我喜欢的两种单例写法

1,第一种: 1 package ToolPackage 2 { 3 /** 4 * 提示 5 * @author tqr <br /> 6 * 创建时间:2014-11-7 下午6:27:10 7 */ 8 public class Tip 9 { 10 private static var instanceB:Boolean=true; 11 private static var instance:Tip; 12 13 public function Tip() 14 { 15 if (i