SpringBoot中使用?ThreadLocal?進(jìn)行多線程上下文管理及注意事項(xiàng)小結(jié)
前言
在多線程編程中,線程安全是一個(gè)重要的問題。Java 提供了多種機(jī)制來處理線程安全問題,其中 ThreadLocal 是一個(gè)非常有用的工具。本文將詳細(xì)介紹 ThreadLocal 的原理及其在多線程上下文管理中的應(yīng)用,并在springboot中使用ThreadLocal保存請求中攜帶的用戶信息。
技術(shù)積累
1.什么是 ThreadLocal
ThreadLocal 是 Java 提供的一個(gè)類,用于在多線程環(huán)境下為每個(gè)線程維護(hù)獨(dú)立的變量副本。這意味著每個(gè)線程都可以獨(dú)立地訪問和修改自己的變量副本,而不會(huì)影響其他線程的變量。
2. ThreadLocal 的原理
2.1 線程隔離
每個(gè)線程都有一個(gè) ThreadLocalMap 對(duì)象,該對(duì)象存儲(chǔ)了所有 ThreadLocal 變量的副本。ThreadLocalMap 是 Thread 類的一個(gè)內(nèi)部類,每個(gè)線程實(shí)例都有一個(gè) ThreadLocalMap 實(shí)例。
2.2 存儲(chǔ)機(jī)制
設(shè)置值: 當(dāng)一個(gè)線程調(diào)用 ThreadLocal.set(value) 時(shí),ThreadLocal 會(huì)將值存儲(chǔ)到當(dāng)前線程的 ThreadLocalMap 中。
獲取值: 調(diào)用 ThreadLocal.get() 時(shí),ThreadLocal 會(huì)從當(dāng)前線程的 ThreadLocalMap 中獲取值。
2.3 內(nèi)存管理
弱引用: ThreadLocalMap 使用弱引用(WeakReference)來存儲(chǔ) ThreadLocal 對(duì)象,以防止內(nèi)存泄漏。
清理: 當(dāng) ThreadLocal 對(duì)象不再被使用時(shí),它可以被垃圾回收,從而避免內(nèi)存泄漏。
3. 使用場景
3.1 用戶會(huì)話管理
在 Web 應(yīng)用中,可以使用 ThreadLocal 存儲(chǔ)用戶會(huì)話信息,確保每個(gè)請求處理線程都能訪問到正確的會(huì)話數(shù)據(jù)。
3.2 事務(wù)上下文管理
在數(shù)據(jù)庫操作中,可以使用 ThreadLocal 存儲(chǔ)事務(wù)上下文,確保每個(gè)線程的操作都在正確的事務(wù)中進(jìn)行。
3.3 線程局部變量
在多線程環(huán)境中,需要每個(gè)線程擁有獨(dú)立的變量副本時(shí),可以使用 ThreadLocal。
4. 示例代碼
以下是一個(gè)簡單的示例,展示了如何使用 ThreadLocal 來管理每個(gè)線程的獨(dú)立變量。
public class ThreadLocalExample { // 創(chuàng)建一個(gè) ThreadLocal 實(shí)例 private static final ThreadLocal threadLocal = new ThreadLocal<>(); public static void main(String[] args) { // 創(chuàng)建多個(gè)線程 for (int i = 0; i < 5; i++) { new Thread(() -> { // 為每個(gè)線程設(shè)置不同的值 threadLocal.set((int) (Math.random() * 100)); try { // 模擬線程執(zhí)行時(shí)間 Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } // 獲取并打印當(dāng)前線程的值 System.out.println("Thread " + Thread.currentThread().getId() + ": " + threadLocal.get()); // 清除 ThreadLocal 變量,避免內(nèi)存泄漏 threadLocal.remove(); }).start(); } } }
4.1 關(guān)鍵點(diǎn)
線程隔離: 每個(gè)線程都有獨(dú)立的 ThreadLocal 變量副本。
內(nèi)存管理: 使用 ThreadLocal.remove() 清除不再需要的變量,避免內(nèi)存泄漏。
性能考慮: ThreadLocal 的使用會(huì)增加一定的內(nèi)存開銷,因此在不需要時(shí)應(yīng)及時(shí)清理。
5. 注意事項(xiàng)
5.1 內(nèi)存泄漏
如果不及時(shí)清理 ThreadLocal 變量,可能會(huì)導(dǎo)致內(nèi)存泄漏。因此,建議在使用完 ThreadLocal 變量后調(diào)用 remove() 方法。
5.2 線程池
在使用線程池時(shí),線程可能會(huì)被重用。如果 ThreadLocal 變量沒有被清理,可能會(huì)導(dǎo)致后續(xù)任務(wù)訪問到錯(cuò)誤的數(shù)據(jù)。因此,在使用線程池時(shí),務(wù)必在任務(wù)執(zhí)行完畢后清理 ThreadLocal 變量。
實(shí)戰(zhàn)演示
1. User 類
User 類表示用戶數(shù)據(jù)
/** * User * @author senfel * @version 1.0 * @date 2025/2/18 17:00 */ @AllArgsConstructor @NoArgsConstructor @Data public class User { private String id; private String username; }
2. UserContext 類
UserContext 類使用 ThreadLocal 來存儲(chǔ)和刪除用戶數(shù)據(jù)。
/** * UserContext * @author senfel * @version 1.0 * @date 2025/2/18 17:01 */ public class UserContext { // 創(chuàng)建一個(gè) ThreadLocal 實(shí)例來存儲(chǔ) User 對(duì)象 private static final ThreadLocal<User> userThreadLocal = new ThreadLocal<>(); // 設(shè)置用戶數(shù)據(jù) public static void setUser(User user) { userThreadLocal.set(user); } // 獲取用戶數(shù)據(jù) public static User getUser() { return userThreadLocal.get(); } // 刪除用戶數(shù)據(jù) public static void clearUser() { userThreadLocal.remove(); } }
3.UserInterceptor 類
UserInterceptor 類用于在請求處理前后設(shè)置和清除 ThreadLocal 中的用戶數(shù)據(jù)。
/** * UserInterceptor * @author senfel * @version 1.0 * @date 2025/2/18 17:03 */ @Component public class UserInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 模擬從請求中獲取用戶數(shù)據(jù) String userId = request.getParameter("userId"); String username = request.getParameter("username"); if (userId == null || username == null) { response.getWriter().write("User ID and Username are required."); return false; } // 創(chuàng)建 User 對(duì)象并存儲(chǔ)在 ThreadLocal 中 User user = new User(userId, username); UserContext.setUser(user); return true; } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // 清除 ThreadLocal 中的用戶數(shù)據(jù),避免內(nèi)存泄漏 UserContext.clearUser(); } }
4.配置攔截器
在 Spring Boot 中配置攔截器,使其在請求處理前后執(zhí)行。
/** * WebConfig * @author senfel * @version 1.0 * @date 2025/2/18 17:10 */ @Configuration public class WebConfig implements WebMvcConfigurer { @Autowired private UserInterceptor userInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(userInterceptor).addPathPatterns("/**"); }
5.實(shí)戰(zhàn)測試
隨便訪問一個(gè)路徑,都會(huì)從請求中獲取用戶信息并放入local,在執(zhí)行控制器結(jié)束后會(huì)清理掉數(shù)據(jù)。
總結(jié)
ThreadLocal 是一個(gè)非常強(qiáng)大的工具,可以幫助我們在多線程環(huán)境中管理線程局部變量。通過合理使用 ThreadLocal,可以有效地避免線程安全問題,提高程序的并發(fā)性能和穩(wěn)定性。我們可以在 Spring Boot 應(yīng)用中安全地存儲(chǔ)和管理每個(gè)請求的用戶數(shù)據(jù),并通過顯式地清理 ThreadLocal 變量,可以有效避免內(nèi)存泄漏問題。
到此這篇關(guān)于SpringBoot中使用 ThreadLocal 進(jìn)行多線程上下文管理及其注意事項(xiàng)的文章就介紹到這了,更多相關(guān)SpringBoot ThreadLocal多線程上下文管理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot在filter中如何用threadlocal存放用戶身份信息
- SpringBoot中的ThreadLocal保存請求用戶信息的實(shí)例demo
- springboot登錄攔截器+ThreadLocal實(shí)現(xiàn)用戶信息存儲(chǔ)的實(shí)例代碼
- SpringBoot ThreadLocal 簡單介紹及使用詳解
- SpringBoot+ThreadLocal+AbstractRoutingDataSource實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源
- Springboot公共字段填充及ThreadLocal模塊改進(jìn)方案
- SpringBoot ThreadLocal實(shí)現(xiàn)公共字段自動(dòng)填充案例講解
- SpringBoot通過ThreadLocal實(shí)現(xiàn)登錄攔截詳解流程
- springboot 使用ThreadLocal的實(shí)例代碼
相關(guān)文章
SpringMVC數(shù)據(jù)輸出相關(guān)知識(shí)總結(jié)
今天帶大家學(xué)習(xí)SpringMVC的相關(guān)知識(shí),文中對(duì)SpringMVC數(shù)據(jù)輸出作了非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)的小伙伴們很有幫助,需要的朋友可以參考下2021-06-06SpringBoot @Value注解支持配置自動(dòng)刷新能力擴(kuò)展方式
本文介紹了如何通過自定義注解和BeanPostProcessor實(shí)現(xiàn)SpringBoot中@Value注解的配置自動(dòng)刷新能力,主要步驟包括:定義一個(gè)支持動(dòng)態(tài)刷新的注解,實(shí)現(xiàn)配置的動(dòng)態(tài)變更,以及通過BeanPostProcessor掃描并刷新使用@Value注解的變量2024-12-12SpringBoot之groups應(yīng)對(duì)不同的Validation規(guī)則自定義方式
這篇文章主要介紹了SpringBoot之groups應(yīng)對(duì)不同的Validation規(guī)則自定義方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10Springboot項(xiàng)目異常處理及返回結(jié)果統(tǒng)一
這篇文章主要介紹了Springboot項(xiàng)目異常處理及返回結(jié)果統(tǒng)一,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08SpringCloud中的Feign服務(wù)間的調(diào)用詳解
這篇文章主要介紹了SpringCloud中的Feign服務(wù)間的調(diào)用詳解,Feign 是一個(gè)聲明式的 REST 客戶端,它能讓 REST 調(diào)用更加簡單,Feign 供了 HTTP 請求的模板,通過編寫簡單的接口和插入注解,就可以定義好 HTTP 請求的參數(shù)、格式、地址等信息,需要的朋友可以參考下2024-01-01SpringCloud Gateway的路由,過濾器和限流解讀
這篇文章主要介紹了SpringCloud Gateway的路由,過濾器和限流解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02