java多线程学习-ThreadLocal
为了凑字,把oracle文档里介绍ThreadLocal抄过来
public class ThreadLocal<T>
extends Object
For example, the class below generates unique identifiers local to each thread. A thread's id is assigned the first time it invokes ThreadId.get() and remains unchanged on subsequent calls.
import java.util.concurrent.atomic.AtomicInteger; public class ThreadId {
// Atomic integer containing the next thread ID to be assigned
private static final AtomicInteger nextId = new AtomicInteger(0); // Thread local variable containing each thread's ID
private static final ThreadLocal<Integer> threadId =
new ThreadLocal<Integer>() {
@Override protected Integer initialValue() {
return nextId.getAndIncrement();
}
}; // Returns the current thread's unique ID, assigning it if necessary
public static int get() {
return threadId.get();
}
}
Each thread holds an implicit reference to its copy of a thread-local variable as long as the thread is alive and the ThreadLocal instance is accessible; after a thread goes away, all of its copies of thread-local instances are subject to garbage collection (unless other references to these copies exist).
接下来一个示例
import java.util.Random; public class ThreadLocalTest { public static void main(String[] args) {
for (int i = 0; i < 2; i++) { new Thread(new Runnable(){
public void run() {
int age = new Random().nextInt();
MyData md = MyData.getInstance();
md.setAge(age);
md.setName(Thread.currentThread().getName()); System.out.println(Thread.currentThread().getName()+ " age is "+age);
new A().get();
new B().get(); };
}).start(); }
} static class A{
public void get(){
MyData md = MyData.getInstance();
System.out.println(Thread.currentThread().getName()+" A age "+md.getAge() + " name = "+md.getName());
}
} static class B{
public void get(){
MyData md = MyData.getInstance();
System.out.println(Thread.currentThread().getName()+" B age "+md.getAge() + " name = "+md.getName());
}
}
} class MyData{ private MyData(){
} public static MyData md = null;
private static ThreadLocal<MyData> tl = new ThreadLocal<MyData>(); public synchronized static MyData getInstance(){
md = tl.get();
if(null==md){
md = new MyData();
tl.set(md);
}
return md;
} private String name; private int age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} }
想了半天不知如何表达,语文太差。
推荐博客
http://www.cnblogs.com/dolphin0520/p/3920407.html
java多线程学习-ThreadLocal的更多相关文章
- Java多线程学习(五)线程间通信知识点补充
系列文章传送门: Java多线程学习(二)synchronized关键字(1) Java多线程学习(二)synchronized关键字(2) Java多线程学习(三)volatile关键字 Java多 ...
- Java多线程学习笔记
进程:正在执行中的程序,其实是应用程序在内存中运行的那片空间.(只负责空间分配) 线程:进程中的一个执行单元,负责进程汇总的程序的运行,一个进程当中至少要有一个线程. 多线程:一个进程中时可以有多个线 ...
- Java多线程学习(转载)
Java多线程学习(转载) 时间:2015-03-14 13:53:14 阅读:137413 评论:4 收藏:3 [点我收藏+] 转载 :http://blog ...
- java多线程学习笔记——详细
一.线程类 1.新建状态(New):新创建了一个线程对象. 2.就绪状态(Runnable):线程对象创建后,其他线程调用了该对象的start()方法.该状态的线程位于可运行线程池中, ...
- 【转】Java多线程学习
来源:http://www.cnblogs.com/samzeng/p/3546084.html Java多线程学习总结--线程概述及创建线程的方式(1) 在Java开发中,多线程是很常用的,用得好的 ...
- JAVA多线程学习笔记(1)
JAVA多线程学习笔记(1) 由于笔者使用markdown格式书写,后续copy到blog可能存在格式不美观的问题,本文的.mk文件已经上传到个人的github,会进行同步更新.github传送门 一 ...
- Java多线程学习(六)Lock锁的使用
系列文章传送门: Java多线程学习(二)synchronized关键字(1) Java多线程学习(二)synchronized关键字(2) Java多线程学习(三)volatile关键字 Java多 ...
- Java多线程学习(四)等待/通知(wait/notify)机制
转载请备注地址:https://blog.csdn.net/qq_34337272/article/details/79690279 系列文章传送门: Java多线程学习(一)Java多线程入门 Ja ...
- Java多线程学习(三)volatile关键字
转载请备注地址:https://blog.csdn.net/qq_34337272/article/details/79680693 系列文章传送门: Java多线程学习(一)Java多线程入门 Ja ...
随机推荐
- 封装对NPOIExcel的操作,方便的设置导出Excel的样式
下载: http://pan.baidu.com/s/1boTpT5l 使用方法: 导入: 使用 ReadToDataTable方法 导出: NPOIExcel.ExcelManager manger ...
- SQL总结(一)基本查询
SQL总结(一)基本查询 SQL查询的事情很简单,但是常常因为很简单的事情而出错.遇到一些比较复杂的查询我们更是忘记了SQL查询的基本语法. 本文希望通过简单的总结,把常用的查询方法予以总结,希望能够 ...
- express创建项目
sudo apt-get install node-express-generator dave@voctrals:~/WebstormProjects/nodejs-study/express$ e ...
- C#后台程序重启IIS,发邮件通知
应用场景:IIS网站挂掉,系统自动重启IIS,通知相关联系人: 主要代码: 监控类 public class monitoringiis { EmailSend send = new EmailSen ...
- datalist的用法
DataList 提供相关的编辑模板,但和DataGrid不一样的是,DataList没有编辑按钮.要在DataList中使用编辑功能,可在项模板中增加一个按 钮,Linkbutton和Button都 ...
- 发送SMS短信(JSON) 转载
http://blog.csdn.net/ldl22847/article/details/42553883 public static string GetMobileConfByUserId( ...
- sysbench压力测试工具简介和使用(一)
sysbench压力测试工具安装和参数介绍 一.sysbench压力测试工具简介: sysbench是一个开源的.模块化的.跨平台的多线程性能测试工具,可以用来进行CPU.内存.磁盘I/O.线程.数据 ...
- QT学习之路--菜单、工具条、状态栏
下面一些是 Menu Bar,用于显示菜单;再下面一点事 Toolbar areas,用于显示工具条,Status Bar,就是状态栏. Qt 提供了一个 QStatusBar 类来实现状态栏. Qt ...
- [ActionScript 3.0] NetConnection建立客户端与服务器的双向连接
一个客户端与服务器之间的接口测试的工具 <?xml version="1.0" encoding="utf-8"?> <!--- - - - ...
- .net一次连接执行多条sql语句
方法一: string SQLString="select 1; select 2;"; using (OdbcConnection connection = new OdbcCo ...