spring中的bean是線程安全的嗎?
spring 中的 bean 是線程安全的嗎?
Spring 不保證 bean 的執行緒安全性。
預設 spring 容器中的 bean 是單例的。當單例中存在競態條件,即有線程安全問題。 如下面的範例
計數類別
package constxiong.interview.threadsafe; /** * 计数类 * @author ConstXiong * @date 2019-07-16 14:35:40 */ public class Counter { private int count = 0; public void addAndPrint() { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(++count); } }
#spring 設定檔
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="counter" class="constxiong.interview.threadsafe.Counter" /> </beans>
測試類別
package constxiong.interview.threadsafe; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class CounterTest { public static void main(String[] args) { final ApplicationContext context = new ClassPathXmlApplicationContext("spring_safe.xml"); for (int i = 0; i < 10; i++) { new Thread(){ @Override public void run() { Counter counter = (Counter)context.getBean("counter"); for (int j = 0; j < 1000; j++) { counter.addAndPrint(); } } }.start(); } } }
列印結果開頭和結尾
1 5 7 4 2 6 3 8 9 . . . 9818 9819 9820 9821 9822 9823 9824 9825
期望列印出的最大值應該是10000
修改spring 設定文件,把bean 的作用域改為prototype
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="counter" class="constxiong.interview.threadsafe.Counter" scope="prototype"/> </beans>
測試結果輸出10個1000
即每個執行緒都建立了一個Counter 對象,執行緒內獨自計數,不存在執行緒安全性問題。但不是我們想要的結果,印出 10000。
所以 spring 管理的 bean 的線程安全跟 bean 的創建作用域和 bean 所在的使用環境是否存在競態條件有關,spring 並不能保證 bean 的線程安全。
以上是spring中的bean是線程安全的嗎?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

Java 8引入了Stream API,提供了一種強大且表達力豐富的處理數據集合的方式。然而,使用Stream時,一個常見問題是:如何從forEach操作中中斷或返回? 傳統循環允許提前中斷或返回,但Stream的forEach方法並不直接支持這種方式。本文將解釋原因,並探討在Stream處理系統中實現提前終止的替代方法。 延伸閱讀: Java Stream API改進 理解Stream forEach forEach方法是一個終端操作,它對Stream中的每個元素執行一個操作。它的設計意圖是處

膠囊是一種三維幾何圖形,由一個圓柱體和兩端各一個半球體組成。膠囊的體積可以通過將圓柱體的體積和兩端半球體的體積相加來計算。本教程將討論如何使用不同的方法在Java中計算給定膠囊的體積。 膠囊體積公式 膠囊體積的公式如下: 膠囊體積 = 圓柱體體積 兩個半球體體積 其中, r: 半球體的半徑。 h: 圓柱體的高度(不包括半球體)。 例子 1 輸入 半徑 = 5 單位 高度 = 10 單位 輸出 體積 = 1570.8 立方單位 解釋 使用公式計算體積: 體積 = π × r2 × h (4

Spring Boot簡化了可靠,可擴展和生產就緒的Java應用的創建,從而徹底改變了Java開發。 它的“慣例慣例”方法(春季生態系統固有的慣例),最小化手動設置
