javaµ¥Ì¬Ä£Ê½½²½â
µ¥Ì¬¶¨Òå:
SingletonģʽÖ÷Òª×÷ÓÃÊDZ£Ö¤ÔÚJavaÓ¦ÓóÌÐòÖУ¬Ò»¸öÀàClassÖ»ÓÐÒ»¸öʵÀý´æÔÚ¡£
Singletonģʽ¾ÍΪÎÒÃÇÌṩÁËÕâÑùʵÏֵĿÉÄÜ¡£Ê¹ÓÃSingletonµÄºÃ´¦»¹ÔÚÓÚ¿ÉÒÔ½ÚÊ¡Äڴ棬ÒòΪËüÏÞÖÆÁËʵÀýµÄ¸öÊý£¬ÓÐÀûÓÚJavaÀ¬»ø»ØÊÕ£¨garbage collection£©¡£
ʹÓÃSingleton×¢ÒâÊÂÏ
ÓÐʱÔÚijЩÇé¿öÏ£¬Ê¹ÓÃSingleton²¢²»ÄÜ´ïµ½SingletonµÄÄ¿µÄ£¬ÈçÓжà¸öSingleton¶ÔÏóͬʱ±»²»Í¬µÄÀà×°ÈëÆ÷×°ÔØ£»ÔÚEJBÕâÑùµÄ·Ö²¼Ê½ÏµÍ³ÖÐʹÓÃҲҪעÒâÕâÖÖÇé¿ö£¬ÒòΪEJBÊÇ¿ç·þÎñÆ÷£¬¿çJVMµÄ
µ¥Ì¬Ä£Ê½µÄÑÝ»¯£º
µ¥Ì¬Ä£Ê½ÊǸö¼òµ¥µÄģʽ£¬µ«ÊÇÕâ¸ö¼òµ¥µÄģʽҲÓкܶิÔӵĶ«Î÷¡£
µ¥Ì¬Ä£Ê½Ò»:
i)
public class Singleton {
¡¡¡¡private Singleton(){}
¡¡¡¡//ÔÚ×Ô¼ºÄÚ²¿¶¨Òå×Ô¼ºÒ»¸öʵÀý£¬ÊDz»ÊǺÜÆæ¹Ö£¿
¡¡¡¡//×¢ÒâÕâÊÇprivate Ö»¹©ÄÚ²¿µ÷ÓÃ
¡¡¡¡private static Singleton instance = new Singleton();
¡¡¡¡//ÕâÀïÌṩÁËÒ»¸ö¹©Íⲿ·ÃÎʱ¾classµÄ¾²Ì¬·½·¨£¬¿ÉÒÔÖ±½Ó·ÃÎÊ¡¡
¡¡ public static Singleton getInstance() {
¡¡¡¡return instance;
¡¡}
¡¡}
ii)µÈͬÓÚÉÏÒ»ÖÖ
public class Singleton {
¡¡¡¡//×¢ÒâÕâÊÇprivate Ö»¹©ÄÚ²¿µ÷ÓÃ
private Singleton(){}
¡¡¡¡private static Singleton instance;
¡¡¡¡//ÕâÀïÌṩÁËÒ»¸ö¹©Íⲿ·ÃÎʱ¾classµÄ¾²Ì¬·½·¨£¬¿ÉÒÔÖ±½Ó·ÃÎÊ¡¡
¡¡ public static Singleton getInstance() {
¡¡¡¡ if (instance == null)
instance = new Singleton();
return instance;
¡¡}
¡¡}
×¢Ò⣺Õâ¸öµ¥Ì¬Ä£Ê½ÊDz»°²È«µÄ£¬ÎªÊ²Ã´ËµÄØ £¿ÒòΪû¿¼ÂǶàỊ̈߳¬ÈçÏÂÇé¿ö
Thread 1 µ÷ÓÃgetInstance() ·½·¨£¬²¢ÇÒÅжÏinstanceÊÇnull£¬È»áá½øÈëifÄ£¿é£¬ÔÚʵÀý»¯instance֮ǰ£¬Thread 2ÇÀÕ¼ÁËThread 1µÄcpu¡£Thread 2 µ÷ÓÃgetInstance() ·½·¨£¬²¢ÇÒÅжÏinstanceÊÇnull£¬È»áá½øÈëifÄ£¿é£¬Thread 2 ʵÀý»¯instance Íê³É£¬
Ïà¹ØÎĵµ£º
ºÜ¼òµ¥µÄСÀý×Ó£¬ÊʺϳõѧÕßÀí½â
1£¬JavaFlex.java
package flex;
public class JavaFlex {
public String helloJavaFlex(String name) {
return name;
}
}
2£¬remoting-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service" class="flex.messaging.services.Remo ......
±äÁ¿,³£Á¿µÄ¸ÅÄî
•
JavaÓïÑÔÖÐÓÐÎåÖÖ²»Í¬ÀàÐ͵ij£Á¿,
–
•1ÕûÐͳ£Á¿:ÈκÎÒ»¸öÕûÐÍÊý×Ö¾ÍÊÇÒ»¸öÕûÐͳ£Á¿.
–JavaÓïÑÔÖÐ,ÕûÐͳ£Á¿ÓÐÈýÖÖ±íʾ·½Ê½:
•Ê®½øÖÆ:0,1,2,3,4,5,6,7,8,9,10,11,12…¶¼ÊÇÊ®½øÖƵij£Á¿
•°Ë½øÖÆ:°Ë½øÖÆ¿ÍÁ÷Á¿Ö»ÄÜÓÃ0,1,2,3,4,5,6,7À ......
JAVAÎļþ²Ù×÷×ܽá
FileÀà
File f = new File(path);
pathΪʵ¼Ê·¾¶£¬¸Ã·¾¶¿ÉÒÔÊÇÎļþ£¬»òÎļþ¼Ð£¬Ò²¿ÉÒÔÊDz»´æÔڵġ£
f.exists() ¿ÉÒÔÅжϸ÷ÊÇ·ñ´æÔÚ¡£
f.isDirectory() ¿ÉÒÔÅжÏÊÇ·ñÊÇÎļþ¼Ð¡£
f.mkdirs(); µÝ¹é´´½¨Îļþ¼Ð
FileºÍÊäÈëÊä³öÁ÷Ö®¼äŦ´øFileInutStream£¬FileOutputStream
URL url = new URL(strUr ......
×òÌì¿´µ½ÕâÑùһƪÌû×Ó£¬ÌÖÂÛÀà¼ÓÔØ˳ÐòµÄ£¬Ò²¿ÉÒÔ˵Êdzõʼ»¯Ë³ÐòµÄ£¬½ñÌìÔçÉÏÕÒ²»µ½ÁË£¬±¾À´Ïë»Ø¸´µÄ£¬ÏÖÔÚ×Ô¼ºÐ´²©¿Í»Ø¸´ÁË£¬ºÇºÇ
public class Singleton {
private static Singleton obj = new Singleton();
public static int counter1 ;
public static int counter2 = 0;& ......
1¡¢¶ÔÏóÊÇ°´ÒýÓô«µÝµÄ---´ø"="ºÅµÄ¸³Öµ²Ù×÷
2¡¢Java Ó¦ÓóÌÐòÓÐÇÒ½öÓеÄÒ»ÖÖ²ÎÊý´«µÝ»úÖÆ£¬¼´°´Öµ´«µÝ---ÓÉ"º¯Êýµ÷ÓÃ"ÒýÆðµÄ
3¡¢°´Öµ´«µÝÒâζ×ŵ±½«Ò»¸ö²ÎÊý´«µÝ¸øÒ»¸öº¯Êýʱ£¬º¯Êý½ÓÊÕµÄÊÇÔʼֵµÄÒ»¸ö¸±±¾
4¡¢°´ÒýÓô«µÝÒâζ×ŵ±½«Ò»¸ö²ÎÊý´«µÝ¸øÒ»¸öº¯Êýʱ£¬º¯Êý½ÓÊÕµÄÊÇÔʼֵµÄÄÚ´æµØÖ·£¬¶ø²»ÊÇÖµ µÄ¸±±¾
5¡¢×¢ ......