ThreadLocal與線程成員變量還有區別
准確的說
下面就做個例子
一
定義了MyThreadLocal類
/**
* 使用了ThreadLocal的類
*
* @author leizhimin
*/
public class MyThreadLocal {
//定義了一個ThreadLocal變量
private ThreadLocal<Integer> tl = new ThreadLocal<Integer>() {
@Override
protected Integer initialValue() {
return
}
};
public Integer getNextNum() {
//將tl的值獲取後加
tl
return tl
}
}
/**
* 測試線程
*
* @author leizhimin
*/
public class TestThread extends Thread {
private MyThreadLocal tlt = new MyThreadLocal();
public TestThread(MyThreadLocal tlt) {
this
}
@Override
public void run() {
for (int i =
System
}
}
}
/**
* ThreadLocal測試
*
* @author leizhimin
*/
public class Test {
public static void main(String[] args) {
MyThreadLocal tlt = new MyThreadLocal();
Thread t
Thread t
Thread t
Thread t
t
t
t
t
}
}
可以看出
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Process finished with exit code
tlt對象是一個
二
假如不用ThreadLocal
/**
* 使用了ThreadLocal的類
*
* @author leizhimin
*/
public class MyThreadLocal {
private Integer t
public Integer getNextNum(){
return t
}
// //定義了一個ThreadLocal變量
// private ThreadLocal<Integer> tl = new ThreadLocal<Integer>() {
// @Override
// protected Integer initialValue() {
// return
// }
// };
//
// public Integer getNextNum() {
// //將tl的值獲取後加
// tl
// return tl
// }
}
然後運行測試
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Process finished with exit code
從這裡可以看出
三
package com
import java
import java
import java
/**
* 使用了ThreadLocal的類
*
* @author leizhimin
*/
public class MyThreadLocal {
//定義了一個ThreadLocal變量
private com
@Override
protected Integer initialValue() {
return
}
};
public Integer getNextNum() {
//將tl的值獲取後加
tl
return tl
}
}
class ThreadLocal<T> {
private Map<Thread
public ThreadLocal() {
}
protected T initialValue() {
return null;
}
public T get() {
Thread t = Thread
T obj = map
if (obj == null && !ntainsKey(t)) {
obj = initialValue();
map
}
return obj;
}
public void set(T value) {
map
}
public void remove() {
map
}
}
運行測試
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Process finished with exit code
很意外
四
其實從程序角度看
是因為使用了Integer嗎?
原因是
為了看清本質
package com
import java
import java
import java
/**
* 使用了ThreadLocal的類
*
* @author leizhimin
*/
public class MyThreadLocal {
//定義了一個ThreadLocal變量
// private ThreadLocal<Bean> tl = new ThreadLocal<Bean>() {
private com
@Override
protected Bean initialValue() {
return new Bean();
}
};
@Override
public String toString() {
return
}
public Bean getBean() {
return tl
}
}
class ThreadLocal<T> {
private Map<Thread
public ThreadLocal() {
}
protected T initialValue() {
return null;
}
public T get() {
Thread t = Thread
T obj = map
if (obj == null && !ntainsKey(t)) {
obj = initialValue();
map
}
return obj;
}
public void set(T value) {
map
}
public void remove() {
map
}
}
package com
/**
* 測試Bean
*
* @author leizhimin
*/
public class Bean {
private String id =
private String name =
public Bean() {
}
public Bean(String id
this
this
}
public String getId() {
return id;
}
public void setId(String id) {
this
}
public String getName() {
return name;
}
public void setName(String name) {
this
}
public String showinfo() {
return
}
}
package com
/**
* 測試線程
*
* @author leizhimin
*/
public class TestThread extends Thread {
private MyThreadLocal tlt = new MyThreadLocal();
public TestThread(MyThreadLocal tlt) {
this
}
@Override
public void run() {
System
for (int i =
System
}
}
}
然後運行測試
>>>>>:MyThreadLocal{tl=com
>>>>>:MyThreadLocal{tl=com
>>>>>:MyThreadLocal{tl=com
>>>>>:MyThreadLocal{tl=com
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Process finished with exit code
從打印結果很清楚的看到
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27562.html