首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

懂得ThreadLocal的涵义

2013-10-13 
理解ThreadLocal的涵义import java.util.HashMapimport java.util.Mappublic class CustomThreadLocal {

理解ThreadLocal的涵义
import java.util.HashMap;import java.util.Map;public class CustomThreadLocal { private static Map threadMap = new HashMap(); public static void add(Object object) { threadMap.put(Thread.currentThread(), object); } public static void remove(Object object) { threadMap.remove(Thread.currentThread()); } public static Object get() { return threadMap.get(Thread.currentThread()); }}

?

public class ThreadContext { private String userId; private Long transactionId; public String getUserId() {  return userId; } public void setUserId(String userId) {  this.userId = userId; } public Long getTransactionId() {  return transactionId; } public void setTransactionId(Long transactionId) {  this.transactionId = transactionId; } public String toString() {  return 'userId:' + userId + ',transactionId:' + transactionId; }}

?

?

public class ThreadLocalMainSampleEx1 { public static void main(String[] args) {  new Thread(new Runnable() {   public void run() {    ThreadContext threadContext = new ThreadContext();    threadContext.setTransactionId(1l);    threadContext.setUserId('User 1');    CustomThreadLocal.add(threadContext);    //here we call a method where the thread context is not passed as parameter    PrintThreadContextValues.printThreadContextValues();   }  }).start();  new Thread(new Runnable() {   public void run() {    ThreadContext threadContext = new ThreadContext();    threadContext.setTransactionId(2l);    threadContext.setUserId('User 2');    CustomThreadLocal.add(threadContext);    //here we call a method where the thread context is not passed as parameter    PrintThreadContextValues.printThreadContextValues();   }  }).start(); }}

?当我CustomThreadLocal.get()时候,它使用通过内部map去寻找到它自身独有Context上下文信息。

那如果采纳使用ThreadLocal呢?

public class ThreadContext { private String userId; private Long transactionId; private static ThreadLocal threadLocal = new ThreadLocal(){  @Override        protected ThreadContext initialValue() {            return new ThreadContext();        } }; public static ThreadContext get() {  return threadLocal.get(); } public String getUserId() {  return userId; } public void setUserId(String userId) {  this.userId = userId; } public Long getTransactionId() {  return transactionId; } public void setTransactionId(Long transactionId) {  this.transactionId = transactionId; } public String toString() {  return 'userId:' + userId + ',transactionId:' + transactionId; }}

?是不是挺简单的使用的呢?如果配置上Pool,是不是挺完美的,在N多框架中都基本上采用上述方式,ibatis、BoneCP等都是携带

?

ThreadLocal另外用处是对于非线程安全的实例,通过ThreadLocal的包装同样能够保证并发下的安全。

package ccs.progest.javacodesamples.threadlocal.ex4;import java.text.SimpleDateFormat;import java.util.Date;public class ThreadLocalDateFormat { // SimpleDateFormat is not thread-safe, so each thread will have one private static final ThreadLocal formatter = new ThreadLocal() {  @Override  protected SimpleDateFormat initialValue() {   return new SimpleDateFormat('MM/dd/yyyy');  } }; public String formatIt(Date date) {  return formatter.get().format(date); }}

?ThreadLocal的应用场景

1.类似于userId、tractionId、以及session等

2.为了保证每个线程更好独享自己实例信息

热点排行