鍒涘缓鍨嬭璁℃ā寮?鍗曚緥妯″紡
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婧愮爜-
鐭ュ叾鐒跺苟鐭ュ叾鎵€浠ョ劧
以上是关于鍒涘缓鍨嬭璁℃ā寮?鍗曚緥妯″紡的主要内容,如果未能解决你的问题,请参考以下文章