- 浏览: 73316 次
文章分类
最新评论
-
kevinflynn:
...
ThreadLocal 源码分析 -
kevinflynn:
[url=aaaa][/url]
ThreadLocal 源码分析 -
kevinflynn:
学习到了 感谢楼主。
ThreadLocal 源码分析
/*
* Copyright (c) 1998, 2004, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package com.hanlin.fadp;
import java.lang.ref.*;
/**
* This class extends <tt>ThreadLocal</tt> to provide inheritance of values
* from parent thread to child thread: when a child thread is created, the
* child receives initial values for all inheritable thread-local variables
* for which the parent has values. Normally the child's values will be
* identical to the parent's; however, the child's value can be made an
* arbitrary function of the parent's by overriding the <tt>childValue</tt>
* method in this class.
*
* <p>Inheritable thread-local variables are used in preference to
* ordinary thread-local variables when the per-thread-attribute being
* maintained in the variable (e.g., User ID, Transaction ID) must be
* automatically transmitted to any child threads that are created.
*
* @author Josh Bloch and Doug Lea
* @see ThreadLocal
* @since 1.2
*/
public class InheritableThreadLocal<T> extends ThreadLocal<T> {
/**
* 这个方法留给子类实现
*
* @param parentValue the parent thread's value
* @return the child thread's initial value
*/
protected T childValue(T parentValue) {
return parentValue;
}
/**
* 重写getMap方法,返回的是Thread的inheritableThreadLocals引用
*
* @param t the current thread
*/
ThreadLocalMap getMap(Thread t) {
return t.inheritableThreadLocals;
}
/**
* 重写createMap方法,构造的ThreadLocalMap会传给Thread的inheritableThreadLocals 变量
*
* @param t the current thread
* @param firstValue value for the initial entry of the table.
* @param map the map to store.
*/
void createMap(Thread t, T firstValue) {
t.inheritableThreadLocals = new ThreadLocalMap(this, firstValue);
}
}
它是如何实现父子线程间 ThreadLocal 传递的了?
首先,重写了 getMap 和 createMap 方法,因为 ThreadLocal 在执行 set 方法或者 get 方法的时候,会用到.
是不是发现了啥?这里使用的是 inheritableThreadLocals,而不再是 ThreadLocal 中的 threadLocals.
再看 Thread 类的 init 方法:
private void init(ThreadGroup g, Runnable target, String name,
long stackSize, AccessControlContext acc) {
if (name == null) {
throw new NullPointerException("name cannot be null");
}
this.name = name.toCharArray();
Thread parent = currentThread();
SecurityManager security = System.getSecurityManager();
if (g == null) {
/* Determine if it's an applet or not */
/* If there is a security manager, ask the security manager
what to do. */
if (security != null) {
g = security.getThreadGroup();
}
/* If the security doesn't have a strong opinion of the matter
use the parent thread group. */
if (g == null) {
g = parent.getThreadGroup();
}
}
/* checkAccess regardless of whether or not threadgroup is
explicitly passed in. */
g.checkAccess();
/*
* Do we have the required permissions?
*/
if (security != null) {
if (isCCLOverridden(getClass())) {
security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
}
}
g.addUnstarted();
this.group = g;
this.daemon = parent.isDaemon();
this.priority = parent.getPriority();
if (security == null || isCCLOverridden(parent.getClass()))
this.contextClassLoader = parent.getContextClassLoader();
else
this.contextClassLoader = parent.contextClassLoader;
this.inheritedAccessControlContext =
acc != null ? acc : AccessController.getContext();
this.target = target;
setPriority(priority);
// 这里 inheritableThreadLocals 将不再为空,ThreadLocal.createInheritedMap(parent.inheritableThreadLocals); 语句会把父类中的 ThreadLocal 中的值拷贝一份到子类中.
if (parent.inheritableThreadLocals != null)
this.inheritableThreadLocals =
ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
/* Stash the specified stack size in case the VM cares */
this.stackSize = stackSize;
/* Set thread ID */
tid = nextThreadID();
}
说明:父子线程 ThreadLocal 在拷贝的时候,拷贝的是引用,也就是说如果父线程修改了 ThreadLocal 中的某个对象,那么子线程中 ThreadLocal 的值也会跟着变.
理论分析:
测试:
package com.hanlin.fadp;
public class InheritableThreadLocalTest {
static class Person{
String name;
public Person(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
'}';
}
}
public static ThreadLocal<Person> inheritableThreadLocal = new InheritableThreadLocal<Person>();
static CountDownLatch latch = new CountDownLatch(1);
public static void main(String args[]) throws InterruptedException {
Person tom = new Person("tom");
inheritableThreadLocal.set(tom);
new Thread(new Runnable() {
@Override
public void run() {
try {
Person p = inheritableThreadLocal.get();
p.setName("sdsds");
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("子线程<InheritableThreadLocal>:" + inheritableThreadLocal.get());
latch.countDown();
}
}, "AAAAA").start();
System.out.println("父线程<InheritableThreadLocal>:" + inheritableThreadLocal.get());
Thread.sleep(1000);
// Person p = inheritableThreadLocal.get();
// p.setName("sdsds");
// inheritableThreadLocal.set(p);
latch.await();
System.out.println("父线程<InheritableThreadLocal>:" + inheritableThreadLocal.get());
}
}
结果:
发表评论
-
AtomicInteger 源码分析
2019-01-24 16:11 958AtomicInteger 是如何实现原子操作的了? 答案是 ... -
SynchronousQueue 源码初步分析
2019-01-23 16:56 1195简介: 就是一个 put 操作必须和 take 操作对应. 如 ... -
LinkedBlockingDeque 源码分析
2019-01-22 16:44 1409简介: LinkedBlockingDeque 是一个双端队 ... -
CopyOnWriteArrayList 源码分析
2019-01-19 16:02 1325简介: 写操作时上锁,然后拷贝一个新的数组,操作新数组,将当 ... -
CyclicBarrier 源码分析
2019-01-19 13:55 1466简介: CyclicBarrier 实现这么一个功能,比如说吃 ... -
Semaphore 源码分析
2019-01-18 17:53 1275介绍: Semaphore 用于对某一物理或逻辑资源被同一时间 ... -
ThreadPoolExecutor 源码分析
2019-01-16 11:10 2086首先说明的是,这个和 ReadWriteLock 一样,同样是 ... -
分析 Java 任务的执行
2019-01-13 20:26 2010Java 并发中会大量使用到多线程,那么Java是如何使用多线 ... -
BlockingQueue 源码分析
2019-01-13 20:19 1280public interface BlockingQueue& ... -
LinkedBlockingQueue源码分析
2019-01-13 20:16 576LinkedBlockingQueue 其实实现的是一个生产者 ... -
FutureTask WaitNode 源码分析
2019-01-11 17:24 1924waiter 存放等待的线程,这是一个单链表,没有用 lock ... -
关于 Future 类 boolean cancel(boolean mayInterruptIfRunning) 的疑问
2019-01-11 09:07 0boolean cancel(boolean mayInter ... -
关于 Future 类 boolean cancel(boolean mayInterruptIfRunning) 的疑问
2019-01-10 21:05 2297boolean cancel(boolean mayInter ... -
ThreadLocal 源码分析
2019-01-05 16:18 2225说明: 每个线程内部持有一个 ThreadLocalMap ... -
ReentrantReadWriteLock 简介
2019-01-04 15:54 13891.首先说明 ReentrantReadWriteLock 和 ... -
CountDownLatch 源码分析
2019-01-03 18:31 1062思路分析: CountDownLatch 的思路是:首先申 ... -
ConditionObject 类分析
2018-12-24 14:28 595Condition 实现主要通过两个方法. ① await ...
相关推荐
ThreadLocal、...InheritableThreadLocal作用和ThreadLocal相同,同时增加了一个功能,可以共享父线程InheritableThreadLocal提供的私有部变量,下面从源码角度,分别介绍这两个类。 ThreadLocal 下面是T
分析都是基于android sdk 23 源码进行的,ThreadLocal在android和jdk中的实现可能并不一致。 在最初使用Threadlocal的时候,很容易会产生的误解就是threadlocal就是一个线程。 首先来看下Threadlocal的简单...
NULL 博文链接:https://jnh.iteye.com/blog/2000288
主要为大家详细介绍了java多线程编程之InheritableThreadLocal,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
InheritableThreadLocal跟ThreadLocal差不多,关键点在Thread#init方法中会对Thread中的实例变量inheritableThreadLocals进行赋值操作,将父线程的本地变量复制到子线程中 首先通过ServiceLoader.load方法生成一个...
:pushpin: TransmittableThreadLocal(TTL) :pushpin: | :open_book:中文文档 :wrench:功能 :backhand_index_pointing_right:在使用线程池等会池化复用线程的执行组件情况下,提供ThreadLocal值的...
5. InheritableThreadLocal作用和实现方式 ? 6. InheritableThreadLocal所带来的问题? 7. 如何解决线程池异步值传递问题 ( transmittable-thread-local)? HashMap ConcurrentHashMap相关 9. HashMap为什么线程...