java volatile ¹Ø¼ü×Ö
volatile¹Ø¼ü×ÖÓÐʲôÓã¿
¿ÖűȽÏÒ»ÏÂvolatileºÍsynchronizedµÄ²»Í¬ÊÇ×îÈÝÒ×½âÊÍÇå³þµÄ¡£volatileÊDZäÁ¿ÐÞÊηû£¬¶øsynchronizedÔò×÷ÓÃÓÚÒ»¶Î´úÂë»ò·½·¨£»¿´ÈçÏÂÈý¾äget´úÂ룺
int i1; int geti1() {return i1;}
volatile int i2; int geti2() {return i2;}
int i3; synchronized int geti3() {return i3;}
geti1()µÃµ½´æ´¢ÔÚµ±Ç°Ïß³ÌÖÐi1µÄÊýÖµ¡£¶à¸öÏß³ÌÓжà¸öi1±äÁ¿¿½±´£¬¶øÇÒÕâЩi1Ö®¼ä¿ÉÒÔ»¥²»Ïàͬ¡£»»¾ä»°Ëµ£¬ÁíÒ»¸öÏ߳̿ÉÄÜÒѾ¸Ä±äÁËËüÏß³ÌÄÚµÄi1Öµ£¬¶øÕâ¸öÖµ¿ÉÒԺ͵±Ç°Ïß³ÌÖеÄi1Öµ²»Ïàͬ¡£ÊÂʵÉÏ£¬JavaÓиö˼Ïë½Ð¡°Ö÷¡±ÄÚ´æÇøÓò£¬ÕâÀï´æ·ÅÁ˱äÁ¿Ä¿Ç°µÄ¡°×¼È·Öµ¡±¡£Ã¿¸öÏ߳̿ÉÒÔÓÐËü×Ô¼ºµÄ±äÁ¿¿½±´£¬¶øÕâ¸ö±äÁ¿¿½±´Öµ¿ÉÒԺ͡°Ö÷¡±ÄÚ´æÇøÓòÀï´æ·ÅµÄ²»Í¬¡£Òò´Ëʵ¼ÊÉÏ´æÔÚÒ»ÖÖ¿ÉÄÜ£º¡°Ö÷¡±ÄÚ´æÇøÓòÀïµÄi1ÖµÊÇ1£¬Ïß³Ì1ÀïµÄi1ÖµÊÇ2£¬Ïß³Ì2ÀïµÄi1ÖµÊÇ3——ÕâÔÚÏß³Ì1ºÍÏß³Ì2¶¼¸Ä±äÁËËüÃǸ÷×ÔµÄi1Öµ£¬¶øÇÒÕâ¸ö¸Ä±ä»¹Ã»À´µÃ¼°´«µÝ¸ø¡°Ö÷¡±ÄÚ´æÇøÓò»òÆäËûÏß³Ìʱ¾Í»á·¢Éú¡£
¶øgeti2()µÃµ½µÄÊÇ¡°Ö÷¡±ÄÚ´æÇøÓòµÄi2ÊýÖµ¡£ÓÃvolatileÐÞÊκóµÄ±äÁ¿²»ÔÊÐíÓв»Í¬ÓÚ¡°Ö÷¡±ÄÚ´æÇøÓòµÄ±äÁ¿¿½±´¡£»»¾ä»°Ëµ£¬Ò»¸ö±äÁ¿¾volatileÐÞÊκóÔÚËùÓÐÏß³ÌÖбØÐëÊÇͬ²½µÄ£»ÈκÎÏß³ÌÖиıäÁËËüµÄÖµ£¬ËùÓÐÆäËûÏß³ÌÁ¢¼´»ñÈ¡µ½ÁËÏàͬµÄÖµ¡£ÀíËùµ±È»µÄ£¬volatileÐÞÊεıäÁ¿´æȡʱ±ÈÒ»°ã±äÁ¿ÏûºÄµÄ×ÊÔ´Òª¶àÒ»µã£¬ÒòΪÏß³ÌÓÐËü×Ô¼ºµÄ±äÁ¿¿½±´¸üΪ¸ßЧ¡£
¼ÈÈ»volatile¹Ø¼ü×ÖÒѾʵÏÖÁËÏ̼߳äÊý¾Ýͬ²½£¬ÓÖÒªsynchronized¸ÉʲôÄØ£¿ºÇºÇ£¬ËüÃÇÖ®¼äÓÐÁ½µã²»Í¬¡£Ê×ÏÈ£¬synchronized»ñµÃ²¢ÊͷżàÊÓÆ÷——Èç¹ûÁ½¸öÏß³ÌʹÓÃÁËͬһ¸ö¶ÔÏóËø£¬¼àÊÓÆ÷ÄÜÇ¿ÖƱ£Ö¤´úÂë¿éͬʱֻ±»Ò»¸öÏß³ÌËùÖ´ÐЗ—ÕâÊÇÖÚËùÖÜÖªµÄÊÂʵ¡£µ«ÊÇ£¬synchronizedҲͬ²½Äڴ棺ÊÂʵÉÏ£¬synchronizedÔÚ¡°Ö÷¡±ÄÚ´æÇøÓòͬ²½Õû¸öÏ̵߳ÄÄÚ´æ¡£Òò´Ë£¬Ö´ÐÐgeti3()·½·¨×öÁËÈçϼ¸²½£º
1. Ïß³ÌÇëÇó»ñµÃ¼àÊÓthis¶ÔÏóµÄ¶ÔÏóËø£¨¼ÙÉèδ±»Ëø£¬·ñÔòÏ̵߳ȴýÖ±µ½ËøÊÍ·Å£©
2. Ïß³ÌÄÚ´æµÄÊý¾Ý±»Ïû³ý£¬´Ó¡°Ö÷¡±ÄÚ´æÇøÓòÖжÁÈ루JavaÐéÄâ»úÄÜÓÅ»¯´Ë²½¡£¡£¡£[ºóÃæµÄ²»ÖªµÀÔõô±í´ï,º¹]£©
Ïà¹ØÎĵµ£º
Ò»£¬Ïß³Ì״̬µÄ¼ì²é
Ò»°ãÇé¿öÏÂÎÞ·¨È·¶¨Ò»¸öÏ̵߳ÄÔËÐÐ״̬£¬¶ÔÓÚÕâЩ´¦ÓÚδ֪״̬µÄỊ̈߳¬¿ÉÒÔͨ¹ýisAlive()·½·¨ÓÃÀ´È·¶¨Ò»¸öÏß³ÌÊÇ·ñÈÔ´¦Ôڻ״̬¡£µ±È»¼´Ê¹´¦Óڻ״̬µÄÏß³ÌÒ²²¢²»Òâζ×ÅÕâ¸öÏß³ÌÒ»¶¨ÕýÔÚÔËÐУ¬¶ÔÓÚÒ»¸öÒÑ¿ªÊ¼ÔËÐе«»¹Ã»ÓÐÍê³ÉÈÎÎñµÄỊ̈߳¬Õâ¸ö·½·¨·µ»ØֵΪtrue¡£
......
Java 1.1ÔöÌíÁËÒ»ÖÖÓÐȤµÄÌØÐÔ£¬ÃûΪ¡°¶ÔÏóÐòÁл¯¡±£¨Object Serialization£©¡£ËüÃæÏòÄÇЩʵÏÖÁËSerializable½Ó¿ÚµÄ¶ÔÏ󣬿ɽ«ËüÃÇת»»³ÉһϵÁÐ×Ö½Ú£¬²¢¿ÉÔÚÒÔºóÍêÈ«»Ö¸´»ØÔÀ´µÄÑù×Ó¡£ÕâÒ»¹ý³ÌÒà¿Éͨ¹ýÍøÂç½øÐС£ÕâÒâζ×ÅÐòÁл¯»úÖÆÄÜ×Ô¶¯²¹³¥²Ù×÷ϵͳ¼äµÄ²îÒì¡£»»¾ä»°Ëµ£¬¿ÉÒÔÏÈÔÚWindows»úÆ÷ÉÏ´´½¨Ò»¸ö¶ÔÏ󣬶ÔÆäÐòÁл¯£ ......
Ò»°ã£¬ÓÐ3ÖÖʹÓÃËø½øÐÐͬ²½µÄ·½·¨
a.·½·¨Í¬²½£¬ÀýÈçpublic synchronized void xxx()...
b.¾²Ì¬·½·¨Í¬²½,ÀýÈçpublic static synchronized void xxx()...
c.³ÌÐò¿éͬ²½,ÀýÈç
...
&n ......
GOF£ºÔËÓù²Ïí¼¼ÊõÓÐЧµØÖ§³Ö´óÁ¿Ï¸Á£¶ÈµÄ¶ÔÏó¡£
½âÊÍһϸÅÄҲ¾ÍÊÇ˵ÔÚÒ»¸öϵͳÖÐÈç¹ûÓжà¸öÏàͬµÄ¶ÔÏó£¬ÄÇôֻ¹²ÏíÒ»·Ý¾Í¿ÉÒÔ
ÁË£¬²»±Øÿ¸ö¶¼È¥ÊµÀý»¯Ò»¸ö¶ÔÏó¡£±ÈÈç˵£¨ÕâÀïÒýÓÃGOFÊéÖеÄÀý×Ó£©Ò»¸öÎı¾ÏµÍ³£¬Ã¿¸ö×Öĸ¶¨Ò»¸ö¶ÔÏó£¬ÄÇô´óСд×Öĸһ¹²¾ÍÊÇ52¸ö£¬ÄÇô¾ÍÒª¶¨Òå
52¸ö¶ÔÏó¡£Èç¹ûÓÐÒ»¸ö1MµÄÎ ......
½ñÌì·Å¼ÙÒ»Ì죬֮ǰµÄѧϰÈÃÎҸоõÂäÏÂÐí¶à£¬ËùÒÔÔçÉÏÔçÔçµÄÆðÀ´ÔÚÕâÀïд´úÂë ²¹¹¦¿ÎÀ²¡£ForÑ»·´ó¼ÒÓ¦¸Ã²»»áÄ°Éú°É£¬½ñÌì°ÑforÔÙ´ÎÊìϤÏ£¬¹®¹Ì֪ʶÂï¡£
µÚÒ»£¬ÔöÇ¿forµÄʹÓÃÌõ¼þ£º
1.Êý×é 2.Iterator µü´úÆ÷
µÚ¶þ£¬´«Í³FORÓëÔöÇ¿FORµÄ¾ÙÀý±È½Ï:
& ......