1. 程式人生 > >Java 多線程下的單例模式

Java 多線程下的單例模式

處理方式 復雜 數據庫 使用 read 對象實例 訪問 -s pac

單例對象(Singleton)是一種常用的設計模式。在Java應用中,單例對象能保證在一個JVM中,該對象只有一個實例存在。正是由於這個特 點,單例對象通常作為程序中的存放配置信息的載體,因為它能保證其他對象讀到一致的信息。例如在某個服務器程序中,該服務器的配置信息可能存放在數據庫或 文件中,這些配置數據由某個單例對象統一讀取,服務進程中的其他對象如果要獲取這些配置信息,只需訪問該單例對象即可。這種方式極大地簡化了在復雜環境 下,尤其是多線程環境下的配置管理,但是隨著應用場景的不同,也可能帶來一些同步問題。   本文將探討一下在多線程環境下,使用單例對象作配置信息管理時可能會帶來的幾個同步問題,並針對每個問題給出可選的解決辦法。   問題描述

  在多線程環境下,單例對象的同步問題主要體現在兩個方面,單例對象的初始化和單例對象的屬性更新。   本文描述的方法有如下假設:   1. 單例對象的屬性(或成員變量)的獲取,是通過單例對象的初始化實現的。也就是說,在單例對象初始化時,會從文件或數據庫中讀取最新的配置信息。   2. 其他對象不能直接改變單例對象的屬性,單例對象屬性的變化來源於配置文件或配置數據庫數據的變化。   1.1 單例對象的初始化   首先,討論一下單例對象的初始化同步。單例模式的通常處理方式是,在對象中有一個靜態成員變量,其類型就是單例類型本身;如果該變量為null,則創建該單例類型的對象,並將該變量指向這個對象;如果該變量不為null,則直接使用該變量。   其過程如下面代碼所示:

Java代碼 技術分享圖片
  1. public class GlobalConfig {
  2. private static GlobalConfig instance = null;
  3. private Vector properties = null;
  4. private GlobalConfig() {
  5. //Load configuration information from DB or file
  6. //Set values for properties
  7. }
  8. public static GlobalConfig getInstance() {
  9. if (instance == null) {
  10. instance = new GlobalConfig();
  11. }
  12. return instance;
  13. }
  14. public Vector getProperties() {
  15. return properties;
  16. }
  17. }

這種處理方式在單線程的模式下可以很好的運行;但是在多線程模式下,可能產生問題。如果第一個線程發現成員變量為null,準備創建對象;這是第二 個線程同時也發現成員變量為null,也會創建新對象。這就會造成在一個JVM中有多個單例類型的實例。如果這個單例類型的成員變量在運行過程中變化,會 造成多個單例類型實例的不一致,產生一些很奇怪的現象。例如,某服務進程通過檢查單例對象的某個屬性來停止多個線程服務,如果存在多個單例對象的實例,就 會造成部分線程服務停止,部分線程服務不能停止的情況。   1.2 單例對象的屬性更新   通常,為了實現配置信息的實時更新,會有一個線程不停檢測配置文件或配置數據庫的內容,一旦發現變化,就更新到單例對象的屬性中。在更新這些信 息的時候,很可能還會有其他線程正在讀取這些信息,造成意想不到的後果。還是以通過單例對象屬性停止線程服務為例,如果更新屬性時讀寫不同步,可能訪問該 屬性時這個屬性正好為空(null),程序就會拋出異常。   解決方法   2.1 單例對象的初始化同步   對於初始化的同步,可以通過如下代碼所采用的方式解決。

Java代碼 技術分享圖片
  1. public class GlobalConfig {
  2. private static GlobalConfig instance = null;
  3. private Vector properties = null;
  4. private GlobalConfig() {
  5. //Load configuration information from DB or file
  6. //Set values for properties
  7. }
  8. private static synchronized void syncInit() {
  9. if (instance == null) {
  10. instance = new GlobalConfig();
  11. }
  12. }
  13. public static GlobalConfig getInstance() {
  14. if (instance == null) {
  15. syncInit();
  16. }
  17. return instance;
  18. }
  19. public Vector getProperties() {
  20. return properties;
  21. }
  22. }

這種處理方式雖然引入了同步代碼,但是因為這段同步代碼只會在最開始的時候執行一次或多次,所以對整個系統的性能不會有影響。 2.2 單例對象的屬性更新同步   為了解決第2個問題,有兩種方法:   1,參照讀者/寫者的處理方式   設置一個讀計數器,每次讀取配置信息前,將計數器加1,讀完後將計數器減1.只有在讀計數器為0時,才能更新數據,同時要阻塞所有讀屬性的調用。代碼如下。

Java代碼 技術分享圖片
  1. public class GlobalConfig {
  2. private static GlobalConfig instance;
  3. private Vector properties = null;
  4. private boolean isUpdating = false;
  5. private int readCount = 0;
  6. private GlobalConfig() {
  7. //Load configuration information from DB or file
  8. //Set values for properties
  9. }
  10. private static synchronized void syncInit() {
  11. if (instance == null) {
  12. instance = new GlobalConfig();
  13. }
  14. }
  15. public static GlobalConfig getInstance() {
  16. if (instance==null) {
  17. syncInit();
  18. }
  19. return instance;
  20. }
  21. public synchronized void update(String p_data) {
  22. syncUpdateIn();
  23. //Update properties
  24. }
  25. private synchronized void syncUpdateIn() {
  26. while (readCount > 0) {
  27. try {
  28. wait();
  29. } catch (Exception e) {
  30. }
  31. }
  32. }
  33. private synchronized void syncReadIn() {
  34. readCount++;
  35. }
  36. private synchronized void syncReadOut() {
  37. readCount--;
  38. notifyAll();
  39. }
  40. public Vector getProperties() {
  41. syncReadIn();
  42. //Process data
  43. syncReadOut();
  44. return properties;
  45. }
  46. }

2,采用"影子實例"的辦法

  具體說,就是在更新屬性時,直接生成另一個單例對象實例,這個新生成的單例對象實例將從數據庫或文件中讀取最新的配置信息;然後將這些配置信息直接賦值給舊單例對象的屬性。如下面代碼所示。

Java代碼 技術分享圖片
  1. public class GlobalConfig {
  2. private static GlobalConfig instance = null;
  3. private Vector properties = null;
  4. private GlobalConfig() {
  5. //Load configuration information from DB or file
  6. //Set values for properties
  7. }
  8. private static synchronized void syncInit() {
  9. if (instance = null) {
  10. instance = new GlobalConfig();
  11. }
  12. }
  13. public static GlobalConfig getInstance() {
  14. if (instance = null) {
  15. syncInit();
  16. }
  17. return instance;
  18. }
  19. public Vector getProperties() {
  20. return properties;
  21. }
  22. public void updateProperties() {
  23. //Load updated configuration information by new a GlobalConfig object
  24. GlobalConfig shadow = new GlobalConfig();
  25. properties = shadow.getProperties();
  26. }
  27. }

註意:在更新方法中,通過生成新的GlobalConfig的實例,從文件或數據庫中得到最新配置信息,並存放到properties屬性中。

  上面兩個方法比較起來,第二個方法更好,首先,編程更簡單;其次,沒有那麽多的同步操作,對性能的影響也不大。

Java 多線程下的單例模式