Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

javaÉè¼Æģʽ֮Singleton(µ¥Ì¬)

¶¨Òå:
SingletonģʽÖ÷Òª×÷ÓÃÊDZ£Ö¤ÔÚJavaÓ¦ÓóÌÐòÖУ¬Ò»¸öÀàClassÖ»ÓÐÒ»¸öʵÀý´æÔÚ¡£
Ôںܶà²Ù×÷ÖУ¬±ÈÈ罨Á¢Ä¿Â¼ Êý¾Ý¿âÁ¬½Ó¶¼ÐèÒªÕâÑùµÄµ¥Ï̲߳Ù×÷¡£
»¹ÓÐ, singletonÄܹ»±»×´Ì¬»¯;
ÕâÑù£¬¶à¸öµ¥Ì¬ÀàÔÚÒ»Æð¾Í¿ÉÒÔ×÷Ϊһ¸ö״̬²Ö¿âÒ»ÑùÏòÍâÌṩ·þÎñ£¬±ÈÈ磬ÄãÒªÂÛ̳ÖеÄÌû×Ó¼ÆÊýÆ÷£¬Ã¿´Îä¯ÀÀÒ»´ÎÐèÒª¼ÆÊý£¬µ¥Ì¬ÀàÄÜ·ñ±£³ÖסÕâ¸ö¼ÆÊý£¬²¢ÇÒ
ÄÜsynchronizeµÄ°²È«×Ô¶¯¼Ó1£¬Èç¹ûÄãÒª°ÑÕâ¸öÊý×ÖÓÀ¾Ã±£´æµ½Êý¾Ý¿â£¬Äã¿ÉÒÔÔÚ²»Ð޸ĵ¥Ì¬½Ó¿ÚµÄÇé¿öÏ·½±ãµÄ×öµ½¡£
ÁíÍâ·½Ã棬SingletonÒ²Äܹ»±»ÎÞ״̬»¯¡£Ìṩ¹¤¾ßÐÔÖʵŦÄÜ£¬
Singletonģʽ¾ÍΪÎÒÃÇÌṩÁËÕâÑùʵÏֵĿÉÄÜ¡£Ê¹ÓÃSingletonµÄºÃ´¦»¹ÔÚÓÚ¿ÉÒÔ½ÚÊ¡Äڴ棬ÒòΪËüÏÞÖÆÁËʵÀýµÄ¸öÊý£¬ÓÐÀûÓÚJavaÀ¬»ø»ØÊÕ£¨garbage collection£©¡£
ÎÒÃdz£³£¿´µ½¹¤³§Ä£Ê½ÖÐÀà×°ÈëÆ÷(class loader)ÖÐÒ²ÓÃSingletonģʽʵÏÖµÄ,ÒòΪ±»×°ÈëµÄÀàʵ¼ÊÒ²ÊôÓÚ×ÊÔ´¡£
ÈçºÎʹÓÃ?
Ò»°ãSingletonģʽͨ³£Óм¸ÖÖÐÎʽ:
public class Singleton {
¡¡¡¡private Singleton(){}
¡¡¡¡//ÔÚ×Ô¼ºÄÚ²¿¶¨Òå×Ô¼ºÒ»¸öʵÀý£¬ÊDz»ÊǺÜÆæ¹Ö£¿
¡¡¡¡//×¢ÒâÕâÊÇprivate Ö»¹©ÄÚ²¿µ÷ÓÃ
¡¡¡¡private static Singleton instance = new Singleton();
¡¡¡¡//ÕâÀïÌṩÁËÒ»¸ö¹©Íⲿ·ÃÎʱ¾classµÄ¾²Ì¬·½·¨£¬¿ÉÒÔÖ±½Ó·ÃÎÊ
¡¡¡¡public static Singleton getInstance() {
¡¡¡¡¡¡¡¡return instance;
¡¡¡¡ }
}
µÚ¶þÖÖÐÎʽ:
public class Singleton {
¡¡¡¡private static Singleton instance = null;
¡¡¡¡public static synchronized Singleton getInstance() {
¡¡¡¡//Õâ¸ö·½·¨±ÈÉÏÃæÓÐËù¸Ä½ø£¬²»ÓÃÿ´Î¶¼½øÐÐÉú³É¶ÔÏó£¬Ö»ÊǵÚÒ»´Î
¡¡¡¡//ʹÓÃʱÉú³ÉʵÀý£¬Ìá¸ßÁËЧÂÊ£¡
¡¡¡¡if (instance==null)
¡¡¡¡¡¡¡¡instance£½new Singleton();
¡¡¡¡return instance; ¡¡¡¡}
}
ʹÓÃSingleton.getInstance()¿ÉÒÔ·ÃÎʵ¥Ì¬Àà¡£
ÉÏÃæµÚ¶þÖÐÐÎʽÊÇlazy initialization£¬Ò²¾ÍÊÇ˵µÚÒ»´Îµ÷ÓÃʱ³õʼSingleton£¬ÒÔºó¾Í²»ÓÃÔÙÉú³ÉÁË¡£
×¢Òâµ½lazy
initializationÐÎʽÖеÄsynchronized£¬Õâ¸ösynchronizedºÜÖØÒª£¬Èç¹ûûÓÐsynchronized£¬ÄÇôʹÓÃ
getInstance()ÊÇÓпÉÄܵõ½¶à¸öSingletonʵÀý¡£¹ØÓÚlazy
initializationµÄSingletonÓкܶàÉæ¼°double-checked locking (DCL)µÄÌÖÂÛ£¬ÓÐÐËȤÕß½øÒ»²½Ñо¿¡£
Ò»°ãÈÏΪµÚÒ»ÖÖÐÎʽҪ¸ü¼Ó°²È«Ð©¡£
ʹÓÃSingleton×¢ÒâÊÂÏ
ÓÐʱÔÚijЩÇé¿öÏ£¬Ê¹ÓÃSingleton²¢²»ÄÜ´ïµ½SingletonµÄÄ¿µÄ£¬ÈçÓжà¸öSingleton¶ÔÏóͬʱ±»


Ïà¹ØÎĵµ£º

½âÎöJavaÀàºÍ¶ÔÏóµÄ³õʼ»¯¹ý³Ì

ÀàµÄ³õʼ»¯ºÍ¶ÔÏó³õʼ»¯ÊÇ JVM ¹ÜÀíµÄÀàÐÍÉúÃüÖÜÆÚÖзdz£ÖØÒªµÄÁ½¸ö»·½Ú£¬Google ÁËÒ»±éÍøÂ磬ÓйØÀà×°ÔØ»úÖƵÄÎÄÕµ¹ÊDz»ÉÙ£¬È»¶øÀà³õʼ»¯ºÍ¶ÔÏó³õʼ»¯µÄÎÄÕ²¢²»¶à£¬ÌرðÊÇ´Ó×Ö½ÚÂëºÍ JVM ²ã´ÎÀ´·ÖÎöµÄÎÄÕ¸üÊÇÏÊÓÐËù¼û¡£
±¾ÎÄÖ÷Òª¶ÔÀàºÍ¶ÔÏó³õʼ»¯È«¹ý³Ì½øÐзÖÎö£¬Í¨¹ýÒ»¸öʵ¼ÊÎÊÌâÒýÈ룬½«Ô´´úÂëת»»³É JVM ×Ö½ÚÂëºó£¬ ......

jspx Ò»¸öÀ´×ÔÓÚ°£¼°µÄjava web¿ìËÙ¿ª·¢¿ò¼Ü


 jspx Ò»¸öÀ´×ÔÓÚ°£¼°µÄjava web¿ìËÙ¿ª·¢¿ò¼Ü
À´Ô´£ºhttp://www.javaeye.com/news/4887
Ã÷£ºJavaEyeÐÂÎÅÎÄÕµİæȨÊôÓÚJavaEyeÍøÕ¾ËùÓУ¬ÑϽûÈκÎÍøվתÔر¾ÎÄ£¬·ñÔò±Ø½«×·¾¿·¨ÂÉÔðÈΣ¡
ÔõôÓÖð³öÒ»¸öweb¿ò¼Ü?
 
 
¿´¿´ÏÖ´æµÄJava webÓ¦ÓóÌÐò¿ò¼Ü£¬ÎÞÒ»ÀýÍâµÄÒªÇ󻨷ѴóÁ¿µÄʱ¼äÓ뾫Á¦ºó²Å¿ÉͶÈëÉ ......

Îı¾¾ÛÀàËã·¨JavaʵÏÖ

ÍÜÍÜÍƼö£ºÍÜÍܽÌÄãÎı¾¾ÛÀà
ÕªÒª£ºÎı¾¾ÛÀàÊÇËÑË÷ÒýÇæºÍÓïÒåwebµÄ»ù±¾¼¼Êõ£¬Õâ´Î±¾Íܺʹó¼ÒÒ»Æðѧϰһϼòµ¥µÄÎı¾¾ÛÀàËã·¨£¬¿ÉÄܲ»ÄÜÖ±½ÓÓÃÓÚʵ¼ÊÓ¦ÓÃÖУ¬µ«¶ÔÓÚÏëѧËÑË÷¼¼ÊõµÄ³õѧÕß»¹ÊÇÓÐÒ»¶¨ÈëÃÅ×÷Óõġ£ÕâÀï»áÓõ½TF/IDFȨÖØ£¬ÓÃÓàÏҼнǼÆËãÎı¾ÏàËƶȣ¬Ó÷½²î¼ÆËãÁ½¸öÊý¾Ý¼äŷʽ¾àÀ룬ÓÃk-means½øÐÐÊý¾Ý¾ÛÀàµÈÊýѧ ......

Java Mail ·¢ËÍÓʼþ

package com.eastpro.batch.biz;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.security.spec.X509EncodedKeySpec;
public class SendMail {
  private MimeMessage mimeMsg; //MIME mail object
  private Session session;
  ......

javaÖÐfinalµÄÓ÷¨

Ðí¶à³ÌÐòÉè¼ÆÓïÑÔ¶¼ÓÐ×Ô¼ºµÄ°ì·¨¸æËß±àÒëÆ÷ij¸öÊý¾ÝÊÇ“³£Êý”¡£³£ÊýÖ÷ÒªÓ¦ÓÃÓÚÏÂÊöÁ½¸ö·½Ã棺
(1) ±àÒëÆÚ³£Êý£¬ËüÓÀÔ¶²»»á¸Ä±ä
(2) ÔÚÔËÐÐÆÚ³õʼ»¯µÄÒ»¸öÖµ£¬ÎÒÃDz»Ï£ÍûËü·¢Éú±ä»¯
¶Ô
ÓÚ±àÒëÆڵij£Êý£¬±àÒëÆ÷£¨³ÌÐò£©¿É½«³£ÊýÖµ“·â×°”µ½ÐèÒªµÄ¼ÆËã¹ý³ÌÀï¡£Ò²¾ÍÊÇ˵£¬¼ÆËã¿ÉÔÚ±àÒëÆÚ¼äÌáÇ°Ö´ÐУ¬ ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ