为了凑字,把oracle文档里介绍ThreadLocal抄过来

public class ThreadLocal<T>
extends Object
This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable.ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread (e.g., a user ID or Transaction ID).

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的更多相关文章

  1. Java多线程学习(五)线程间通信知识点补充

    系列文章传送门: Java多线程学习(二)synchronized关键字(1) Java多线程学习(二)synchronized关键字(2) Java多线程学习(三)volatile关键字 Java多 ...

  2. Java多线程学习笔记

    进程:正在执行中的程序,其实是应用程序在内存中运行的那片空间.(只负责空间分配) 线程:进程中的一个执行单元,负责进程汇总的程序的运行,一个进程当中至少要有一个线程. 多线程:一个进程中时可以有多个线 ...

  3. Java多线程学习(转载)

    Java多线程学习(转载) 时间:2015-03-14 13:53:14      阅读:137413      评论:4      收藏:3      [点我收藏+] 转载 :http://blog ...

  4. java多线程学习笔记——详细

    一.线程类  1.新建状态(New):新创建了一个线程对象.        2.就绪状态(Runnable):线程对象创建后,其他线程调用了该对象的start()方法.该状态的线程位于可运行线程池中, ...

  5. 【转】Java多线程学习

    来源:http://www.cnblogs.com/samzeng/p/3546084.html Java多线程学习总结--线程概述及创建线程的方式(1) 在Java开发中,多线程是很常用的,用得好的 ...

  6. JAVA多线程学习笔记(1)

    JAVA多线程学习笔记(1) 由于笔者使用markdown格式书写,后续copy到blog可能存在格式不美观的问题,本文的.mk文件已经上传到个人的github,会进行同步更新.github传送门 一 ...

  7. Java多线程学习(六)Lock锁的使用

    系列文章传送门: Java多线程学习(二)synchronized关键字(1) Java多线程学习(二)synchronized关键字(2) Java多线程学习(三)volatile关键字 Java多 ...

  8. Java多线程学习(四)等待/通知(wait/notify)机制

    转载请备注地址:https://blog.csdn.net/qq_34337272/article/details/79690279 系列文章传送门: Java多线程学习(一)Java多线程入门 Ja ...

  9. Java多线程学习(三)volatile关键字

    转载请备注地址:https://blog.csdn.net/qq_34337272/article/details/79680693 系列文章传送门: Java多线程学习(一)Java多线程入门 Ja ...

随机推荐

  1. MapReduce 2简介

    在Hadoop 1.0版本中,mapred.job.tracker决定了执行MapReduce程序的方式,若设置为local,则使用本地的作业运行器,若设置为主机:端口(eb179:9001),则该配 ...

  2. Python入门1

    简介 Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心开发一个新的脚本解释程序,做为ABC 语言的一种继承.Python ...

  3. 【maven】之使用tomcat7-maven-plugin自动编译的问题

    今天遇到一个奇怪的问题,项目依赖关系如下, 在web端通过tomcat7插件debug模式启动项目的时候,dao层写的代码不能加载,还是原来的代码效果, 然后我在eclipse部署一个tomcat,将 ...

  4. mvc view-controller mvc annotation-driven

    1.mvc view-controller 使页面直接通过某个连接跳转,不进过mvc handler 需要加一个配置 <mvc : view-controller path="/suc ...

  5. READONLY、、cursor、、VARYING

    针对 Transact-SQL 过程的准则:所有 Transact-SQL 数据类型都可以用作参数.您可以使用用户定义的表类型创建表值参数.表值参数只能是 INPUT 参数,并且这些参数必须带有 RE ...

  6. Android API Guides 学习笔记---Application Fundamentals(一)

    今天开始学习google官网上的API guides ,主要读了Application Fundamentals这一章节,此章节介绍了一个App的基本组成,共包括四大部分内容. 1.      App ...

  7. cordova-plugin-unionpay

    https://github.com/Santino-Wu/cordova-plugin-unionpay

  8. Runloop基础知识

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  9. java web须知细节

    1.${pageContext.request.contextPath}是从这个请求路径(URL)上截取你的项目应用名的,比如你的项目名是hello,截取的结果应该就是/hello,/代表http// ...

  10. MVC4.0中项目发布遇到IE11时session存入URL中,导致记不住密码的问题

    ///MVC4.0中项目发布遇到IE11时session存入URL中,导致记不住密码的问题,在webconfig中配置<system.web><authentication mode ...