Skip to content

Latest commit

 

History

History
47 lines (34 loc) · 1.06 KB

ThreadLocal Tip.md

File metadata and controls

47 lines (34 loc) · 1.06 KB

ThreadLocal Tip 정리

public class UserThreadLocal {
    
    private static final ThreadLocal<String> threadLocal;
    
    static {
        threadLocal = new ThreadLocal<>();
    }
    
    public static String get() {
        return threadLocal.get();
    }
    
    public static void set(String userId) {
        threadLocal.set(userId);
    } 
    
    public static void remove() {
        threadLocal.remove();
    }
}

위와 같이 ThreadLocal을 사용할 수 있다.


Interceptor ThreadLocal

public class AuthInterceptor implements HandlerInterceptor {
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        UserThreadLocal.set(); // set 하기
    }
    
    @Override
    public boolean postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        UserThreadLocal.remove(); // 사용했던 ThreadLocal 제거해주기
    }
}

인터셉터를 사용하면서 ThreadLocal 팁 정리