ThreadLocal実装スレッドレベルコンテキスト

25024 ワード

一.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             @Override

26             public void run() {

27                 System.out.println(ThreadScopeData.getInstance());

28 

29             }

30         }).start();

31 

32         new Thread(new Runnable() {

33             @Override

34             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 Administrator

12  * 

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             @Override

11             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             @Override

28             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