本地线程变量【原】

 

 

 本地缓存变量之缓存功能

package com.bobo;


import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.Map;

/**
 * 线程独立的本地线程缓存
 *
 * @author King
 * @date 2017-11-15
 */
public class ThreadLocalCache {
    private static ThreadLocal<Map> threadLocal = new ThreadLocal<Map>();

    //设置缓存方式一(一般)
    public static void set(Map map) {
        threadLocal.set(map);
    }

    //设置缓存方式二(推荐)
    public static void set(Object key, Object value) {
        Map map = threadLocal.get();
        if (map == null) {
            map = new HashMap();
        }
        map.put(key, value);
        set(map);
    }

    //获取指定缓存
    public static Object get(Object key) {
        Map cache = threadLocal.get();
        if (cache != null) {
            return cache.get(key);
        } else {
            return null;
        }
    }

    //获取所有缓存
    public static Map get() {
        return threadLocal.get();
    }

    //清除缓存
    public static void clear() {
        threadLocal.remove();
    }

    //移除特定缓存
    public static void remove(Object key) {
        Map cache = threadLocal.get();
        if (cache != null) {
            cache.remove(key);
        }
    }

    //打印缓存(无前缀)
    public void printCache() {
        printCache("");
    }

    //打印缓存(有前缀)
    public void printCache(String prefix) {
        ThreadLocalCache cache = new ThreadLocalCache();
        System.out.println(prefix + " " + JSON.toJSONString(cache.get()));
    }

    // ------ ↑上方为标准使用类↑ ------ 分割线 ------ ↓下方为样例(可删除)(可移到其它class类中)↓ ------

    public static void main(String[] args) {
        ThreadLocalCache cache = new ThreadLocalCache();
        try {
            cache.method1();//设置缓存key1
            cache.printCache("current thread");//打印缓存
            cache.printCacheInNewThread();//新线程中打印缓存

            cache.method2();//设置缓存key2
            cache.printCache("current thread");//打印缓存
            cache.printCacheInNewThread();//新线程中打印缓存

            cache.method3();//设置缓存key3
            cache.printCache("current thread");//打印缓存
            cache.printCacheInNewThread();//新线程中打印缓存
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cache.clear();//必须在finally中执行清除缓存操作,目的是为了防止 try 语句块中发生异常导致的缓存堆积
            cache.printCache("current thread");//打印缓存
            cache.printCacheInNewThread();//新线程中打印缓存
        }
    }

    public void method1() {
        ThreadLocalCache cache = new ThreadLocalCache();
        Map map = new HashMap();
        map.put("key1", "value1");
        cache.set(map);//设置缓存
        cache.get("key1");//获取缓存
    }

    public void method2() {
        ThreadLocalCache cache = new ThreadLocalCache();
        cache.set("key2", "value2");//设置缓存
        cache.get("key2");//获取缓存
    }

    public void method3() {
        ThreadLocalCache cache = new ThreadLocalCache();
        cache.get().put("key3", "value3");
        cache.get("key3");//获取缓存
    }

    public void method4() {
        ThreadLocalCache cache = new ThreadLocalCache();
        cache.clear();//清除缓存
    }

    public void printCacheInNewThread() {
        Thread newThread = new Thread(new Runnable() {
            @Override
            public void run() {
                ThreadLocalCache cache = new ThreadLocalCache();
                cache.set("newKey", "newValue");
                cache.printCache("new thread ");//打印缓存
            }
        });
        newThread.start();
    }
}

 

 

 

进阶用法

线程本地变量ThreadLocal (耗时工具)【原】==>https://www.cnblogs.com/whatlonelytear/p/4919883.html

posted @ 2016-05-18 16:08  苦涩泪滴  阅读(88)  评论(0编辑  收藏  举报