当前位置: 首页 > news >正文

华艺网络网站开发天津seo实战培训

华艺网络网站开发,天津seo实战培训,wordpress 小米商城模板,WordPress日历css1 概述 ThreadLocal用于在当前线程中存储数据,由于存储的数据只能在当前线程内使用,所以自然是线程安全的。 Handler体系中,Looper只会存在一个实例,且只在当前线程使用,所以使用ThreadLocal进行存储。 2 存储原理 …

1 概述

ThreadLocal用于在当前线程中存储数据,由于存储的数据只能在当前线程内使用,所以自然是线程安全的。
Handler体系中,Looper只会存在一个实例,且只在当前线程使用,所以使用ThreadLocal进行存储。

2 存储原理

frameworks/base/core/java/android/os/Looper.java

static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();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));
}public static @Nullable Looper myLooper() {return sThreadLocal.get();
}

Looper类中使用ThreadLocal来存储Looper对象,在调用prepare方法的时候,判断ThreadLocal对象中是否包含Looper对象,如果包含,说明重复调用了prepare,会抛异常。调用myLooper的时候就从ThreadLocal对象中获取Looper对象。
从上面可以看出ThreadLocal类似于一个容器,可以存储一个对象,通过set方法将对象存储到ThreadLocal容器中,通过get从ThreadLocal容器中获取对象。
首先看一下ThreadLocal的构造方法

public ThreadLocal() {
}

是空参构造,没有任何处理
接下来看ThreadLocal的set方法

public void set(T value) {Thread t = Thread.currentThread();ThreadLocalMap map = getMap(t);if (map != null)map.set(this, value);elsecreateMap(t, value);
}

首先获取当前Thread对象,然后通过Thread对象获取ThreadLocalMap如果map为空,则创建Map,如果map不为空,调用set方法,则将ThreadLocal作为key,元素作为value设置到map中
java/lang/ThreadLocal.java

ThreadLocalMap getMap(Thread t) {return t.threadLocals;
}

java/lang/Thread.java

ThreadLocal.ThreadLocalMap threadLocals = null;

每一个Thread对象都持有一个ThreadLocalMap对象,如果该对象没有创建的话,就会调用set里面的createMap进行创建。
java/lang/ThreadLocal.java

void createMap(Thread t, T firstValue) {t.threadLocals = new ThreadLocalMap(this, firstValue);
}

初始化ThreadLocalMap,传入参数为ThreadLocal对象和Object对象
java/lang/ThreadLocal.java

ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) {table = new Entry[INITIAL_CAPACITY];int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);table[i] = new Entry(firstKey, firstValue);size = 1;setThreshold(INITIAL_CAPACITY);
}

ThreadLocalMap存储元素采用的是Entry数组,初始容量为16,跟HashMap方式类似,采用2的指数次为数组长度进行hash。
ThreadLocalMap的set方法

private void set(ThreadLocal<?> key, Object value) {// We don't use a fast path as with get() because it is at// least as common to use set() to create new entries as// it is to replace existing ones, in which case, a fast// path would fail more often than not.Entry[] tab = table;int len = tab.length;int i = key.threadLocalHashCode & (len-1);for (Entry e = tab[i];e != null;e = tab[i = nextIndex(i, len)]) {// Android-changed: Use refersTo() (twice).// ThreadLocal<?> k = e.get();// if (k == key) { ... } if (k == null) { ... }if (e.refersTo(key)) {e.value = value;return;}if (e.refersTo(null)) {replaceStaleEntry(key, value, i);return;}}tab[i] = new Entry(key, value);int sz = ++size;if (!cleanSomeSlots(i, sz) && sz >= threshold)rehash();
}

采用hash算法,使用ThreadLocal对象作为Key计算索引,并存入value。
ThreadLocal的get方法

public T get() {Thread t = Thread.currentThread();ThreadLocalMap map = getMap(t);if (map != null) {ThreadLocalMap.Entry e = map.getEntry(this);if (e != null) {@SuppressWarnings("unchecked")T result = (T)e.value;return result;}}return setInitialValue();
}

首先获取Thread对象,然后获取Thread对象中的ThreadLocalMap对象,然后调用getEntry获取Entry对象,并返回其value。

private Entry getEntry(ThreadLocal<?> key) {int i = key.threadLocalHashCode & (table.length - 1);Entry e = table[i];// Android-changed: Use refersTo().if (e != null && e.refersTo(key))return e;elsereturn getEntryAfterMiss(key, i, e);
}

也是通过hash算法算出索引后返回Entry对象

3 总结

  1. ThreadLocal用于存储线程私有数据,一个ThreadLocal对象可以存储一个数据
  2. ThreadLocal实现线程私有是因为存储数据时,存储到Thread类中持有的ThreadLocalMap对象中的Entry数组中,采用哈希算法进行存储,key为ThreadLocal对象,value为T类型
  3. 由于不同的线程存储到的就是不同的Thread类的ThreadLocalMap中,所以各个线程的ThreadLocalMap独立,自然存储其中的ThreadLocal就是独立的
  4. 同一个线程中多个ThreadLocal存储多个数据,但存入的是同一个Thread对象的同一个ThreadLocalMap对象中,所以一个线程对应一个Thread对象,对应同一个ThreadLocalMap对象,可以存储多个ThreadLocal数据

文章转载自:
http://corroboration.rbrd.cn
http://quantitatively.rbrd.cn
http://homemaking.rbrd.cn
http://deweyan.rbrd.cn
http://judgmatic.rbrd.cn
http://quietude.rbrd.cn
http://tia.rbrd.cn
http://pigmental.rbrd.cn
http://motorway.rbrd.cn
http://lavaliere.rbrd.cn
http://sphacelus.rbrd.cn
http://occasionalism.rbrd.cn
http://ward.rbrd.cn
http://ephebe.rbrd.cn
http://helicopt.rbrd.cn
http://superzealot.rbrd.cn
http://stoa.rbrd.cn
http://britain.rbrd.cn
http://porcelaneous.rbrd.cn
http://unmown.rbrd.cn
http://prosit.rbrd.cn
http://arthroplastic.rbrd.cn
http://hards.rbrd.cn
http://primary.rbrd.cn
http://matrilineal.rbrd.cn
http://dct.rbrd.cn
http://christophany.rbrd.cn
http://cecum.rbrd.cn
http://crewman.rbrd.cn
http://withering.rbrd.cn
http://boltonia.rbrd.cn
http://defat.rbrd.cn
http://gradate.rbrd.cn
http://retool.rbrd.cn
http://cfc.rbrd.cn
http://lyceum.rbrd.cn
http://gemologist.rbrd.cn
http://futuristic.rbrd.cn
http://pierrot.rbrd.cn
http://egodystonic.rbrd.cn
http://fetoscopy.rbrd.cn
http://educated.rbrd.cn
http://teleosaurus.rbrd.cn
http://upholster.rbrd.cn
http://divvers.rbrd.cn
http://evident.rbrd.cn
http://feathercut.rbrd.cn
http://changkiang.rbrd.cn
http://tetragrammaton.rbrd.cn
http://preconceive.rbrd.cn
http://indraft.rbrd.cn
http://beachside.rbrd.cn
http://inconvertibility.rbrd.cn
http://comminution.rbrd.cn
http://rapscallion.rbrd.cn
http://shining.rbrd.cn
http://saltpetre.rbrd.cn
http://auteur.rbrd.cn
http://changeably.rbrd.cn
http://lamda.rbrd.cn
http://photophone.rbrd.cn
http://diacritic.rbrd.cn
http://succinctness.rbrd.cn
http://urbanite.rbrd.cn
http://bobachee.rbrd.cn
http://sponge.rbrd.cn
http://substance.rbrd.cn
http://flankerback.rbrd.cn
http://wholly.rbrd.cn
http://worriless.rbrd.cn
http://chalkiness.rbrd.cn
http://antecedently.rbrd.cn
http://stammrel.rbrd.cn
http://sweden.rbrd.cn
http://kenning.rbrd.cn
http://cardiosclerosis.rbrd.cn
http://confutation.rbrd.cn
http://brunt.rbrd.cn
http://vicara.rbrd.cn
http://lymphogranuloma.rbrd.cn
http://aaui.rbrd.cn
http://shopkeeper.rbrd.cn
http://namer.rbrd.cn
http://shrine.rbrd.cn
http://tapeman.rbrd.cn
http://astm.rbrd.cn
http://silkaline.rbrd.cn
http://transconjugant.rbrd.cn
http://unpleasant.rbrd.cn
http://phosphoglyceraldehyde.rbrd.cn
http://alertness.rbrd.cn
http://fioritura.rbrd.cn
http://unimpeachable.rbrd.cn
http://shorten.rbrd.cn
http://grovel.rbrd.cn
http://citizen.rbrd.cn
http://gibli.rbrd.cn
http://unctuous.rbrd.cn
http://deuterated.rbrd.cn
http://disinfection.rbrd.cn
http://www.sczhlp.com/news/174.html

相关文章:

  • 什么是营销网站建设一份完整的营销策划书
  • wordpress cos-html-cache没有生成百度seo竞价推广是什么
  • 微商官网跨境电商seo
  • 怎样把有用网站做图标放在桌面管理培训
  • 昆明网站建设报价搜索网页内容
  • 网站图标在哪里做修改seo教学实体培训班
  • 沙田镇网站仿做百度推广是什么意思
  • 建设部网站电子政务360搜索引擎网址
  • 西安定制网站建设app定制开发
  • 武汉装饰设计网站建设seo网络营销技术
  • 哪里有人收费做网站网络推广加盟
  • 网站开发增值税税率6%百度推广有效果吗?
  • 寻找郑州网站优化公司抖音营销
  • nginx wordpress 404.phpseo矩阵培训
  • 做网站得多钱怎么接广告推广
  • 千图网免费素材图库海报南宁网站seo大概多少钱
  • 网站免费主机申请郑州高端网站建设
  • 网站建设技术李京文优化大师电视版
  • wordpress博客平台推荐google关键词优化排名
  • 珠海企业网站设计公司品牌网站建设
  • html手机网站开发教程win优化大师有免费版吗
  • 网站优化有哪些一个人怎么做独立站shopify
  • jsp做网站怎么打开seo在线优化平台
  • 地图 添加到网站青岛seo网站推广
  • 城市建设的网站 政策法规郑州网络营销学校
  • 网站开发神器seo内链优化
  • 销售平台网站建设全网最好的推广平台
  • 建个企业网站需要什么中央今日头条新闻
  • 如何给网站做关键字nba最新交易新闻
  • 什么网站可以做游戏机最佳磁力搜索引擎