鍒涘缓鍨嬭璁℃ā寮?鍗曚緥妯″紡

Posted 娣卞叆鍘熺悊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鍒涘缓鍨嬭璁℃ā寮?鍗曚緥妯″紡相关的知识,希望对你有一定的参考价值。

鐐瑰嚮馃憜钃濊壊鈥?nbsp;娣卞叆婧愮爜鈥濓紝鍏虫敞骞垛€?/span>璁句负鏄熸爣鈥?/span>

鎶€鏈共璐э紝绗竴鏃堕棿鎺ㄩ€?/span>

涓轰粈涔堣浣跨敤鍗曚緥锛?/h2>

鍗曚緥璁捐妯″紡锛圫ingleton Design Pattern锛夌悊瑙h捣鏉ラ潪甯哥畝鍗曘€備竴涓被鍙厑璁稿垱寤轰竴涓璞★紙鎴栬€呭疄渚嬶級锛岄偅杩欎釜绫诲氨鏄竴涓崟渚嬬被锛岃繖绉嶈璁℃ā寮忓氨鍙綔鍗曚緥璁捐妯″紡锛岀畝绉板崟渚嬫ā寮忋€?/span>

瀵逛簬鍗曚緥鐨勬蹇碉紝鎴戣寰楁病蹇呰瑙i噴澶锛屼綘涓€鐪嬪氨鑳芥槑鐧姐€傛垜浠噸鐐圭湅涓€涓嬶紝涓轰粈涔堟垜浠渶瑕佸崟渚嬭繖绉嶈璁℃ā寮忥紵瀹冭兘瑙e喅鍝簺闂锛熸帴涓嬫潵鎴戦€氳繃涓や釜瀹炴垬妗堜緥鏉ヨ瑙c€?/span>

瀹炴垬妗堜緥涓€锛氬鐞嗚祫婧愯闂啿绐?/h3>

鎴戜滑鍏堟潵鐪嬬涓€涓緥瀛愩€傚湪杩欎釜渚嬪瓙涓紝鎴戜滑鑷畾涔夊疄鐜颁簡涓€涓線鏂囦欢涓墦鍗版棩蹇楃殑 Logger 绫汇€傚叿浣撶殑浠g爜瀹炵幇濡備笅鎵€绀猴細


public class Logger { private FileWriter writer;
public Logger() { File file = new File("/Users/wangzheng/log.txt"); writer = new FileWriter(file, true); //true琛ㄧず杩藉姞鍐欏叆 }
public void log(String message) { writer.write(mesasge); }}
// Logger绫荤殑搴旂敤绀轰緥锛?/span>public class UserController { private Logger logger = new Logger();
public void login(String username, String password) { // ...鐪佺暐涓氬姟閫昏緫浠g爜... logger.log(username + " logined!"); }}
public class OrderController { private Logger logger = new Logger();
public void create(OrderVo order) { // ...鐪佺暐涓氬姟閫昏緫浠g爜... logger.log("Created an order: " + order.toString()); }}


鐪嬪畬浠g爜涔嬪悗锛屽厛鍒潃鎬ョ湅鎴戜笅闈㈢殑璁茶В锛屼綘鍙互鍏堟€濊€冧竴涓嬶紝杩欐浠g爜瀛樺湪浠€涔堥棶棰樸€?/span>

鍦ㄤ笂闈㈢殑浠g爜涓紝鎴戜滑娉ㄦ剰鍒帮紝鎵€鏈夌殑鏃ュ織閮藉啓鍏ュ埌鍚屼竴涓枃浠?/Users/wangzheng/log.txt 涓€傚湪 UserController 鍜?OrderController 涓紝鎴戜滑鍒嗗埆鍒涘缓涓や釜 Logger 瀵硅薄銆傚湪 Web 瀹瑰櫒鐨?Servlet 澶氱嚎绋嬬幆澧冧笅锛屽鏋滀袱涓?Servlet 绾跨▼鍚屾椂鍒嗗埆鎵ц login() 鍜?create() 涓や釜鍑芥暟锛屽苟涓斿悓鏃跺啓鏃ュ織鍒?log.txt 鏂囦欢涓紝閭e氨鏈夊彲鑳藉瓨鍦ㄦ棩蹇椾俊鎭簰鐩歌鐩栫殑鎯呭喌銆?/span>

涓轰粈涔堜細鍑虹幇浜掔浉瑕嗙洊鍛紵鎴戜滑鍙互杩欎箞绫绘瘮鐫€鐞嗚В銆傚湪澶氱嚎绋嬬幆澧冧笅锛屽鏋滀袱涓嚎绋嬪悓鏃剁粰鍚屼竴涓叡浜彉閲忓姞 1锛屽洜涓哄叡浜彉閲忔槸绔炰簤璧勬簮锛屾墍浠ワ紝鍏变韩鍙橀噺鏈€鍚庣殑缁撴灉鏈夊彲鑳藉苟涓嶆槸鍔犱簡 2锛岃€屾槸鍙姞浜?1銆傚悓鐞嗭紝杩欓噷鐨?log.txt 鏂囦欢涔熸槸绔炰簤璧勬簮锛屼袱涓嚎绋嬪悓鏃跺線閲岄潰鍐欐暟鎹紝灏辨湁鍙兘瀛樺湪浜掔浉瑕嗙洊鐨勬儏鍐点€?/span>

閭e浣曟潵瑙e喅杩欎釜闂鍛紵鎴戜滑鏈€鍏堟兂鍒扮殑灏辨槸閫氳繃鍔犻攣鐨勬柟寮忥細缁?log() 鍑芥暟鍔犱簰鏂ラ攣锛圝ava 涓彲浠ラ€氳繃 synchronized 鐨勫叧閿瓧锛夛紝鍚屼竴鏃跺埢鍙厑璁镐竴涓嚎绋嬭皟鐢ㄦ墽琛?log() 鍑芥暟銆傚叿浣撶殑浠g爜瀹炵幇濡備笅鎵€绀猴細


public class Logger { private FileWriter writer;
public Logger() { File file = new File("/Users/wangzheng/log.txt"); writer = new FileWriter(file, true); //true琛ㄧず杩藉姞鍐欏叆 }
public void log(String message) { synchronized(this) { writer.write(mesasge); } }}


涓嶈繃锛屼綘浠旂粏鎯虫兂锛岃繖鐪熺殑鑳借В鍐冲绾跨▼鍐欏叆鏃ュ織鏃朵簰鐩歌鐩栫殑闂鍚楋紵绛旀鏄惁瀹氱殑銆傝繖鏄洜涓猴紝杩欑閿佹槸涓€涓璞$骇鍒殑閿侊紝涓€涓璞″湪涓嶅悓鐨勭嚎绋嬩笅鍚屾椂璋冪敤 log() 鍑芥暟锛屼細琚己鍒惰姹傞『搴忔墽琛屻€備絾鏄紝涓嶅悓鐨勫璞′箣闂村苟涓嶅叡浜悓涓€鎶婇攣銆傚湪涓嶅悓鐨勭嚎绋嬩笅锛岄€氳繃涓嶅悓鐨勫璞¤皟鐢ㄦ墽琛?log() 鍑芥暟锛岄攣骞朵笉浼氳捣浣滅敤锛屼粛鐒舵湁鍙兘瀛樺湪鍐欏叆鏃ュ織浜掔浉瑕嗙洊鐨勯棶棰樸€?/span>

鍒涘缓鍨嬭璁℃ā寮?鍗曚緥妯″紡

鎴戣繖閲岀◢寰ˉ鍏呬竴涓嬶紝鍦ㄥ垰鍒氱殑璁茶В鍜岀粰鍑虹殑浠g爜涓紝鎴戞晠鎰忊€滈殣鐬掆€濅簡涓€涓簨瀹烇細鎴戜滑缁?log() 鍑芥暟鍔犱笉鍔犲璞$骇鍒殑閿侊紝鍏跺疄閮芥病鏈夊叧绯汇€傚洜涓?FileWriter 鏈韩灏辨槸绾跨▼瀹夊叏鐨勶紝瀹冪殑鍐呴儴瀹炵幇涓湰韬氨鍔犱簡瀵硅薄绾у埆鐨勯攣锛屽洜姝わ紝鍦ㄥ灞傝皟鐢?write() 鍑芥暟鐨勬椂鍊欙紝鍐嶅姞瀵硅薄绾у埆鐨勯攣瀹為檯涓婃槸澶氭涓€涓俱€傚洜涓轰笉鍚岀殑 Logger 瀵硅薄涓嶅叡浜?FileWriter 瀵硅薄锛屾墍浠ワ紝FileWriter 瀵硅薄绾у埆鐨勯攣涔熻В鍐充笉浜嗘暟鎹啓鍏ヤ簰鐩歌鐩栫殑闂銆?/span>

閭f垜浠鎬庝箞瑙e喅杩欎釜闂鍛紵瀹為檯涓婏紝瑕佹兂瑙e喅杩欎釜闂涔熶笉闅撅紝鎴戜滑鍙渶瑕佹妸瀵硅薄绾у埆鐨勯攣锛屾崲鎴愮被绾у埆鐨勯攣灏卞彲浠ヤ簡銆傝鎵€鏈夌殑瀵硅薄閮藉叡浜悓涓€鎶婇攣銆傝繖鏍峰氨閬垮厤浜嗕笉鍚屽璞′箣闂村悓鏃惰皟鐢?log() 鍑芥暟锛岃€屽鑷寸殑鏃ュ織瑕嗙洊闂銆傚叿浣撶殑浠g爜瀹炵幇濡備笅鎵€绀猴細


public class Logger { private FileWriter writer;
public Logger() { File file = new File("/Users/wangzheng/log.txt"); writer = new FileWriter(file, true); //true琛ㄧず杩藉姞鍐欏叆 }
public void log(String message) { synchronized(Logger.class) { // 绫荤骇鍒殑閿?/span> writer.write(mesasge); } }}


闄や簡浣跨敤绫荤骇鍒攣涔嬪锛屽疄闄呬笂锛岃В鍐宠祫婧愮珵浜夐棶棰樼殑鍔炴硶杩樻湁寰堝锛屽垎甯冨紡閿佹槸鏈€甯稿惉鍒扮殑涓€绉嶈В鍐虫柟妗堛€備笉杩囷紝瀹炵幇涓€涓畨鍏ㄥ彲闈犮€佹棤 bug銆侀珮鎬ц兘鐨勫垎甯冨紡閿侊紝骞朵笉鏄欢瀹规槗鐨勪簨鎯呫€傞櫎姝や箣澶栵紝骞跺彂闃熷垪锛堟瘮濡?Java 涓殑 BlockingQueue锛変篃鍙互瑙e喅杩欎釜闂锛氬涓嚎绋嬪悓鏃跺線骞跺彂闃熷垪閲屽啓鏃ュ織锛屼竴涓崟鐙殑绾跨▼璐熻矗灏嗗苟鍙戦槦鍒椾腑鐨勬暟鎹紝鍐欏叆鍒版棩蹇楁枃浠躲€傝繖绉嶆柟寮忓疄鐜拌捣鏉ヤ篃绋嶅井鏈夌偣澶嶆潅銆?/span>

鐩稿浜庤繖涓ょ瑙e喅鏂规锛屽崟渚嬫ā寮忕殑瑙e喅鎬濊矾灏辩畝鍗曚竴浜涗簡銆傚崟渚嬫ā寮忕浉瀵逛簬涔嬪墠绫荤骇鍒攣鐨勫ソ澶勬槸锛屼笉鐢ㄥ垱寤洪偅涔堝 Logger 瀵硅薄锛屼竴鏂归潰鑺傜渷鍐呭瓨绌洪棿锛屽彟涓€鏂归潰鑺傜渷绯荤粺鏂囦欢鍙ユ焺锛堝浜庢搷浣滅郴缁熸潵璇达紝鏂囦欢鍙ユ焺涔熸槸涓€绉嶈祫婧愶紝涓嶈兘闅忎究娴垂锛夈€?/span>

鎴戜滑灏?Logger 璁捐鎴愪竴涓崟渚嬬被锛岀▼搴忎腑鍙厑璁稿垱寤轰竴涓?Logger 瀵硅薄锛屾墍鏈夌殑绾跨▼鍏变韩浣跨敤鐨勮繖涓€涓?Logger 瀵硅薄锛屽叡浜竴涓?FileWriter 瀵硅薄锛岃€?FileWriter 鏈韩鏄璞$骇鍒嚎绋嬪畨鍏ㄧ殑锛屼篃灏遍伩鍏嶄簡澶氱嚎绋嬫儏鍐典笅鍐欐棩蹇椾細浜掔浉瑕嗙洊鐨勯棶棰樸€?/span>

鎸夌収杩欎釜璁捐鎬濊矾锛屾垜浠疄鐜颁簡 Logger 鍗曚緥绫汇€傚叿浣撲唬鐮佸涓嬫墍绀猴細


public class Logger { private FileWriter writer; private static final Logger instance = new Logger();
private Logger() { File file = new File("/Users/wangzheng/log.txt"); writer = new FileWriter(file, true); //true琛ㄧず杩藉姞鍐欏叆 }
public static Logger getInstance() { return instance; }
public void log(String message) { writer.write(mesasge); }}
// Logger绫荤殑搴旂敤绀轰緥锛?/span>public class UserController { public void login(String username, String password) { // ...鐪佺暐涓氬姟閫昏緫浠g爜... Logger.getInstance().log(username + " logined!"); }}
public class OrderController { private Logger logger = new Logger();
public void create(OrderVo order) { // ...鐪佺暐涓氬姟閫昏緫浠g爜... Logger.getInstance().log("Created a order: " + order.toString()); }}


瀹炴垬妗堜緥浜岋細琛ㄧず鍏ㄥ眬鍞竴绫?/h3>

浠庝笟鍔℃蹇典笂锛屽鏋滄湁浜涙暟鎹湪绯荤粺涓彧搴斾繚瀛樹竴浠斤紝閭e氨姣旇緝閫傚悎璁捐涓哄崟渚嬬被銆?/span>

姣斿锛岄厤缃俊鎭被銆傚湪绯荤粺涓紝鎴戜滑鍙湁涓€涓厤缃枃浠讹紝褰撻厤缃枃浠惰鍔犺浇鍒板唴瀛樹箣鍚庯紝浠ュ璞$殑褰㈠紡瀛樺湪锛屼篃鐞嗘墍搴斿綋鍙湁涓€浠姐€?/span>

鍐嶆瘮濡傦紝鍞竴閫掑 ID 鍙风爜鐢熸垚鍣紝濡傛灉绋嬪簭涓湁涓や釜瀵硅薄锛岄偅灏变細瀛樺湪鐢熸垚閲嶅 ID 鐨勬儏鍐碉紝鎵€浠ワ紝鎴戜滑搴旇灏?ID 鐢熸垚鍣ㄧ被璁捐涓哄崟渚嬨€?/span>


import java.util.concurrent.atomic.AtomicLong;public class IdGenerator { // AtomicLong鏄竴涓狫ava骞跺彂搴撲腑鎻愪緵鐨勪竴涓師瀛愬彉閲忕被鍨? // 瀹冨皢涓€浜涚嚎绋嬩笉瀹夊叏闇€瑕佸姞閿佺殑澶嶅悎鎿嶄綔灏佽涓轰簡绾跨▼瀹夊叏鐨勫師瀛愭搷浣滐紝 // 姣斿涓嬮潰浼氱敤鍒扮殑incrementAndGet(). private AtomicLong id = new AtomicLong(0); private static final IdGenerator instance = new IdGenerator(); private IdGenerator() {} public static IdGenerator getInstance() { return instance; } public long getId() { return id.incrementAndGet(); }}
// IdGenerator浣跨敤涓句緥long id = IdGenerator.getInstance().getId();


瀹為檯涓婏紝浠婂ぉ璁插埌鐨勪袱涓唬鐮佸疄渚嬶紙Logger銆両dGenerator锛夛紝璁捐鐨勯兘骞朵笉浼橀泤锛岃繕瀛樺湪涓€浜涢棶棰樸€傝嚦浜庢湁浠€涔堥棶棰樹互鍙婂浣曟敼閫狅紝浠婂ぉ鎴戞殏鏃跺崠涓叧瀛愶紝涓嬩竴鑺傝鎴戜細璇︾粏璁茶В銆?/span>

濡備綍瀹炵幇涓€涓崟渚嬶紵

灏界浠嬬粛濡備綍瀹炵幇涓€涓崟渚嬫ā寮忕殑鏂囩珷宸茬粡鏈夊緢澶氫簡锛屼絾涓轰簡淇濊瘉鍐呭鐨勫畬鏁存€э紝鎴戣繖閲岃繕鏄畝鍗曚粙缁嶄竴涓嬪嚑绉嶇粡鍏稿疄鐜版柟寮忋€傛鎷捣鏉ワ紝瑕佸疄鐜颁竴涓崟渚嬶紝鎴戜滑闇€瑕佸叧娉ㄧ殑鐐规棤澶栦箮涓嬮潰鍑犱釜锛?/span>

鏋勯€犲嚱鏁伴渶瑕佹槸 private 璁块棶鏉冮檺鐨勶紝杩欐牱鎵嶈兘閬垮厤澶栭儴閫氳繃 new 鍒涘缓瀹炰緥锛?/span>

鑰冭檻瀵硅薄鍒涘缓鏃剁殑绾跨▼瀹夊叏闂锛?/span>

鑰冭檻鏄惁鏀寔寤惰繜鍔犺浇锛?/span>

鑰冭檻 getInstance() 鎬ц兘鏄惁楂橈紙鏄惁鍔犻攣锛夈€?/span>

濡傛灉浣犲杩欏潡宸茬粡寰堢啛鎮変簡锛屼綘鍙互褰撲綔澶嶄範銆傛敞鎰忥紝涓嬮潰鐨勫嚑绉嶅崟渚嬪疄鐜版柟寮忔槸閽堝 Java 璇█璇硶鐨勶紝濡傛灉浣犵啛鎮夌殑鏄叾浠栬瑷€锛屼笉濡ㄥ姣?Java 鐨勮繖鍑犵瀹炵幇鏂瑰紡锛岃嚜宸辫瘯鐫€鎬荤粨涓€涓嬶紝鍒╃敤浣犵啛鎮夌殑璇█锛岃濡備綍瀹炵幇銆?/span>

1. 楗挎眽寮?/h3>

楗挎眽寮忕殑瀹炵幇鏂瑰紡姣旇緝绠€鍗曘€傚湪绫诲姞杞界殑鏃跺€欙紝instance 闈欐€佸疄渚嬪氨宸茬粡鍒涘缓骞跺垵濮嬪寲濂戒簡锛屾墍浠ワ紝instance 瀹炰緥鐨勫垱寤鸿繃绋嬫槸绾跨▼瀹夊叏鐨勩€備笉杩囷紝杩欐牱鐨勫疄鐜版柟寮忎笉鏀寔寤惰繜鍔犺浇锛堝湪鐪熸鐢ㄥ埌 IdGenerator 鐨勬椂鍊欙紝鍐嶅垱寤哄疄渚嬶級锛屼粠鍚嶅瓧涓垜浠篃鍙互鐪嬪嚭杩欎竴鐐广€傚叿浣撶殑浠g爜瀹炵幇濡備笅鎵€绀猴細


public class IdGenerator { private AtomicLong id = new AtomicLong(0); private static final IdGenerator instance = new IdGenerator(); private IdGenerator() {} public static IdGenerator getInstance() { return instance; } public long getId() { return id.incrementAndGet(); }}


鏈変汉瑙夊緱杩欑瀹炵幇鏂瑰紡涓嶅ソ锛屽洜涓轰笉鏀寔寤惰繜鍔犺浇锛屽鏋滃疄渚嬪崰鐢ㄨ祫婧愬锛堟瘮濡傚崰鐢ㄥ唴瀛樺锛夋垨鍒濆鍖栬€楁椂闀匡紙姣斿闇€瑕佸姞杞藉悇绉嶉厤缃枃浠讹級锛屾彁鍓嶅垵濮嬪寲瀹炰緥鏄竴绉嶆氮璐硅祫婧愮殑琛屼负銆傛渶濂界殑鏂规硶搴旇鍦ㄧ敤鍒扮殑鏃跺€欏啀鍘诲垵濮嬪寲銆備笉杩囷紝鎴戜釜浜哄苟涓嶈鍚岃繖鏍风殑瑙傜偣銆?/span>

濡傛灉鍒濆鍖栬€楁椂闀匡紝閭f垜浠渶濂戒笉瑕佺瓑鍒扮湡姝h鐢ㄥ畠鐨勬椂鍊欙紝鎵嶅幓鎵ц杩欎釜鑰楁椂闀跨殑鍒濆鍖栬繃绋嬶紝杩欎細褰卞搷鍒扮郴缁熺殑鎬ц兘锛堟瘮濡傦紝鍦ㄥ搷搴斿鎴风鎺ュ彛璇锋眰鐨勬椂鍊欙紝鍋氳繖涓垵濮嬪寲鎿嶄綔锛屼細瀵艰嚧姝よ姹傜殑鍝嶅簲鏃堕棿鍙橀暱锛岀敋鑷宠秴鏃讹級銆傞噰鐢ㄩタ姹夊紡瀹炵幇鏂瑰紡锛屽皢鑰楁椂鐨勫垵濮嬪寲鎿嶄綔锛屾彁鍓嶅埌绋嬪簭鍚姩鐨勬椂鍊欏畬鎴愶紝杩欐牱灏辫兘閬垮厤鍦ㄧ▼搴忚繍琛岀殑鏃跺€欙紝鍐嶅幓鍒濆鍖栧鑷寸殑鎬ц兘闂銆?/span>

濡傛灉瀹炰緥鍗犵敤璧勬簮澶氾紝鎸夌収 fail-fast 鐨勮璁″師鍒欙紙鏈夐棶棰樺強鏃╂毚闇诧級锛岄偅鎴戜滑涔熷笇鏈涘湪绋嬪簭鍚姩鏃跺氨灏嗚繖涓疄渚嬪垵濮嬪寲濂姐€傚鏋滆祫婧愪笉澶燂紝灏变細鍦ㄧ▼搴忓惎鍔ㄧ殑鏃跺€欒Е鍙戞姤閿欙紙姣斿 Java 涓殑 PermGen Space OOM锛夛紝鎴戜滑鍙互绔嬪嵆鍘讳慨澶嶃€傝繖鏍蜂篃鑳介伩鍏嶅湪绋嬪簭杩愯涓€娈垫椂闂村悗锛岀獊鐒跺洜涓哄垵濮嬪寲杩欎釜瀹炰緥鍗犵敤璧勬簮杩囧锛屽鑷寸郴缁熷穿婧冿紝褰卞搷绯荤粺鐨勫彲鐢ㄦ€с€?/span>

2. 鎳掓眽寮?/h3>

鏈夐タ姹夊紡锛屽搴旂殑锛屽氨鏈夋噿姹夊紡銆傛噿姹夊紡鐩稿浜庨タ姹夊紡鐨勪紭鍔挎槸鏀寔寤惰繜鍔犺浇銆傚叿浣撶殑浠g爜瀹炵幇濡備笅鎵€绀猴細


public class IdGenerator { private AtomicLong id = new AtomicLong(0); private static IdGenerator instance; private IdGenerator() {} public static synchronized IdGenerator getInstance() { if (instance == null) { instance = new IdGenerator(); } return instance; } public long getId() { return id.incrementAndGet(); }}


涓嶈繃鎳掓眽寮忕殑缂虹偣涔熷緢鏄庢樉锛屾垜浠粰 getInstance() 杩欎釜鏂规硶鍔犱簡涓€鎶婂ぇ閿侊紙synchronzed锛夛紝瀵艰嚧杩欎釜鍑芥暟鐨勫苟鍙戝害寰堜綆銆傞噺鍖栦竴涓嬬殑璇濓紝骞跺彂搴︽槸 1锛屼篃灏辩浉褰撲簬涓茶鎿嶄綔浜嗐€傝€岃繖涓嚱鏁版槸鍦ㄥ崟渚嬩娇鐢ㄦ湡闂达紝涓€鐩翠細琚皟鐢ㄣ€傚鏋滆繖涓崟渚嬬被鍋跺皵浼氳鐢ㄥ埌锛岄偅杩欑瀹炵幇鏂瑰紡杩樺彲浠ユ帴鍙椼€備絾鏄紝濡傛灉棰戠箒鍦扮敤鍒帮紝閭i绻佸姞閿併€侀噴鏀鹃攣鍙婂苟鍙戝害浣庣瓑闂锛屼細瀵艰嚧鎬ц兘鐡堕锛岃繖绉嶅疄鐜版柟寮忓氨涓嶅彲鍙栦簡銆?/span>

3. 鍙岄噸妫€娴?/h3>

楗挎眽寮忎笉鏀寔寤惰繜鍔犺浇锛屾噿姹夊紡鏈夋€ц兘闂锛屼笉鏀寔楂樺苟鍙戙€傞偅鎴戜滑鍐嶆潵鐪嬩竴绉嶆棦鏀寔寤惰繜鍔犺浇銆佸張鏀寔楂樺苟鍙戠殑鍗曚緥瀹炵幇鏂瑰紡锛屼篃灏辨槸鍙岄噸妫€娴嬪疄鐜版柟寮忋€?/span>

鍦ㄨ繖绉嶅疄鐜版柟寮忎腑锛屽彧瑕?instance 琚垱寤轰箣鍚庯紝鍗充究鍐嶈皟鐢?getInstance() 鍑芥暟涔熶笉浼氬啀杩涘叆鍒板姞閿侀€昏緫涓簡銆傛墍浠ワ紝杩欑瀹炵幇鏂瑰紡瑙e喅浜嗘噿姹夊紡骞跺彂搴︿綆鐨勯棶棰樸€傚叿浣撶殑浠g爜瀹炵幇濡備笅鎵€绀猴細


public class IdGenerator { private AtomicLong id = new AtomicLong(0); private static IdGenerator instance; private IdGenerator() {} public static IdGenerator getInstance() { if (instance == null) { synchronized(IdGenerator.class) { // 姝ゅ涓虹被绾у埆鐨勯攣 if (instance == null) { instance = new IdGenerator(); } } } return instance; } public long getId() { return id.incrementAndGet(); }}


缃戜笂鏈変汉璇达紝杩欑瀹炵幇鏂瑰紡鏈変簺闂銆傚洜涓烘寚浠ら噸鎺掑簭锛屽彲鑳戒細瀵艰嚧 IdGenerator 瀵硅薄琚?new 鍑烘潵锛屽苟涓旇祴鍊肩粰 instance 涔嬪悗锛岃繕娌℃潵寰楀強鍒濆鍖栵紙鎵ц鏋勯€犲嚱鏁颁腑鐨勪唬鐮侀€昏緫锛夛紝灏辫鍙︿竴涓嚎绋嬩娇鐢ㄤ簡銆?/span>

瑕佽В鍐宠繖涓棶棰橈紝鎴戜滑闇€瑕佺粰 instance 鎴愬憳鍙橀噺鍔犱笂 volatile 鍏抽敭瀛楋紝绂佹鎸囦护閲嶆帓搴忔墠琛屻€傚疄闄呬笂锛屽彧鏈夊緢浣庣増鏈殑 Java 鎵嶄細鏈夎繖涓棶棰樸€傛垜浠幇鍦ㄧ敤鐨勯珮鐗堟湰鐨?Java 宸茬粡鍦?JDK 鍐呴儴瀹炵幇涓В鍐充簡杩欎釜闂锛堣В鍐崇殑鏂规硶寰堢畝鍗曪紝鍙鎶婂璞?new 鎿嶄綔鍜屽垵濮嬪寲鎿嶄綔璁捐涓哄師瀛愭搷浣滐紝灏辫嚜鐒惰兘绂佹閲嶆帓搴忥級銆傚叧浜庤繖鐐圭殑璇︾粏瑙i噴锛岃窡鐗瑰畾璇█鏈夊叧锛屾垜灏变笉灞曞紑璁蹭簡锛屾劅鍏磋叮鐨勫悓瀛﹀彲浠ヨ嚜琛岀爺绌朵竴涓嬨€?/span>

4. 闈欐€佸唴閮ㄧ被

鎴戜滑鍐嶆潵鐪嬩竴绉嶆瘮鍙岄噸妫€娴嬫洿鍔犵畝鍗曠殑瀹炵幇鏂规硶锛岄偅灏辨槸鍒╃敤 Java 鐨勯潤鎬佸唴閮ㄧ被銆傚畠鏈夌偣绫讳技楗挎眽寮忥紝浣嗗張鑳藉仛鍒颁簡寤惰繜鍔犺浇銆傚叿浣撴槸鎬庝箞鍋氬埌鐨勫憿锛熸垜浠厛鏉ョ湅瀹冪殑浠g爜瀹炵幇銆?/span>


public class IdGenerator { private AtomicLong id = new AtomicLong(0); private IdGenerator() {}
private static class SingletonHolder{ private static final IdGenerator instance = new IdGenerator(); }
public static IdGenerator getInstance() { return SingletonHolder.instance; }
public long getId() { return id.incrementAndGet(); }}


SingletonHolder 鏄竴涓潤鎬佸唴閮ㄧ被锛屽綋澶栭儴绫?IdGenerator 琚姞杞界殑鏃跺€欙紝骞朵笉浼氬垱寤?SingletonHolder 瀹炰緥瀵硅薄銆傚彧鏈夊綋璋冪敤 getInstance() 鏂规硶鏃讹紝SingletonHolder 鎵嶄細琚姞杞斤紝杩欎釜鏃跺€欐墠浼氬垱寤?instance銆俰nstance 鐨勫敮涓€鎬с€佸垱寤鸿繃绋嬬殑绾跨▼瀹夊叏鎬э紝閮界敱 JVM 鏉ヤ繚璇併€傛墍浠ワ紝杩欑瀹炵幇鏂规硶鏃繚璇佷簡绾跨▼瀹夊叏锛屽張鑳藉仛鍒板欢杩熷姞杞姐€?/span>

5. 鏋氫妇

鏈€鍚庯紝鎴戜滑浠嬬粛涓€绉嶆渶绠€鍗曠殑瀹炵幇鏂瑰紡锛屽熀浜庢灇涓剧被鍨嬬殑鍗曚緥瀹炵幇銆傝繖绉嶅疄鐜版柟寮忛€氳繃 Java 鏋氫妇绫诲瀷鏈韩鐨勭壒鎬э紝淇濊瘉浜嗗疄渚嬪垱寤虹殑绾跨▼瀹夊叏鎬у拰瀹炰緥鐨勫敮涓€鎬с€傚叿浣撶殑浠g爜濡備笅鎵€绀猴細


public enum IdGenerator { INSTANCE; private AtomicLong id = new AtomicLong(0);
public long getId() { return id.incrementAndGet(); }}


閲嶇偣鍥為【

濂戒簡锛屼粖澶╃殑鍐呭鍒版灏辫瀹屼簡銆傛垜浠潵鎬荤粨鍥為【涓€涓嬶紝浣犻渶瑕佹帉鎻$殑閲嶇偣鍐呭銆?/span>

1. 鍗曚緥鐨勫畾涔?/span>

鍗曚緥璁捐妯″紡锛圫ingleton Design Pattern锛夌悊瑙h捣鏉ラ潪甯哥畝鍗曘€備竴涓被鍙厑璁稿垱寤轰竴涓璞★紙鎴栬€呭彨瀹炰緥锛夛紝閭h繖涓被灏辨槸涓€涓崟渚嬬被锛岃繖绉嶈璁℃ā寮忓氨鍙綔鍗曚緥璁捐妯″紡锛岀畝绉板崟渚嬫ā寮忋€?/span>

2. 鍗曚緥鐨勭敤澶?/span>

浠庝笟鍔℃蹇典笂锛屾湁浜涙暟鎹湪绯荤粺涓彧搴旇淇濆瓨涓€浠斤紝灏辨瘮杈冮€傚悎璁捐涓哄崟渚嬬被銆傛瘮濡傦紝绯荤粺鐨勯厤缃俊鎭被銆傞櫎姝や箣澶栵紝鎴戜滑杩樺彲浠ヤ娇鐢ㄥ崟渚嬭В鍐宠祫婧愯闂啿绐佺殑闂銆?/span>

3. 鍗曚緥鐨勫疄鐜?/span>

鍗曚緥鏈変笅闈㈠嚑绉嶇粡鍏哥殑瀹炵幇鏂瑰紡銆?/span>

楗挎眽寮?/span>

楗挎眽寮忕殑瀹炵幇鏂瑰紡锛屽湪绫诲姞杞界殑鏈熼棿锛屽氨宸茬粡灏?instance 闈欐€佸疄渚嬪垵濮嬪寲濂戒簡锛屾墍浠ワ紝instance 瀹炰緥鐨勫垱寤烘槸绾跨▼瀹夊叏鐨勩€備笉杩囷紝杩欐牱鐨勫疄鐜版柟寮忎笉鏀寔寤惰繜鍔犺浇瀹炰緥銆?/span>

鎳掓眽寮?/span>

鎳掓眽寮忕浉瀵逛簬楗挎眽寮忕殑浼樺娍鏄敮鎸佸欢杩熷姞杞姐€傝繖绉嶅疄鐜版柟寮忎細瀵艰嚧棰戠箒鍔犻攣銆侀噴鏀鹃攣锛屼互鍙婂苟鍙戝害浣庣瓑闂锛岄绻佺殑璋冪敤浼氫骇鐢熸€ц兘鐡堕銆?/span>

鍙岄噸妫€娴?/span>

鍙岄噸妫€娴嬪疄鐜版柟寮忔棦鏀寔寤惰繜鍔犺浇銆佸張鏀寔楂樺苟鍙戠殑鍗曚緥瀹炵幇鏂瑰紡銆傚彧瑕?instance 琚垱寤轰箣鍚庯紝鍐嶈皟鐢?getInstance() 鍑芥暟閮戒笉浼氳繘鍏ュ埌鍔犻攣閫昏緫涓€傛墍浠ワ紝杩欑瀹炵幇鏂瑰紡瑙e喅浜嗘噿姹夊紡骞跺彂搴︿綆鐨勯棶棰樸€?/span>

闈欐€佸唴閮ㄧ被

鍒╃敤 Java 鐨勯潤鎬佸唴閮ㄧ被鏉ュ疄鐜板崟渚嬨€傝繖绉嶅疄鐜版柟寮忥紝鏃㈡敮鎸佸欢杩熷姞杞斤紝涔熸敮鎸侀珮骞跺彂锛屽疄鐜拌捣鏉ヤ篃姣斿弻閲嶆娴嬬畝鍗曘€?/span>

鏋氫妇

鏈€绠€鍗曠殑瀹炵幇鏂瑰紡锛屽熀浜庢灇涓剧被鍨嬬殑鍗曚緥瀹炵幇銆傝繖绉嶅疄鐜版柟寮忛€氳繃 Java 鏋氫妇绫诲瀷鏈韩鐨勭壒鎬э紝淇濊瘉浜嗗疄渚嬪垱寤虹殑绾跨▼瀹夊叏鎬у拰瀹炰緥鐨勫敮涓€鎬с€?/span>


鐐瑰嚮馃憞鈥滈槄璇诲師鏂団€濔煈囷紝鏌ョ湅鍘熸枃锛?/span>



  

 -瑙h婧愮爜-  

   鐭ュ叾鐒跺苟鐭ュ叾鎵€浠ョ劧    

鍒涘缓鍨嬭璁℃ā寮?鍗曚緥妯″紡



以上是关于鍒涘缓鍨嬭璁℃ā寮?鍗曚緥妯″紡的主要内容,如果未能解决你的问题,请参考以下文章

甯歌璁捐妯″瀷

璁捐妯″紡

mongodb鐨勮繛鎺ユ睜绠$悊

Struts2 杩炴帴鏁版嵁搴撴€荤粨

Vue寮€鍙戜腑鐨勪腑澶簨浠舵€荤嚎鍒涘缓浠ュ強浣跨敤