package com.willlee.designpatterns.singleton;

public class LockSingleton {
	private static LockSingleton singleTon = null;

	public LockSingleton() {
	}

	public static LockSingleton getInstance() {
		// Double-Check Locking
		/*
		 * ÎªºÎÒªÊ¹ÓÃË«ÖØ¼ì²éËø¶¨ÄØ£¿ ¿¼ÂÇÕâÑùÒ»ÖÖÇé¿ö£¬¾ÍÊÇÓÐÁ½¸öÏß³ÌÍ¬Ê±µ½´ï£¬¼´Í¬Ê±µ÷ÓÃ GetInstance£¨£©£¬ ´ËÊ±ÓÉÓÚ
		 * singleton == null £¬ËùÒÔºÜÃ÷ÏÔ£¬Á½¸öÏß³Ì¶¼¿ÉÒÔÍ¨¹ýµÚÒ»ÖØµÄ singleton == null £¬ ½øÈëµÚÒ»ÖØ if
		 * Óï¾äºó£¬ÓÉÓÚ´æÔÚËø»úÖÆ£¬ËùÒÔ»áÓÐÒ»¸öÏß³Ì½øÈë lock Óï¾ä²¢½øÈëµÚ¶þÖØ singleton == null £¬ ¶øÁíÍâµÄÒ»¸öÏß³ÌÔò»áÔÚ
		 * lock Óï¾äµÄÍâÃæµÈ´ý¡£ ¶øµ±µÚÒ»¸öÏß³ÌÖ´ÐÐÍê new Singleton£¨£©Óï¾äºó£¬±ã»áÍË³öËø¶¨ÇøÓò£¬´ËÊ±£¬µÚ¶þ¸öÏß³Ì±ã¿ÉÒÔ½øÈë
		 * lock Óï¾ä¿é£¬ ´ËÊ±£¬Èç¹ûÃ»ÓÐµÚ¶þÖØ singleton == null µÄ»°£¬ÄÇÃ´µÚ¶þ¸öÏß³Ì»¹ÊÇ¿ÉÒÔµ÷ÓÃ new
		 * Singleton£¨£©Óï¾ä£¬ ÕâÑùµÚ¶þ¸öÏß³ÌÒ²»á´´½¨Ò»¸ö Singleton ÊµÀý£¬ÕâÑùÒ²»¹ÊÇÎ¥±³ÁËµ¥ÀýÄ£Ê½µÄ³õÖÔµÄ£¬
		 * ËùÒÔÕâÀï±ØÐëÒªÊ¹ÓÃË«ÖØ¼ì²éËø¶¨¡£ Ï¸ÐÄµÄÅóÓÑÒ»¶¨»á·¢ÏÖ£¬Èç¹ûÎÒÈ¥µôµÚÒ»ÖØ singleton == null
		 * £¬³ÌÐò»¹ÊÇ¿ÉÒÔÔÚ¶àÏß³ÌÏÂÍêºÃµÄÔËÐÐµÄ£¬ ¿¼ÂÇÔÚÃ»ÓÐµÚÒ»ÖØ singleton == null µÄÇé¿öÏÂ£¬ µ±ÓÐÁ½¸öÏß³ÌÍ¬Ê±µ½´ï£¬´ËÊ±£¬ÓÉÓÚ
		 * lock »úÖÆµÄ´æÔÚ£¬µÚÒ»¸öÏß³Ì»á½øÈë lock Óï¾ä¿é£¬²¢ÇÒ¿ÉÒÔË³ÀûÖ´ÐÐ new Singleton£¨£©£¬ µ±µÚÒ»¸öÏß³ÌÍË³ö lock
		 * Óï¾ä¿éÊ±£¬ singleton Õâ¸ö¾²Ì¬±äÁ¿ÒÑ²»Îª null ÁË£¬ËùÒÔµ±µÚ¶þ¸öÏß³Ì½øÈë lock Ê±£¬ »¹ÊÇ»á±»µÚ¶þÖØ singleton
		 * == null µ²ÔÚÍâÃæ£¬¶øÎÞ·¨Ö´ÐÐ new Singleton£¨£©£¬ ËùÒÔÔÚÃ»ÓÐµÚÒ»ÖØ singleton == null
		 * µÄÇé¿öÏÂ£¬Ò²ÊÇ¿ÉÒÔÊµÏÖµ¥ÀýÄ£Ê½µÄ£¿ÄÇÃ´ÎªÊ²Ã´ÐèÒªµÚÒ»ÖØ singleton == null ÄØ£¿
		 * ÕâÀï¾ÍÉæ¼°Ò»¸öÐÔÄÜÎÊÌâÁË£¬ÒòÎª¶ÔÓÚµ¥ÀýÄ£Ê½µÄ»°£¬new Singleton£¨£©Ö»ÐèÒªÖ´ÐÐÒ»´Î¾Í OK ÁË£¬ ¶øÈç¹ûÃ»ÓÐµÚÒ»ÖØ
		 * singleton == null µÄ»°£¬Ã¿Ò»´ÎÓÐÏß³Ì½øÈë GetInstance£¨£©Ê±£¬¾ù»áÖ´ÐÐËø¶¨²Ù×÷À´ÊµÏÖÏß³ÌÍ¬²½£¬
		 * ÕâÊÇ·Ç³£ºÄ·ÑÐÔÄÜµÄ£¬¶øÈç¹ûÎÒ¼ÓÉÏµÚÒ»ÖØ singleton == null µÄ»°£¬ ÄÇÃ´¾ÍÖ»ÓÐÔÚµÚÒ»´Î£¬Ò²¾ÍÊÇ singleton
		 * ==null ³ÉÁ¢Ê±µÄÇé¿öÏÂÖ´ÐÐÒ»´ÎËø¶¨ÒÔÊµÏÖÏß³ÌÍ¬²½£¬ ¶øÒÔºóµÄ»°£¬±ãÖ»ÒªÖ±½Ó·µ»Ø Singleton ÊµÀý¾Í OK ÁË¶ø¸ù±¾ÎÞÐèÔÙ½øÈë
		 * lock Óï¾ä¿éÁË£¬ÕâÑù¾Í¿ÉÒÔ½â¾öÓÉÏß³ÌÍ¬²½´øÀ´µÄÐÔÄÜÎÊÌâÁË¡£
		 * ºÃ£¬¹ØÓÚ¶àÏß³ÌÏÂµ¥ÀýÄ£Ê½µÄÊµÏÖµÄ½éÉÜ¾Íµ½ÕâÀïÁË£¬µ«ÊÇ£¬¹ØÓÚµ¥ÀýÄ£Ê½µÄ½éÉÜ»¹Ã»Íê¡£
		 */
		if (singleTon == null) {
			synchronized (LockSingleton.class) {
				if (singleTon == null) {
					singleTon = new LockSingleton();
				}
			}
		}
		return singleTon;
	}
}
