单例模式获取某个类对象(ThreadLocal方式)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CoderTnT/article/details/86080369
public Class ObjClass{

//私有构造方法  防止外部创建
private ObjClass(){
};

//私有需要存储的对象位置
private static ThreadLocal threadLocal= new ThreadLocal();


//公有静态获取实例的方法
public static ObjClass getInstance(){
  Objclass instance=threadLocal.get();
   if(instance==null){
      instance=new  ObjClass();
      threadLocal.set(instance);

}
  return instance;
}

}

猜你喜欢

转载自blog.csdn.net/CoderTnT/article/details/86080369
今日推荐