FastThreadLocal传递本地变量

206 阅读2分钟

FastThreadLocal传递本地变量

FastThreadLocal传递本地变量,比ThreadLocal省略了销毁变量这一步,速度看情况,反正为了省事。 话不多说,直接上测试代码。

先新建一个全局变量的类,里面定义传参所需的各个字段

import io.netty.util.concurrent.FastThreadLocal;
import lombok.Data;
import lombok.experimental.UtilityClass;

import java.util.*;

/**
 * 本地变量常量
 *
 * @author zed
 */
@UtilityClass
public class FastContext {

    /**
     * FastThreadLocal基于netty,设置全局上下文
     */
    public static final FastThreadLocal<Params> CONTEXT = new FastThreadLocal<>();

    /**
     * 建立一个实体以传递所需的东西
     */
    @Data
    public static class Params {
        private String contextStr;
        private Integer contextInt;
        private Long contextLong;
        private Date contextDate;
        private List<String> contextList = new ArrayList<>();
        private Map<String, Object> contextMap = new HashMap<>();

        // 其他字段 ...

    }

}

直接写个接口测试这个类


import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Maps;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 测试本地变量传递
 *
 * @author zed
 */
@Slf4j
@RestController
@RequestMapping(value = "/fast")
public class FastContextTestController {


    @ApiOperation("test")
    @GetMapping("/test")
    public void test() {
        // 每次先调用,看看是否每次都是新创建的值
        FastContext.Params context = FastContext.CONTEXT.get();
        log.info("context:" + context);


        Map<String, Object> map = Maps.newHashMap();
        map.put("id", 1L);
        map.put("name", "朝花夕拾");
        map.put("desc", "鲁迅著");
        map.put("time", DateUtil.date());

        // 模拟放个值进去
        FastContext.Params cc = new FastContext.Params();
        cc.setContextMap(map);
        FastContext.CONTEXT.set(cc);

        // 看看存进去的值在不在
        FastContext.Params context1 = FastContext.CONTEXT.get();
        log.info("context1:" + context1);


        // 方法之间调用,直接取自己设置的本地变量
        checkOne();

    }


    public void checkOne() {
        FastContext.Params localContext1 = FastContext.CONTEXT.get();
        localContext1.setContextInt(111);

        log.info("checkOne:" + localContext1);

        checkTwo();

    }


    public void checkTwo() {

        FastContext.Params localContext1 = FastContext.CONTEXT.get();
        localContext1.setContextLong(666L);

        log.info("checkTwo:" + localContext1);
        log.info("I'm a genius");

    }
}

调用两次接口看看日志

14:25:17.831 logback [http-nio-7777-exec-1] INFO  c.p.z.l.FastContextTestController - context1:FastContext.Params(contextStr=null, contextInt=null, contextLong=null, contextDate=null, contextList=[], contextMap={name=朝花夕拾, id=1, time=2023-09-01 14:25:17, desc=鲁迅著})
14:25:17.831 logback [http-nio-7777-exec-1] INFO  c.p.z.l.FastContextTestController - checkOne:FastContext.Params(contextStr=null, contextInt=111, contextLong=null, contextDate=null, contextList=[], contextMap={name=朝花夕拾, id=1, time=2023-09-01 14:25:17, desc=鲁迅著})
14:25:17.831 logback [http-nio-7777-exec-1] INFO  c.p.z.l.FastContextTestController - checkTwo:FastContext.Params(contextStr=null, contextInt=111, contextLong=666, contextDate=null, contextList=[], contextMap={name=朝花夕拾, id=1, time=2023-09-01 14:25:17, desc=鲁迅著})
14:25:17.831 logback [http-nio-7777-exec-1] INFO  c.p.z.l.FastContextTestController - I'm a genius
14:25:39.582 logback [http-nio-7777-exec-2] INFO  c.p.z.l.FastContextTestController - context:null
14:25:39.586 logback [http-nio-7777-exec-2] INFO  c.p.z.l.FastContextTestController - context1:FastContext.Params(contextStr=null, contextInt=null, contextLong=null, contextDate=null, contextList=[], contextMap={name=朝花夕拾, id=1, time=2023-09-01 14:25:39, desc=鲁迅著})
14:25:39.588 logback [http-nio-7777-exec-2] INFO  c.p.z.l.FastContextTestController - checkOne:FastContext.Params(contextStr=null, contextInt=111, contextLong=null, contextDate=null, contextList=[], contextMap={name=朝花夕拾, id=1, time=2023-09-01 14:25:39, desc=鲁迅著})
14:25:39.589 logback [http-nio-7777-exec-2] INFO  c.p.z.l.FastContextTestController - checkTwo:FastContext.Params(contextStr=null, contextInt=111, contextLong=666, contextDate=null, contextList=[], contextMap={name=朝花夕拾, id=1, time=2023-09-01 14:25:39, desc=鲁迅著})
14:25:39.590 logback [http-nio-7777-exec-2] INFO  c.p.z.l.FastContextTestController - I'm a genius

完活,谢谢观赏。