Java多线程(四)之线程封闭

线程封闭:将对象封装到一个线程里;

线程封闭的方式:

  • ad-hoc线程封闭:程序控制,最糟糕,忽悠;
  • 堆栈封闭:局部变量、无并发问题(全局的变量,容易引发线程安全问题)
  • TreadLocal线程封闭:特别好的封闭方法。

 我们平时写的代码,没有线程安全问题,是因为堆栈封闭的原因。

TreadLocal的使用:

 1、创建requestHolder

package com.liuhy.test.testapplication.trhreadlocal;

import sun.rmi.runtime.Log;

/**
 * @Auther: liuhy
 * @Date: 2018/11/26 17:33
 */
public class RequestHolder {

    private final static ThreadLocal requestHolder =  new ThreadLocal<>();

    public static void add(Long id){
        requestHolder.set(id);
    }

    public static Long getId(){
        return requestHolder.get();
    }

    public static void remove(){
        requestHolder.remove();
    }

}

2、创建并添加filter

package com.liuhy.test.testapplication.filter;


import com.liuhy.test.testapplication.trhreadlocal.RequestHolder;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Auther: liuhy
 * @Date: 2018/11/26 17:36
 */
@Slf4j
public class HttpFileter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //转化 ServletRequest 为 HttpServletRequest
        HttpServletRequest request=(HttpServletRequest)servletRequest;

        log.info("do filter,{},{}",Thread.currentThread().getId(),request.getServletPath());
        //过滤指定uri请求,并在controller之前将线程id放入TreadLocal中
        RequestHolder.add(Thread.currentThread().getId());
        filterChain.doFilter(request,servletResponse);
    }

    @Override
    public void destroy() {

    }
}


3、添加拦截器

package com.liuhy.test.testapplication.filter;

import com.liuhy.test.testapplication.trhreadlocal.RequestHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Auther: liuhy
 * @Date: 2018/11/26 17:52
 */
@Slf4j
public class HttpInterceptor extends HandlerInterceptorAdapter {

    /**
     * 接口处理前
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        log.info("preHandler");

        return super.preHandle(request, response, handler);
    }

    /**
     * 接口处理后
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //请求结束 移除防止内存泄漏
        RequestHolder.remove();
        log.info("afterHandler");
        super.afterCompletion(request, response, handler, ex);
    }
}

 

4、注册filter以及Intercepter

package com.liuhy.test.testapplication;

import com.liuhy.test.testapplication.filter.HttpFileter;
import com.liuhy.test.testapplication.filter.HttpInterceptor;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Auther: liuhy
 * @Date: 2018/11/26 17:56
 */
@Configuration
public class Config implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HttpInterceptor());
    }

    @Bean
    public FilterRegistrationBean httpFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new HttpFileter());
        registrationBean.addUrlPatterns("/threadLocal/*");
        return registrationBean;
    }
}

5、实例Controller 使用

package com.liuhy.test.testapplication.trhreadlocal;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Auther: liuhy
 * @Date: 2018/11/26 18:02
 */
@Controller
@RequestMapping("/threadLocal")
public class ThreadLocalController {

    @RequestMapping("/test")
    @ResponseBody
    public String getLocal(){
        Long id = RequestHolder.getId();

        return "this is ThreadLocal "+id;
    }
}

 

你可能感兴趣的:(Java多线程问题)