Effective Java ѧϰ±Ê¼Ç£¨3£©
ÔÚʵÏÖsingletonģʽʱ£¬ÎÒÃÇÓÐÒÔϼ¸ÖÖ·½·¨¡£ 1. public static final ×ֶμÓÉÏprivate µÄ¹¹Ô캯Êý¡£ public class Singleton{
public static final Singleton INSTANCE = new Singleton();
private Singleton() { ... }
} ÕâÖÖ·½·¨¼òµ¥£¬ÆäÖеÄ×Ö¶ÎÈÃÈËһĿÁËÈ»¾ÍÖªµÀÕâ¸öÀàÊÇSingletonģʽ£¬ÓÃfinal¹Ø¼ü×ÖÈ·±£Õâ¸ö±äÁ¿ÔÚ³õʼ»¯ºó¾Í²»»áÔÙ±»¸ü¸Ä£¬µ«Òª×¢ÒâµÄÊÇСÐÄ·ÀÖ¹ÉÙÊý±ðÓÐÓÃÐĵijÌÐòʹÓÃAccessibleObject.setAccessibleÀ´·ÃÎÊ˽Óй¹Ô캯Êý£¬Õâʱ¿ÉÒÔÔÚ˽Óй¹Ô캯ÊýÖÐÅжϵ±´´½¨µÚ¶þ¸öʵÀýµÄʱºòÅ׳öÒ»¸öÒì³£¡£ 2. private static final ×ֶμÓÉÏprivate µÄ¹¹Ô캯ÊýÔÙ¼ÓÉÏpublic ¾²Ì¬¹¤³§º¯Êý public class Singleton{
private static final Singleton INSTANCE = new Singleton();
private Singleton() { ... }
public static Singleton getInstance{}{return INSTANCE;} } ÕâÖÖ·½Ê½²»Óõ£ÐÄÐÔÄÜÎÊÌ⣬ÒòΪÏÖÔÚµÄJVM»á°Ñ¾ø´ó¶àÊýµÄ¾²Ì¬¹¤³§º¯Êý¸ÄΪÄÚÁª¡£´ËÍ⣬ÕâÖÖ·½·¨»¹¿ÉÒÔÔÚ²»¸Ä±äAPIµÄÇé¿ö϶Էµ»ØÖµ½øÐиı䣬¼´ÔÚÒÔºó¸ù¾ÝÐèÒªÊÇ·ñ·µ»ØµÚ¶þ¸öʵÀý£¬»òÊÇÿ¸öÏß³ÌÒ»¸öʵÀý¡£ ¶ÔÓÚÒÔÉÏÁ½ÖÖ·½·¨ÔÚ´®Ðл¯µÄʱºòҪעÒ⣬²»Äܽö½öÖ»¼ÓÉÏimplements Serializable½Ó¿Ú£¬ÕâÑù»áµ¼ÖÂÿ´Î·´ÐòÁл¯µÄʱºò´´½¨Ò»¸öжÔÏó£¬ÕâÑù¾Í²»ÊÇÕæÕýµÄsingletonÁË£¬½â¾öÕâ¸öÎÊÌâµÄ·½·¨ÊÇÔÚÉùÃ÷µÄʱºò¶Ôÿһ¸öfinal×ֶμÓÉÏtransient¹Ø¼ü×Ö£¬È»ºóÔÙʵÏÖÒ»¸öreadResolve·½·¨¡£ private Object readResolve() {
return INSTANCE;
} ¶ÔÓÚjava 1.5ºóµÄ°æ±¾£¬ÊµÏÖsingletonģʽ×îºÃµÄ·½·¨ÊÇÓÃö¾Ù¡£
public enum Singleton {
INSTANCE; } ÕâÖÖ·½·¨Ã»ÓÐÉÏÃæ˵µÄȱµã£¬ÊÇ×îÓŵķ½·¨¡£
Ïà¹ØÎĵµ£º
ÎÄÕÂÓÃʵÀý´úÂëչʾÁËJavaÖжàÏ̷߳ÃÎʹ²Ïí×ÊÔ´
ʱÏß³Ìͬ²½
µÄÖØÒªÐÔ¡£
·Ö±ðͨ¹ýÔÚÁ½¸öÏß³ÌÖÐͬʱ·ÃÎÊ(µ÷ÓÃget_id*·½·¨)¾¹ýͬ²½´¦Àí(lock¼°Synchronized)µÄ¹²Ïí×ÊÔ´(tmp)¼°Î´¾¹ýͬ²½´¦ÀíµÄ¹²Ïí×ÊÔ´(tmp)À´ËµÃ÷ͬ²½´¦ÀíµÄµÄ×÷Óá£
mainÖзÖÁ½²¿·Ö:
1£©Ç°°ë²¿·Ö£¬non-synchronization²¿·ÖÓÃÀ´²âÊÔûÓÐ×öͬ²½´¦ÀíµÄ´úÂë ......
JavaÐéÄâ»úµÄÄÚ²¿Ìåϵ½á¹¹Ò²ÐíºÜÉÙÓÐÈËÈ¥¹ØÐÄ£¬ÒòΪ¶ÔÓÚJava³ÌÐòÔ±À´Ëµ£¬Ò»°ãÖ»ÐèÒª¸úAPI´ò½»µÀ¾Í¿ÉÒÔÁË¡£ÕâЩÌåϵ½á¹¹Ö»ÊÇJavaÐéÄâ»úÄÚ²¿µÄ½á¹¹¶øÒÑ¡£µ«ÊÇÈç¹ûÀí½âÁËÆäÄÚ²¿½á¹¹£¬¶ÔÓÚÎÒÃÇ¿ª·¢ÏîÄ¿£¬ÒÔ¼°ÅųýϵͳÖеÄÏà¹Ø¹ÊÕÏÊÇÓÉ°ïÖúµÄ¡£
ÏÈ¿´¿´×ÜÌå½á¹¹Í¼£¬ÈçÏ£º
¸÷²¿·ÖµÄÖ÷Òª¹¦ÄÜ·Ö±ðÊÇ£º
1. Àà×°ÔØ×Óϵͳ:¸ºÔðJavaÀ ......
http://www.blogjava.net/flysky19/articles/93492.html
1.java project»·¾³£¬Ê¹ÓÃjava.ioÓÃÏà¶Ô·¾¶¶ÁÈ¡ÎļþµÄÀý×Ó£º
*Ŀ¼½á¹¹£º
DecisionTree
|___src
  ......
Java ±à³Ì¼¼ÊõÖкº×ÖÎÊÌâµÄ·ÖÎö¼°½â¾ö
ÏÖÔÚ Java ±à³ÌÓïÑÔÒѾ¹ã·ºÓ¦ÓÃÓÚ»¥ÁªÍøÊÀ½ç£¬ÔçÔÚ Sun ¹«Ë¾¿ª·¢ Java ÓïÑÔµÄʱºò£¬¾ÍÒѾ¿¼Âǵ½¶Ô·ÇÓ¢ÎÄ×Ö·ûµÄÖ§³ÖÁË¡£Sun ¹«Ë¾¹«²¼µÄ Java ÔËÐл·¾³£¨JRE£©±¾Éí¾Í·ÖÓ¢ÎÄ°æºÍ¹ú¼Ê°æ£¬µ«Ö»Óйú¼Ê°æ²ÅÖ§³Ö·ÇÓ¢ÎÄ×Ö·û¡£²»¹ýÔÚ Java ±à³ÌÓïÑÔµÄÓ¦ÓÃÖУ¬¶ÔÖÐÎÄ×Ö·ûµÄÖ§³Ö²¢·ÇÈçͬ Java S ......