lesson1:threadlocal的使用demo及源码分析
本文中所使用的demo源码地址:https://github.com/mantuliu/javaAdvance 其中的类Lesson1ThreadLocal
本文为java晋级系列的第一讲,后续会陆续推出java相关的高级应用和分析。我个人一直都比较推崇threadlocal的设计原理和实现方式。以下关于threadlocal的描述来源于百度百科:
package com.mantu.advance; /**
* blog http://www.cnblogs.com/mantu/
* github https://github.com/mantuliu/
* @author mantu
*
*/
public class Lesson1ThreadLocal {
public static ThreadLocal<String> local = new ThreadLocal<String>();//声明静态的threadlocal变量
public static ThreadLocal<String> local2 = new ThreadLocal<String>();//声明静态的threadlocal变量
public static void main(String [] args){
for(int i=0;i<5;i++){
TestThread testThread = new TestThread();//创建5个线程
new Thread(testThread).start();
}
} } class TestThread implements Runnable{ @Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(1l);//让线程停顿一下,便于其它线程执行
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Lesson1ThreadLocal.local.set(Thread.currentThread().getId()+":"+System.currentTimeMillis());
Lesson1ThreadLocal.local2.set(Thread.currentThread().getId()+"");
firstStep();
try {
Thread.sleep(1l);//让线程停顿一下,便于其它线程执行
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
secondStep();
try {
Thread.sleep(1l);//让线程停顿一下,便于其它线程执行
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
thirdStep();
try {
Thread.sleep(1l);//让线程停顿一下,便于其它线程执行
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fourthStep();
try {
Thread.sleep(1l);//让线程停顿一下,便于其它线程执行
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fStep();
} public void firstStep(){
System.out.println(Lesson1ThreadLocal.local.get().toString()+":first step");//获取本线程的threadlocal变量值并打印
}
public void secondStep(){
System.out.println(Lesson1ThreadLocal.local.get().toString()+":second step");
}
public void thirdStep(){
System.out.println(Lesson1ThreadLocal.local.get().toString()+":third step");
}
public void fourthStep(){
System.out.println(Lesson1ThreadLocal.local.get().toString()+":fourth step");
}
public void fStep(){
System.out.println(Lesson1ThreadLocal.local.get().toString()+":fifth step");
}
}
代码的主要思路是5个线程,使用了同一个静态的threadlocal变量,每个线程在启动时,存储本线程相关的变量,在后面的5个步骤中都会使用到,展示每个线程的所使用的变量都是独立的,执行结果如下,大家可以自行执行并观察执行结果:
9:1470882533007:first step
11:1470882533023:first step
10:1470882533024:first step
13:1470882533024:first step
9:1470882533007:second step
12:1470882533024:first step
9:1470882533007:third step
12:1470882533024:second step
13:1470882533024:second step
11:1470882533023:second step
10:1470882533024:second step
11:1470882533023:third step
10:1470882533024:third step
12:1470882533024:third step
9:1470882533007:fourth step
13:1470882533024:third step
11:1470882533023:fourth step
10:1470882533024:fourth step
12:1470882533024:fourth step
13:1470882533024:fourth step
9:1470882533007:fifth step
12:1470882533024:fifth step
10:1470882533024:fifth step
13:1470882533024:fifth step
11:1470882533023:fifth step
从执行结果标注红色的部分可以看出,线程id为9的线程在step1到step5的操作过程中,从threadlocal变量中所取到变量值是同一个:9:1470882533007,由此便巧妙的利用了threadlocal变量来实现了在同一个线程内部的变量共享功能,对于同一个变量的操作与其它线程隔离。
下面我们开始分析一下threadlocal的源码,首先从set()方法看起:
public void set(T value) {
Thread t = Thread.currentThread();//获取到当前的线程
ThreadLocalMap map = getMap(t);//通过当前的线程来获取到本线程对应的存储map
if (map != null)
map.set(this, value);//如果map不为空,则在map中存储值value,对应的key为当前的threadlocal对象
else
createMap(t, value);//如果map为空,则创建map,并在map中存储此变量
}
接下来,我们再分析一下createMap()相关的代码
void createMap(Thread t, T firstValue) {
t.threadLocals = new ThreadLocalMap(this, firstValue);//创建ThreadLocalMap,参数为threadlocal变量和之前传递的变量值
}
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);
}
我们再来看看get()的源码
public T get() {
Thread t = Thread.currentThread();//一样的获取当前的线程
ThreadLocalMap map = getMap(t);//因为每个线程都有一个独立的map空间,通过线程获取到这个map
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);//通过key值,也就是我们的local变量来获取到实际的变量值
if (e != null)
return (T)e.value;
}
return setInitialValue();
}
从源码中,我们可以发现,每个线程都有一个独立的存储空间,此空间是一个map,map的key值是我们所使用的threadlocal变量,例如文中的ThreadLocal<String> local 变量,此key对应的值为我们存储的变量Thread.currentThread().getId()+":"+System.currentTimeMillis()。通过下面的图可以更好的帮助大家来理解threadlocal变量中线程、存储空间map、threadlocal变量、存储的变量四者间的关系:一个thread有且只有一个存储空间(map),map会对应多个键值对,其中键为threadlocal变量,值为业务使用的实际变量。

lesson1:threadlocal的使用demo及源码分析的更多相关文章
- lesson2:java阻塞队列的demo及源码分析
本文向大家展示了java阻塞队列的使用场景.源码分析及特定场景下的使用方式.java的阻塞队列是jdk1.5之后在并发包中提供的一组队列,主要的使用场景是在需要使用生产者消费者模式时,用户不必再通过多 ...
- caffe web demo运行+源码分析
caffe web demo学习 1.运行 安装好caffe后,进入/opt/caffe/examples/web_demo/的caffe web demo项目目录,查看一下app.py文件,这是一个 ...
- Bytom Dapp 开发笔记(三):Dapp Demo前端源码分析
本章内容会针对比原官方提供的dapp-demo,分析里面的前端源码,分析清楚整个demo的流程,然后针对里面开发过程遇到的坑,添加一下个人的见解还有解决的方案. 储蓄分红合约简述 为了方便理解,这里简 ...
- ThreadLocal 工作原理、部分源码分析
1.大概去哪里看 ThreadLocal 其根本实现方法,是在Thread里面,有一个ThreadLocal.ThreadLocalMap属性 ThreadLocal.ThreadLocalMap t ...
- cocos2D-x demo 的源码分析 #define ..##.. 的妙用.
最近在看cocos2d-x 但不知道如何下手,于是先看一下他编译的完成的testcpp的源码.发现了下面一段程序 typedef CCLayer* (*NEWTESTFUNC)(); #define ...
- ThreadLocal 线程本地变量 及 源码分析
■ ThreadLocal 定义 ThreadLocal通过为每个线程提供一个独立的变量副本解决了变量并发访问的冲突问题 当使用ThreadLocal维护变量时,ThreadLocal为每个使用该变量 ...
- spring AOP源码分析(一)
对于springAOP的源码分析,我打算分三部分来讲解:1.配置文件的解析,解析为BeanDefination和其他信息然后注册到BeanFactory中:2.为目标对象配置增强行为以及代理对象的生成 ...
- ASimpleCache源码分析
ASimpleCache里只有一个JAVA文件——ACache.java,首先我用思维导图制作了ACache类的详细结构图: 通过分析官方给的demo来驱动源码分析吧 以字符串存储为例(官方给的dem ...
- ThreadLocal详解,ThreadLocal源码分析,ThreadLocal图解
本文脉路: 概念阐释 ----> 原理图解 ------> 源码分析 ------> 思路整理 ----> 其他补充. 一.概念阐述. ThreadLocal 是一个为 ...
随机推荐
- css position 应用(absolute和relative用法)
1.absolute(绝对定位) absolute是生成觉对定位的元素,脱离了文本流(即在文档中已经不占据位置),参照浏览器的左上角通过top,right,bottom,left(简称TRBL) 定位 ...
- css布局之三栏布局
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- 武汉科技大学ACM :1004: A+B for Input-Output Practice (IV)
Problem Description Your task is to Calculate the sum of some integers. Input Input contains multipl ...
- STL 之 vector 用法
一.头文件 #include<vector> 二.常用方法: // 在这个向量的尾部插入x的考贝,平均时间为常数,最坏时间为O(n): 1: void push_back(const T& ...
- EcStore操作笔记
1.去掉首页里面代码: <meta http-equiv="content-type" content="text/html; charset=utf-8" ...
- css画小米、遨游logo
狠简单的2个Logo,用纯css写出来,觉得挺好玩的. <!DOCTYPE html> <html> <head> <meta charset="u ...
- php随机抽奖实例分析
<?php header('Content-type:text/html;charset=utf-8'); /** * 抽奖工具 */ class lottery_tool { protecte ...
- 类似a:hover的伪类的注解
a:link { font-size: 14pt; text-decoration: underline; color: blue; } /*设置a对象在未被访问前的样式表属性 .*/ a:hover ...
- BestCoder Round #36 [B] Gunner
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5199 先对树的高度排序,然后对每次射击高度二分查找即可,打过之后数目变为0. #include< ...
- java-Mysql-SQLServer数据类型匹配速查表
java-Mysql-SQLServer数据类型匹配速查表 Mysql ************************************ 当前列 ClassName ColumnType Di ...