java Thread之ThreadLocal(线程局部变量)
2012-06-05 09:26
302 查看
ThreadLocal即线程局部变量。
功能:为每个使用该变量的线程,提供一个该变量的副本。
其实ThreadLocal中拥有一个Map,它是线程->变量的映射。
主要方法有三个,set,get,initValue。
set与get的作用不用多讲,主要是initValue,它默认返回的是null,子类可以通过重载该方法,来改变默认值。
1 public void set(T value) {
2 Thread t = Thread.currentThread();
3 ThreadLocalMap map = getMap(t);
4 if (map != null)
5 map.set(this, value);
6 else
7 createMap(t, value);
8 }
9
public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null)
return (T)e.value;
}
return setInitialValue();
}
protected T initialValue() {
return null;
}
使用示例:
1 package com.cody.test.thread;
2
3 public class ThreadLocalTest {
4
5 private static ThreadLocal<String> mThreadLocal = new ThreadLocal<String>();
6 private static Business business = new Business();
7 public static void main(String[] args) {
8 new Thread(new Runnable() {
9
@Override
public void run() {
business.sub();
}
}).start(); //开始一个线程
business.main();
}
public static class Business{
public void sub(){
for(int i = 0;i < 10;i ++){
mThreadLocal.set(String.valueOf(i));
try {
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " : " + mThreadLocal.get());
}
}
public void main(){
for(int i = 0;i < 20;i ++){
mThreadLocal.set(String.valueOf(i));
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " : " + mThreadLocal.get());
}
}
}
46 }
功能:为每个使用该变量的线程,提供一个该变量的副本。
其实ThreadLocal中拥有一个Map,它是线程->变量的映射。
主要方法有三个,set,get,initValue。
set与get的作用不用多讲,主要是initValue,它默认返回的是null,子类可以通过重载该方法,来改变默认值。
1 public void set(T value) {
2 Thread t = Thread.currentThread();
3 ThreadLocalMap map = getMap(t);
4 if (map != null)
5 map.set(this, value);
6 else
7 createMap(t, value);
8 }
9
public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null)
return (T)e.value;
}
return setInitialValue();
}
protected T initialValue() {
return null;
}
使用示例:
1 package com.cody.test.thread;
2
3 public class ThreadLocalTest {
4
5 private static ThreadLocal<String> mThreadLocal = new ThreadLocal<String>();
6 private static Business business = new Business();
7 public static void main(String[] args) {
8 new Thread(new Runnable() {
9
@Override
public void run() {
business.sub();
}
}).start(); //开始一个线程
business.main();
}
public static class Business{
public void sub(){
for(int i = 0;i < 10;i ++){
mThreadLocal.set(String.valueOf(i));
try {
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " : " + mThreadLocal.get());
}
}
public void main(){
for(int i = 0;i < 20;i ++){
mThreadLocal.set(String.valueOf(i));
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " : " + mThreadLocal.get());
}
}
}
46 }
相关文章推荐
- java实现线程局部变量:ThreadLocal
- ThreadLocal<T> Java线程局部变量
- 详解Java中的ThreadLocal、ThreadLocalMap和Thread之间的关系
- java学习——ThreadLocal 线程局部 (thread-local) 变量的使用
- Java 之ThreadLocal 对应C#之ThreadStatic
- Java Thread&Concurrency(10): 深入理解ThreadLocal及其实现原理
- Java多线程探究-线程局部变量ThreadLocal
- Java多线程(四) Thread和Runnable之区别(售票系统)
- 执行打的maven jar包时出现“Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes”
- 问题描述 Exception in thread "main" java.lang.OutOfMemoryError: Java heap space 解决方案[转] 一直都知道可以设置jvm he
- 【Java并发】(二) 线程同步之Thread.join()、CountDownLatch、CyclicBarrier
- Exception in thread "main " java.lang.NoClassDefFoundError:XXX
- 明述java中线程(thread)的使用(2)
- Java: 第一次尝试Java,Thread类
- JAVA中runnable和thread区别
- What is Java thread priority? 什么是java线程优先级
- java多线程 基础(二) Thread Runnable
- Thread.java
- 关于Exception in thread "AWT-EventQueue-1" java.lang.NoSuchMethodError的处理
- eclipse:Exception in thread "http-bio-8080-exec-11" java.lang.OutOfMemoryError: PermGen space