第四夢 多例模式
阿新 • • 發佈:2018-06-14
http lis RM ron stat created img 環境 spl
邂逅多例
多例在我們日常編寫程序過程中可能相對用到得少一些,但是其起到的作用並不小。就比如我們日常接觸到的數據庫連接池就是多例模式的一種實現,還有EJB無狀態會話Bean的實例池。
為什麽使用多例
在多並發的請求環境下,系統需要為每個客戶端的獨立請求提供單獨服務的資源,但是系統開銷總是有限的,系統在並發量很大時也不可能為所有的並發請求提供相應的資源。這個時候就考慮使用多例的方式來分配資源回收資源再分配,從而達到減輕系統的壓力。
代碼操作
1、提供容器類,也就是池的概念
2、通過讀取配置好的一些屬性來進行對池進行初始化
3、程序請求時,先判斷池中有無空閑的鏈接資源,有則返回該資源,沒有,則新建連接對象。
簡單的代碼實戰(比池的實現簡單的多)
1、定義MultitionPattern類,用來生產固定數量的實例化對象(註意看我的註釋)
1 package com.yrml.multition; 2 3 import com.google.common.collect.Lists; 4 5 import java.util.List; 6 import java.util.Random; 7 8 /** 9 * @author created by John Tan on 2018/6/14 10 */ 11 public class MultitionPattern { 12View Code13 /** 14 * 最多實例化個數 15 */ 16 private static final int MAX_NUMBER_OF_MUTITION = 3; 17 private static List<String> multitionInfoList = Lists.newArrayListWithExpectedSize(MAX_NUMBER_OF_MUTITION); 18 private static List<MultitionPattern> multitionList = Lists.newArrayListWithExpectedSize(MAX_NUMBER_OF_MUTITION);19 /** 20 * 當前使用的實例化對象 21 */ 22 private static int countNumberOfMultition = 0; 23 24 //首先把所有的對象都實例化 25 static { 26 for(int i = 0; i < MAX_NUMBER_OF_MUTITION; i++){ 27 multitionList.add(new MultitionPattern("Multition" + (i+1) + "號")); 28 } 29 } 30 31 private MultitionPattern() { 32 } 33 34 private MultitionPattern(String info) { 35 multitionInfoList.add(info); 36 } 37 38 /** 39 * 隨機的獲取一個實例化對象 40 * @return 實例化對象 41 */ 42 public static MultitionPattern getInstance(){ 43 Random random = new Random(); 44 countNumberOfMultition = random.nextInt(MAX_NUMBER_OF_MUTITION); 45 return multitionList.get(countNumberOfMultition); 46 } 47 48 /** 49 * 通過對象標識獲取指定的實例化對象 50 * @param multition 對象標識 51 * @return 實例化對象 52 */ 53 public static MultitionPattern getInstance(int multition){ 54 countNumberOfMultition = multition; 55 return multitionList.get(countNumberOfMultition); 56 } 57 58 /** 59 * 該實例化對象相關的信息 60 * @return information 61 */ 62 public String multitionInfo(){ 63 return multitionInfoList.get(countNumberOfMultition); 64 } 65 66 }
2、通過Caller對實例化好的對象進行調用(兩種方式調用【隨機&指定】)
1 package com.yrml.multition; 2 3 /** 4 * @author created by John Tan on 2018/6/14 5 */ 6 public class Caller { 7 8 private static final int CALLER_NUMBER = 8; 9 10 private static void randomCaller(){ 11 System.out.println("------------random---------------"); 12 for (int i = 0; i < CALLER_NUMBER; i++) { 13 MultitionPattern multitionPattern = MultitionPattern.getInstance(); 14 System.out.println("第" + (i+1) + "個調用:" + multitionPattern.multitionInfo()); 15 } 16 } 17 18 private static void pointCaller(){ 19 System.out.println("------------point---------------"); 20 for (int i = 0; i < CALLER_NUMBER; i++) { 21 MultitionPattern multitionPattern = null; 22 if(i < 3){ 23 multitionPattern = MultitionPattern.getInstance(0); 24 } else if(i < 5){ 25 multitionPattern = MultitionPattern.getInstance(1); 26 } else { 27 multitionPattern = MultitionPattern.getInstance(2); 28 } 29 System.out.println("第" + (i+1) + "個調用:" + multitionPattern.multitionInfo()); 30 } 31 } 32 33 public static void main(String[] args) { 34 Caller.randomCaller(); 35 Caller.pointCaller(); 36 } 37 }View Code
GitHub位置
實戰demo請點擊這裏。
寫在後面
後期有必要看一下各種工具對線程池的實現,對比學習一下,理解應該會深些,Mark、、
第四夢 多例模式