ThreadLocal学习Demo

时间:2019-03-19
本文章向大家介绍ThreadLocal学习Demo,主要包括ThreadLocal学习Demo使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

使用ThreadLocal就实现了多线程里的单线程。。ThreadLocal<MyThreadEntity2>  threadLocal = new ThreadLocal<>();  MyThreadEntity2 entity = threadLocal.get();  entity 中存放的是单个线程整个生命周期都可以共享的数据。实现了多个线程时单个线程的数据隔离,每个线程都会有自己的ThreadLocal<T>  对象,存放自己的数据。

package com.thc.utils;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 田海超
 * @E-mail  tianhaichao@sinosoft.com.cn
 * @date 2019年3月19日 上午10:32:37  
 * @Description 单个线程整个声明周期的共享参数
 */
public class ThreadLocalUtils2 {
    // MyThreadEntity 这个是自己创建的类,里面可以设置你要存储在该线程中的变量(里面封装了你要放在单个线程整个生命周期都要共享的参数)
    private final static ThreadLocal<MyThreadEntity2> threadLocal = new ThreadLocal<>();

    /**
     * @author 田海超
     * @date 2019年3月19日
     * @return description :通过threadLoal获得我们自己创建的MyThreadEntity的实例(第一次创建)
     */
    public static MyThreadEntity2 getMyThreadEntity() {
        MyThreadEntity2 entity = threadLocal.get();
        if (entity == null) {
            entity = new MyThreadEntity2();
            threadLocal.set(entity);
        }
        return entity;
    }

    /**
     * @author 田海超
     * @date 2019年3月19日 description :创建目的:【清空当前线程内对象】
     */
    public static void remove() {
        threadLocal.remove();
    }

    /**
     * <li>创建人:xiepan</li>
     * <li>创建时间:2018年5月3日</li>
     * <li>创建目的:【获取当前MyThreadEntity中的Map】</li>
     */
    public static Map<String, String> getThreadMap() {
        Map<String, String> map = getMyThreadEntity().getMap();
        if (map == null) {
            map = new HashMap<>();
            getMyThreadEntity().setMap(map);
        }
        return map;
    }

    /**
     * @author 田海超
     * @date 2019年3月19日
     * @param key
     * @param value
     *            description :创建目的:【当前线程内添加key值】
     */
    public static void put(String key, String value) {
        getThreadMap().put(key, value);
    }

    /**
     * @author 田海超
     * @date 2019年3月19日
     * @param key
     * @return description :创建目的:【当前线程内获取value值】
     */
    public static String get(String key) {
        return getThreadMap().get(key);
    }

    public static void main(String[] args) {
        // 使用时,先往线程实例中放值
        ThreadLocalUtils2.getMyThreadEntity().setPlanId("1033");
        ThreadLocalUtils2.getThreadMap().put("aa", "test");
        // 然后在需要用的时候取值
        ThreadLocalUtils2.getMyThreadEntity().getPlanId();
        ThreadLocalUtils2.getThreadMap().get("aa");
    }
    
}
class MyThreadEntity{
    private Map<String, String> map;
    private List<?> list;
    private Date date = new Date();
    private String planId;

    public String getPlanId() {
        return planId;
    }

    public void setPlanId(String planId) {
        this.planId = planId;
    }

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public List<?> getList() {
        return list;
    }

    public void setList(List<?> list) {
        this.list = list;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}
普通demo - ThreadLocalUtils2
package com.sinosig.xb.crms.utils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 田海超
 * @E-mail  tianhaichao@sinosoft.com.cn
 * @date 2019年3月19日 上午10:32:37  
 * @Description 单个线程整个声明周期的共享参数
 */
public class ThreadLocalUtils3 {
    // MyThreadEntity 这个是自己创建的类,里面可以设置你要存储在该线程中的变量(里面封装了你要放在单个线程整个生命周期都要共享的参数)
    private final static ThreadLocal<MyThreadEntity2> threadLocal = new ThreadLocal<>();

    /**
     * @author 田海超
     * @date 2019年3月19日
     * @return description :通过threadLoal获得我们自己创建的MyThreadEntity的实例(第一次创建)
     */
    public static MyThreadEntity2 getMyThreadEntity() {
        MyThreadEntity2 entity = threadLocal.get();
        if (entity == null) {
            entity = new MyThreadEntity2() {
                private Map<String, String> map;
                private List<?> list;
                private Date date = new Date();
                private String planId;

                public String getPlanId() {
                    return planId;
                }

                public void setPlanId(String planId) {
                    this.planId = planId;
                }

                public Map<String, String> getMap() {
                    return map;
                }

                public void setMap(Map<String, String> map) {
                    this.map = map;
                }

                public List<?> getList() {
                    return list;
                }

                public void setList(List<?> list) {
                    this.list = list;
                }

                public Date getDate() {
                    return date;
                }

                public void setDate(Date date) {
                    this.date = date;
                }
            };
            threadLocal.set(entity);
        }
        return entity;
    }

    /**
     * @author 田海超
     * @date 2019年3月19日 description :创建目的:【清空当前线程内对象】
     */
    public static void remove() {
        threadLocal.remove();
    }

    /**
     * <li>创建人:xiepan</li>
     * <li>创建时间:2018年5月3日</li>
     * <li>创建目的:【获取当前线程ConcurrentHashMap】</li>
     */
    public static Map<String, String> getThreadMap() {
        Map<String, String> map = getMyThreadEntity().getMap();
        if (map == null) {
            map = new HashMap<>();
            getMyThreadEntity().setMap(map);
        }
        return map;
    }

    /**
     * @author 田海超
     * @date 2019年3月19日
     * @param key
     * @param value
     *            description :创建目的:【当前线程内添加key值】
     */
    public static void put(String key, String value) {
        getThreadMap().put(key, value);
    }

    /**
     * @author 田海超
     * @date 2019年3月19日
     * @param key
     * @return description :创建目的:【当前线程内获取value值】
     */
    public static String get(String key) {
        return getThreadMap().get(key);
    }

    public static void main(String[] args) {
        // 使用时,先往线程实例中放值
        ThreadLocalUtils3.getMyThreadEntity().setPlanId("1033");
        ThreadLocalUtils3.getThreadMap().put("aa", "test");
        // 然后在需要用的时候取值
        ThreadLocalUtils3.getMyThreadEntity().getPlanId();
        ThreadLocalUtils3.getThreadMap().get("aa");
    }
}
使用内部类的demo - ThreadLocalUtils3

方法里使用内部类:

ThreadLocalUtils3中getThreadMap方法的注解是错的。真正的ConcurrentHashMap是这样的:

参考博文:https://www.cnblogs.com/ITtangtang/p/3948786.html

参考博文:https://blog.csdn.net/qq_38663729/article/details/80058980