首页 > 代码库 > ThreadLocal实现线程级上下文

ThreadLocal实现线程级上下文

一.ThreadLocal测试

 1 package com.junge.threadlocal.context; 2  3 /** 4  * @author Administrator 5  *  6  */ 7 public class ThreadScopeData { 8     private ThreadScopeData() { 9 10     }11 12     private static ThreadLocal<ThreadScopeData> threadLocal = new ThreadLocal<ThreadScopeData>();13 14     public static ThreadScopeData getInstance() {15         if (null == threadLocal.get()) {16             threadLocal.set(new ThreadScopeData());17         }18 19         return threadLocal.get();20     }21 22     public static void main(String[] args) {23 24         new Thread(new Runnable() {25             @Override26             public void run() {27                 System.out.println(ThreadScopeData.getInstance());28 29             }30         }).start();31 32         new Thread(new Runnable() {33             @Override34             public void run() {35                 System.out.println(ThreadScopeData.getInstance());36 37             }38         }).start();39     }40 }

 

二.对上下接口进行封装,使用方便(线程绑定不同的业务数据,线程之间的业务数据放在上下文中)

1.上下文接口ThreadContext

package com.junge.threadlocal.context;import java.util.Map;/** * 线程上下文接口 *  * @author Administrator *  */public interface ThreadContext {    /**     * 保持用户名     *      * @param userName     */    void setUserName(String userName);    /**     * 获取用户名     *      * @return     */    String getUserName();    /**     * 保持参数1     *      * @param param     */    void setParam1(Object param);    /**     * 获取参数1     *      * @return     */    Object getParam1();    /**     * 添加参数     *      * @param key     * @param param     */    void addParam(String key, Object param);    /**     * 获取参数     *      * @param key     * @return     */    Object getParam(String key);    /**     * 获取所有添加的参数     *      * @return     */    Map<String, Object> getAllParam();}

2.线程上下文接口实现ThreadContextImpl

package com.junge.threadlocal.context;import java.util.HashMap;import java.util.Map;/** * 线程上下文接口实现 *  * @author Administrator *  */public class ThreadContextImpl implements ThreadContext {    private String userName;    private Object param1;    private Map<String, Object> paramMap;    @Override    public void setUserName(String userName) {        this.userName = userName;    }    @Override    public String getUserName() {        return this.userName;    }    @Override    public void setParam1(Object param) {        this.param1 = param;    }    @Override    public Object getParam1() {        return param1;    }    /*     * (non-Javadoc)     *      * @see     * com.junge.threadlocal.context.ThreadContext#addParam(java.lang.String,     * java.lang.Object)     */    @Override    public void addParam(String key, Object param) {        if (null == paramMap) {            paramMap = new HashMap<String, Object>();        }        paramMap.put(key, param);    }    /*     * (non-Javadoc)     *      * @see     * com.junge.threadlocal.context.ThreadContext#getParam(java.lang.String)     */    @Override    public Object getParam(String key) {        if (null == paramMap) {            paramMap = new HashMap<String, Object>();        }        return paramMap.get(key);    }    /*     * (non-Javadoc)     *      * @see com.junge.threadlocal.context.ThreadContext#getAllParam()     */    @Override    public Map<String, Object> getAllParam() {        if (null == paramMap) {            paramMap = new HashMap<String, Object>();        }        return paramMap;    }}

3.线程上下文助手ThreadContextHolder

 1 /** 2  *  3  */ 4 package com.junge.threadlocal.context; 5  6 import java.util.Map; 7  8 /** 9  * 线程上下文助手10  * 11  * @author Administrator12  * 13  */14 public class ThreadContextHolder {15 16     /**17      * 上下文保持对象18      */19     private static ThreadLocal<ThreadContext> threadContext = new ThreadLocal<ThreadContext>();20 21     public static void setThreadContext(ThreadContext context) {22         threadContext.set(context);23     }24 25     public static ThreadContext getThreadContext() {26         if (null == threadContext.get()) {27             threadContext.set(new ThreadContextImpl());28         }29         return threadContext.get();30     }31 32     public static void setUserName(String userName) {33         getThreadContext().setUserName(userName);34     }35 36     public static String getUserName() {37         return getThreadContext().getUserName();38     }39 40     public static void setParam1(Object param) {41         getThreadContext().setParam1(param);42     }43 44     public static Object getParam1() {45         return getThreadContext().getParam1();46     }47 48     public static void addParam(String key, Object param) {49         getThreadContext().addParam(key, param);50     }51 52     public static Object getParam(String key) {53         return getThreadContext().getParam(key);54     }55 56     public static Map<String, Object> getAllParam() {57         return getThreadContext().getAllParam();58     }59 60 }

4.线程上下文测试代码
 1 package com.junge.threadlocal.context; 2  3 public class ThreadContextHolderTest { 4  5     /** 6      * @param args 7      */ 8     public static void main(String[] args) { 9         new Thread(new Runnable() {10             @Override11             public void run() {12                 ThreadContextHolder.setUserName("userName1");13                 ThreadContextHolder.setParam1(new Object());14                 ThreadContextHolder.addParam("param1", "aaaa");15 16                 System.out.println(Thread.currentThread().getName() + ":"17                         + ThreadContextHolder.getUserName());18                 System.out.println(Thread.currentThread().getName() + ":"19                         + ThreadContextHolder.getParam1());20                 System.out.println(Thread.currentThread().getName() + ":"21                         + ThreadContextHolder.getParam("param1"));22 23             }24         }).start();25 26         new Thread(new Runnable() {27             @Override28             public void run() {29                 ThreadContextHolder.setUserName("userName2");30                 ThreadContextHolder.setParam1(new Object());31                 ThreadContextHolder.addParam("param1", "bbbb");32 33                 System.out.println(Thread.currentThread().getName() + ":"34                         + ThreadContextHolder.getUserName());35                 System.out.println(Thread.currentThread().getName() + ":"36                         + ThreadContextHolder.getParam1());37                 System.out.println(Thread.currentThread().getName() + ":"38                         + ThreadContextHolder.getParam("param1"));39 40             }41         }).start();42 43     }44 45 }
运行结果:
Thread-0:userName1
Thread-0:java.lang.Object@6e1408
Thread-0:aaaa
Thread-1:userName2
Thread-1:java.lang.Object@e53108
Thread-1:bbbb


 

 

ThreadLocal实现线程级上下文