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

Java ÀíÂÛÓëʵ¼ù: ÓÃÈõÒýÓöÂסÄÚ´æй© 1

ËäÈ»Óà Java™ ÓïÑÔ±àдµÄ³ÌÐòÔÚÀíÂÛÉÏÊDz»»á³öÏÖ“ÄÚ´æй©”µÄ£¬µ«ÊÇÓÐʱ¶ÔÏóÔÚ²»ÔÙ×÷Ϊ³ÌÐòµÄÂß¼­×´Ì¬µÄÒ»²¿·ÖÖ®ºóÈÔÈ»²»±»À¬»øÊÕ¼¯¡£±¾Ô£¬¸ºÔð±£ÕÏÓ¦ÓóÌÐò½¡¿µµÄ¹¤³Ìʦ Brian Goetz ̽ÌÖÁËÎÞÒâʶµÄ¶ÔÏó±£ÁôµÄ³£¼ûÔ­Òò£¬²¢Õ¹Ê¾ÁËÈçºÎÓÃÈõÒýÓöÂסй©¡£
ÒªÈÃÀ¬»øÊÕ¼¯£¨GC£©»ØÊÕ³ÌÐò²»ÔÙʹÓõĶÔÏ󣬶ÔÏóµÄÂß¼­ ÉúÃüÖÜÆÚ£¨Ó¦ÓóÌÐòʹÓÃËüµÄʱ¼ä£©ºÍ¶Ô¸Ã¶ÔÏóÓµÓеÄÒýÓõÄʵ¼Ê ÉúÃüÖÜÆÚ±ØÐëÊÇÏàͬµÄ¡£ÔÚ´ó¶àÊýʱºò£¬ºÃµÄÈí¼þ¹¤³Ì¼¼Êõ±£Ö¤ÕâÊÇ×Ô¶¯ÊµÏֵģ¬²»ÓÃÎÒÃǶԶÔÏóÉúÃüÖÜÆÚÎÊÌ⻨·Ñ¹ý¶àÐÄ˼¡£µ«ÊÇż¶ûÎÒÃǻᴴ½¨Ò»¸öÒýÓã¬ËüÔÚÄÚ´æÖаüº¬¶ÔÏóµÄʱ¼ä±ÈÎÒÃÇÔ¤ÆÚµÄÒª³¤µÃ¶à£¬ÕâÖÖÇé¿ö³ÆΪÎÞÒâʶµÄ¶ÔÏó±£Áô£¨unintentional object retention£©¡£
È«¾Ö Map Ôì³ÉµÄÄÚ´æй©
ÎÞÒâʶ¶ÔÏó±£Áô×î³£¼ûµÄÔ­ÒòÊÇʹÓà Map ½«ÔªÊý¾ÝÓëÁÙʱ¶ÔÏó£¨transient object£©Ïà¹ØÁª¡£¼Ù¶¨Ò»¸ö¶ÔÏó¾ßÓÐÖеÈÉúÃüÖÜÆÚ£¬±È·ÖÅäËüµÄÄǸö·½·¨µ÷ÓõÄÉúÃüÖÜÆÚ³¤£¬µ«ÊDZÈÓ¦ÓóÌÐòµÄÉúÃüÖÜÆڶ̣¬Èç¿Í»§»úµÄÌ×½Ó×ÖÁ¬½Ó¡£ÐèÒª½«Ò»Ð©ÔªÊý¾ÝÓëÕâ¸öÌ×½Ó×Ö¹ØÁª£¬ÈçÉú³ÉÁ¬½ÓµÄÓû§µÄ±êʶ¡£ÔÚ´´½¨ Socket ʱÊDz»ÖªµÀÕâЩÐÅÏ¢µÄ£¬²¢ÇÒ²»Äܽ«Êý¾ÝÌí¼Óµ½ Socket ¶ÔÏóÉÏ£¬ÒòΪ²»ÄÜ¿ØÖÆ Socket Àà»òÕßËüµÄ×ÓÀà¡£Õâʱ£¬µäÐ͵ķ½·¨¾ÍÊÇÔÚÒ»¸öÈ«¾Ö Map Öд洢ÕâЩÐÅÏ¢£¬ÈçÇåµ¥ 1 ÖÐµÄ SocketManager ÀàËùʾ£º
Çåµ¥ 1. ʹÓÃÒ»¸öÈ«¾Ö Map ½«ÔªÊý¾Ý¹ØÁªµ½Ò»¸ö¶ÔÏó
public class SocketManager {
private Map<Socket,User> m = new HashMap<Socket,User>();

public void setUser(Socket s, User u) {
m.put(s, u);
}
public User getUser(Socket s) {
return m.get(s);
}
public void removeUser(Socket s) {
m.remove(s);
}
}
SocketManager socketManager;
...
socketManager.setUser(socket, user);
ÕâÖÖ·½·¨µÄÎÊÌâÊÇÔªÊý¾ÝµÄÉúÃüÖÜÆÚÐèÒªÓëÌ×½Ó×ÖµÄÉúÃüÖÜÆÚ¹Ò¹³£¬µ«Êdzý·Ç׼ȷµØÖªµÀʲôʱºò³ÌÐò²»ÔÙÐèÒªÕâ¸öÌ×½Ó×Ö£¬²¢¼Çס´Ó Map ÖÐɾ³ýÏàÓ¦µÄÓ³É䣬·ñÔò£¬Socket ºÍ User ¶ÔÏ󽫻áÓÀÔ¶ÁôÔÚ Map ÖУ¬Ô¶Ô¶³¬¹ýÏìÓ¦ÁËÇëÇóºÍ¹Ø±ÕÌ×½Ó×ÖµÄʱ¼ä¡£Õâ»á×èÖ¹ Socket ºÍ User ¶ÔÏó±»À¬»øÊÕ¼¯£¬¼´Ê¹Ó¦ÓóÌÐò²»»áÔÙʹÓÃËüÃÇ¡£ÕâЩ¶ÔÏóÁôÏÂÀ´²»ÊÜ¿ØÖÆ£¬ºÜÈÝÒ×Ôì³É³ÌÐòÔÚ³¤Ê±¼äÔËÐкóÄڴ汬Âú¡£³ýÁË×î¼òµ¥µÄÇé¿ö£¬ÔÚ¼¸ºõËùÓÐÇé¿öÏÂÕÒ³öʲôʱºò Socket ²»ÔÙ±»³ÌÐòʹÓÃÊÇÒ»¼þºÜ·³È˺ÍÈÝÒ׳ö´íµÄÈÎÎ


Ïà¹ØÎĵµ£º

´Ó½ñÌ쿪ʼ£¬°Ñjava´ÓÍ·ÖÁβºÃºÃµÄÊáÀíÒ»±é£¡

ѧjavaÒ²½«½ü¿ìÁ½ÄêµÄʱ¼äÁË£¬Ö®Ç°Ñ§¹ýµÄ¶«Î÷×Ô¼º¸Ð¾õÓеãÄ£ºý£¬ÀíÂÛÕÆÎյIJ»ÊǺÜ͸³¹£¬ÓÐЩÎÊÌâ½â¾öµÄÒ²²»ÊǺÜÈ«Ã棬Ϊ´ËÔÚ´óѧ±ÏҵǰϦ£¬Ïë°Ñ֪ʶºÃºÃµÄÊáÀíһϣ¬°Ñ×Ô¼º¶Ô¼¼ÊõµÄÒɵãºÍһЩÑо¿ÐĵÃдµ½csdn²©¿ÍÉÏ¡£ ......

javaÖÐstringÓëÆäËûÀàÐÍÖ®¼äµÄ»¥Ïàת»»

1.½«Int,Float,Double,Longת»»ÎªString
String s = ""+i;
String s = String.valueOf(i);
String s = Integer.toString(i);
µÚÒ»ÖÖ·½·¨£ºs = ""+i;   //»á²úÉúÁ½¸öString¶ÔÏó
µÚ¶þÖÖ·½·¨£ºs=String.valueOf(i); //Ö±½ÓʹÓÃStringÀàµÄ¾²Ì¬·½·¨£¬Ö»²úÉúÒ»¸ö¶ÔÏó
µÚÈýÖÖ·½·¨£ºÐ§ÂÊ×î¸ß£¿
2.½«Stringת»»ÎªIn ......

javaÈÕÆÚ´¦ÀíSystem£¨1£©

È¡µÃµ±Ç°ÏµÍ³Ê±¼ä   System.currentTimeMillis()
·µ»ØºÁÃëʱ¼ä£¬ÎªlongÐÍÕûÊý£¬
¸Ãº¯ÊýÔÚ½øÐÐϵͳÐÔÄܼà¿Øʱ×î³£Óõ½
Àý£º
  long start = System.currentTimeMillis();
  //Òª¼à¿ØµÄ´úÂë¶Î
  long end = System.currentTimeMillis();
  System.out.println(end-start);
ÓÐʱҪ¼à¿Øµ ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ