Controller是线程安全吗? 安全方式如何

controller是线程不安全的,因为controller使用的是单例模式,不同的线程会对数据进行共享,导致数据混乱,没有实现我们想要的结果,除非定义的是常量,那就没有关系.
因为设计成单例模式的话,就不需要处理太多的gc,性能就可能得到提高

测试案例

package com.walker.springbootdemo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* author:walker
* time: 2022/3/20
* description:  测试controller是否安全
*/
@RestController
@RequestMapping("notSafe")
public class ControllerNotSafeTest {
    //1、定义num,判断不同线程访问的时候,num的返回结果是否一致
    private Integer num=0;

    /**
    * 2、定义两个方法
    */
    @GetMapping("/test1")
    public Integer test1(){
        System.out.println(++num);
        return num;
    }

    @GetMapping("/test2")
    public Integer test2(){
        System.out.println(++num);
        return num;
    }




}

依次调用test1和test2方法
测试结果:发现结果是在test1的结果下进行叠加的

1
2

那么如果想让controller变成线程安全,也就是每个线程独享自己的属性,应该怎么处理呢?

方式一:使用多例模式

增加注解:@Scope("prototype")

package com.walker.springbootdemo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* author:walker
* time: 2022/3/20
* description:  测试controller是否安全
*/
@RestController
@RequestMapping("notSafe")
public class ControllerNotSafeTest {
    //1、定义num,判断不同线程访问的时候,num的返回结果是否一致
    private Integer num=0;

    /**
    * 2、定义两个方法
    */
    @GetMapping("/test1")
    public Integer test1(){
        System.out.println(++num);
        return num;
    }

    @GetMapping("/test2")
    public Integer test2(){
        System.out.println(++num);
        return num;
    }
}

方式二:使用threadLocal进行数据的处理

package com.walker.springbootdemo.controller.safe;

import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* author:walker
* time: 2022/3/20
* description:  测试controller是否安全
*/
@RestController
//修改为多例模式
@Scope("prototype")
@RequestMapping("threadLocalSafe")
public class ControllerSafeThreadLocalTest {
    //1、定义threadLocal 线程独享
    ThreadLocal<Integer> threadLocal=new ThreadLocal<>();

    /**
    * 2、定义两个方法
    */
    @GetMapping("/test1")
    public void test1(){
        threadLocal.set(1);
        System.out.println(threadLocal.get());
    }

    @GetMapping("/test2")
    public void test2(){
        threadLocal.set(2);
        System.out.println(threadLocal.get());
    }

}

执行结果:test1和test2线程可以操纵自己的threadLocal,从而达到数据隔离

1
2
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容