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

JAVA¸ß¼¶:¶àºËÏß³Ì volatileÔ­ÀíÓë¼¼ÇÉ

 
ΪʲôʹÓÃvolatile±Èͬ²½´ú¼Û¸üµÍ?
ͬ²½µÄ´ú¼Û, Ö÷ÒªÓɯ䏲¸Ç·¶Î§¾ö¶¨, Èç¹û¿ÉÒÔ½µµÍͬ²½µÄ¸²¸Ç·¶Î§, Ôò¿ÉÒÔ´ó·ùÌáÉý³ÌÐòÐÔÄÜ.
¶øvolatileµÄ¸²¸Ç·¶Î§½ö½ö±äÁ¿¼¶±ðµÄ. Òò´ËËüµÄͬ²½´ú¼ÛºÜµÍ.
volatileÔ­ÀíÊÇʲô?
volatileµÄÓïÒå, ÆäʵÊǸæËß´¦ÀíÆ÷, ²»Òª½«ÎÒ·ÅÈ빤×÷ÄÚ´æ, ÇëÖ±½ÓÔÚÖ÷´æ²Ù×÷ÎÒ.(¹¤×÷ÄÚ´æÏê¼ûjavaÄÚ´æÄ£ÐÍ)
Òò´Ë, µ±¶àºË»ò¶àÏß³ÌÔÚ·ÃÎʸñäÁ¿Ê±, ¶¼½«Ö±½Ó²Ù×÷Ö÷´æ, Õâ´Ó±¾ÖÊÉÏ, ×öµ½Á˱äÁ¿¹²Ïí.
volatileµÄÓÐʲôÓÅÊÆ?
1, ¸ü´óµÄ³ÌÐòÍÌÍÂÁ¿
2, ¸üÉٵĴúÂëʵÏÖ¶àÏß³Ì
3, ³ÌÐòµÄÉìËõÐԽϺÃ
4, ±È½ÏºÃÀí½â, ÎÞÐèÌ«¸ßµÄѧϰ³É±¾
volatileÓÐʲôÁÓÊÆ?
1, ÈÝÒ׳öÎÊÌâ
2, ±È½ÏÄÑÉè¼Æ
volatileÔËËã´æÔÚÔàÊý¾ÝÎÊÌâ
volatile½ö½öÄܱ£Ö¤±äÁ¿¿É¼ûÐÔ, ÎÞ·¨±£Ö¤Ô­×ÓÐÔ.
volatileµÄrace conditionʾÀý:
public class TestRaceCondition {
private volatile int i = 0;
public void increase() {
i++;
}
public int getValue() {
return i;
}
}
µ±¶àÏß³ÌÖ´ÐÐincrease·½·¨Ê±, ÊÇ·ñÄܱ£Ö¤ËüµÄÖµ»áÊÇÏßÐÔµÝÔöµÄÄØ?
´ð°¸ÊÇ·ñ¶¨µÄ.
Ô­Òò:
ÕâÀïµÄincrease·½·¨, Ö´ÐеIJÙ×÷ÊÇi++, ¼´ i = i + 1;
Õë¶Ôi = i + 1, ÔÚ¶àÏß³ÌÖеÄÔËËã, ±¾ÉíÐèÒª¸Ä±äiµÄÖµ.
Èç¹û, ÔÚiÒÑ´ÓÄÚ´æÖÐÈ¡µ½×îÐÂÖµ, µ«Î´Óë1½øÐÐÔËËã, ´ËʱÆäËûÏß³ÌÒÑÊý´Î½«ÔËËã½á¹û¸³Öµ¸øi.
Ôòµ±Ç°Ï߳̽áÊøÊ±, ֮ǰµÄÊý´ÎÔËËã½á¹û¶¼½«±»¸²¸Ç.
¼´, Ö´ÐÐ100´Îincrease, ¿ÉÄܽá¹ûÊÇ < 100.
Ò»°ãÀ´Ëµ, ÕâÖÖÇé¿öÐèÒª½Ï¸ßµÄѹÁ¦Óë²¢·¢Çé¿öÏÂ, ²Å»á³öÏÖ.
ÈçºÎ±ÜÃâÕâÖÖÇé¿ö?
½â¾öÒÔÉÏÎÊÌâµÄ·½·¨:
Ò»ÖÖÊÇ ²Ù×÷ʱ, ¼ÓÉÏͬ²½.
ÕâÖÖ·½·¨, ÎÞÒɽ«´ó´ó½µµÍ³ÌÐòÐÔÄÜ, ÇÒÎ¥±³ÁËvolatileµÄ³õÖÔ.
µÚ¶þÖÖ·½Ê½ÊÇ, ʹÓÃÓ²¼þÔ­Óï(CAS), ʵÏÖ·Ç×èÈûËã·¨
´ÓCPUÔ­ÓïÉÏ, Ö§³Ö±äÁ¿¼¶±ðµÄµÍ¿ªÏúͬ²½.
CPUÔ­Óï-±È½Ï²¢½»»»(CompareAndSet),ʵÏÖ·Ç×èÈûËã·¨
ʲôÊÇCAS?
casÊÇÏÖ´úCPUÌṩ¸ø²¢·¢³ÌÐòʹÓõÄÔ­Óï²Ù×÷. ²»Í¬µÄCPUÓв»Í¬µÄʹÓù淶.
ÔÚ Intel ´¦ÀíÆ÷ÖУ¬±È½Ï²¢½»»»Í¨¹ýÖ¸ÁîµÄ cmpxchg ϵÁÐʵÏÖ¡£
PowerPC ´¦ÀíÆ÷ÓÐÒ»¶ÔÃûΪ“¼ÓÔØ²¢±£Áô”ºÍ“Ìõ¼þ´æ´¢”µÄÖ¸ÁËüÃÇʵÏÖÏàͬµÄÄ¿µØ£»
MIPS Óë PowerPC ´¦ÀíÆ÷ÏàËÆ£¬³ýÁ˵ÚÒ»¸öÖ¸Áî³ÆÎª“¼ÓÔØÁ´½Ó”¡£
CAS ²Ù×÷°üº¬Èý¸ö²Ù×÷Êý —— ÄÚ´æÎ»Öã¨V£©¡¢Ô¤ÆÚÔ­Öµ£¨A£©ºÍÐÂÖµ(B)
ʲôÊÇ·Ç×èÈûËã·¨?
Ò»¸öÏ̵߳Äʧ°Ü»ò¹ÒÆð²»Ó¦¸ÃÓ°ÏìÆäËûÏ̵߳Äʧ°Ü»ò¹ÒÆð.ÕâÀàËã·¨³ÆÖ®Îª·Ç×èÈû(nonblo


Ïà¹ØÎĵµ£º

´Ójava jarÎļþ°üÖжÁ×ÊÔ´µÄ³£Ó÷½·¨

 
ÔÚÄãµÄ´úÂëÀïµ÷ÓÃÁËһЩ×ÊÔ´Îļþ£¬ÈçͼƬ£¬ÒôÀֵȣ¬ÔÚµ÷ÊÔ»·¾³»òµ¥¶ÀÔËÐеÄʱºò¿ÉÒÔÕý³£ÏÔʾ»ò²¥·Å£¬¶øÒ»µ©´ò°üµ½jarÎļþÖУ¬ÕâЩ¶«¶«¾ÍÔÙÒ²³ö²»À´ÁË£¬³ý·Ç°ÑÕâ¸öjar·Åµ½Ô­À´Î´´ò°üÒÔǰµÄĿ¼Ï£¬µ«Í¨³£jarÊǵ¥¶À·¢²¼µÄ¡£
[¹Ø¼ü×Ö] java jarÎļþ°ü ×ÊÔ´
¡¡¡¡¿ÉÄÜÓв»ÉÙ³õѧÕß»áÓÐÕâÑùµÄÀ§»ó£ºÔÚÄãµÄ´úÂëÀïµ÷ÓÃÁË ......

ʹÓÃJAVA¿ª·¢ÏµÍ³ÍÐÅ̳ÌÐò

 Ê¹ÓÃJAVA¿ª·¢ÏµÍ³ÍÐÅ̳ÌÐò
@for&ever 2009-11-27
²Î¿¼ÎÄÕ¡¶New System Tray Functionality in Java SE 6¡·£¬ÍøÖ· http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/systemtray/ ¡£
JDK±ØÐëÒªÔÚ 1.6 °æ±¾¡£
×î½üÏîÄ¿ÖÐҪʵÏÖ¶¨Ê±ÈÎÎñ³£×¤ÏµÍ³£¬Äܹ»Í¬Ê±ÓÐÒ»¸ö±È½ÏÓѺõIJÙ×÷·½Ê½Ìṩ¸ø ......

JavaϵĿò¼Ü±à³Ì(5)

 



   ·´Éä¡¢ProxyºÍÔªÊý¾ÝÊÇJava×îÇ¿µÄÈý¸öÌØÕ÷£¬ÔÙ¼ÓÉÏCGLib (Code Generation Library)
ºÍASM£¬Ê¹µÃJavaËäȻûÓÐRuby£¬Python°ãºóÉú¿É磬һÑùÄÜ×ö³öÇ¿º·µÄ¿ò¼Ü¡£
   Proxy
¿ÉÒÔ¿´×÷ÊÇ΢Ð͵ÄAOP£¬Ã÷°×ÌṩÁËÔڼ̳кÍίÍÐÖ®ÍâµÄµÚÈý¸ö´úÂë·âװ;¾¶£¬Ö»ÒªÓÐ×ã¹»µÄÏëÏóÁ¦£¬¿É ......

JavaỊ̈߳º¸ÅÄîÓëÔ­Àí

 
SCJP5ѧϰ±Ê¼Ç
 
Ò»¡¢²Ù×÷ϵͳÖÐÏ̺߳ͽø³ÌµÄ¸ÅÄî
ÏÖÔڵIJÙ×÷ϵͳÊǶàÈÎÎñ²Ù×÷ϵͳ¡£¶àÏß³ÌÊÇʵÏÖ¶àÈÎÎñµÄÒ»ÖÖ·½Ê½¡£
½ø³ÌÊÇÖ¸Ò»¸öÄÚ´æÖÐÔËÐеÄÓ¦ÓóÌÐò£¬Ã¿¸ö½ø³Ì¶¼ÓÐ×Ô¼º¶ÀÁ¢µÄÒ»¿éÄÚ´æ¿Õ¼ä£¬Ò»¸ö½ø³ÌÖпÉÒÔÆô¶¯¶à¸öÏ̡߳£±ÈÈçÔÚWindowsϵͳÖУ¬Ò»¸öÔËÐеÄexe¾ÍÊÇÒ»¸ö½ø³Ì¡£
 
Ïß³ÌÊÇÖ¸½ø³ÌÖеÄÒ ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ