javaÖйؼü×ÖvolatileµÄ×÷ÓÃ
ÓÃÔÚ¶àỊ̈߳¬Í¬²½±äÁ¿¡£ Ïß³ÌΪÁËÌá¸ßЧÂÊ£¬½«Ä³³ÉÔ±±äÁ¿(ÈçA)¿½±´ÁËÒ»·Ý£¨ÈçB£©£¬Ïß³ÌÖжÔAµÄ·ÃÎÊÆäʵ·ÃÎʵÄÊÇB¡£Ö»ÔÚijЩ¶¯×÷ʱ²Å½øÐÐAºÍBµÄͬ²½¡£Òò´Ë´æÔÚAºÍB²»Ò»ÖµÄÇé¿ö¡£volatile¾ÍÊÇÓÃÀ´±ÜÃâÕâÖÖÇé¿öµÄ¡£volatile¸æËßjvm£¬ ËüËùÐÞÊεıäÁ¿²»±£Áô¿½±´£¬Ö±½Ó·ÃÎÊÖ÷ÄÚ´æÖеģ¨Ò²¾ÍÊÇÉÏÃæ˵µÄA)
=========================·Ö¸îÏß1=================================
°æȨÉùÃ÷ £º×ªÔØʱÇëÒÔ³¬Á´½ÓÐÎʽ±êÃ÷ÎÄÕÂÔʼ³ö´¦ºÍ×÷ÕßÐÅÏ¢¼°±¾ÉùÃ÷
http://aleung.blogbus.com/logs/32090434.html
ÔÚJavaÄÚ´æÄ£ÐÍÖУ¬ÓÐmain memory£¬Ã¿¸öÏß³ÌÒ²ÓÐ×Ô¼ºµÄmemory (ÀýÈç¼Ä´æÆ÷)¡£ÎªÁËÐÔÄÜ£¬Ò»¸öÏ̻߳áÔÚ×Ô¼ºµÄmemoryÖб£³ÖÒª·ÃÎʵıäÁ¿µÄ¸±±¾¡£ÕâÑù¾Í»á³öÏÖͬһ¸ö±äÁ¿ÔÚij¸ö˲¼ä£¬ÔÚÒ»¸öÏ̵߳ÄmemoryÖеÄÖµ¿ÉÄÜÓëÁíÍâÒ»¸öÏß³ÌmemoryÖеÄÖµ£¬»òÕßmain memoryÖеÄÖµ²»Ò»ÖµÄÇé¿ö¡£
Ò»¸ö±äÁ¿ÉùÃ÷Ϊvolatile£¬¾ÍÒâζ×ÅÕâ¸ö±äÁ¿ÊÇËæʱ»á±»ÆäËûÏß³ÌÐ޸ĵģ¬Òò´Ë²»Äܽ«ËücacheÔÚÏß³ÌmemoryÖС£ÒÔÏÂÀý×ÓÕ¹ÏÖÁËvolatileµÄ×÷Óãº
public class StoppableTask extends Thread {
private volatile boolean pleaseStop;
public void run() {
while (!pleaseStop) {
// do some stuff...
}
}
public void tellMeToStop() {
pleaseStop = true;
}
}¼ÙÈçpleaseStopûÓб»ÉùÃ÷Ϊvolatile£¬Ïß³ÌÖ´ÐÐrunµÄʱºò¼ì²éµÄÊÇ×Ô¼ºµÄ¸±±¾£¬¾Í²»Äܼ°Ê±µÃÖªÆäËûÏß³ÌÒѾµ÷ÓÃtellMeToStop()ÐÞ¸ÄÁËpleaseStopµÄÖµ¡£
VolatileÒ»°ãÇé¿öϲ»ÄÜ´úÌæsychronized£¬ÒòΪvolatile²»Äܱ£Ö¤²Ù×÷µÄÔ×ÓÐÔ£¬¼´Ê¹Ö»ÊÇi++£¬Êµ¼ÊÉÏÒ²ÊÇÓɶà¸öÔ×Ó²Ù×÷×é³É£ºread i; inc; write i£¬¼ÙÈç¶à¸öÏß³ÌͬʱִÐÐi++£¬volatileÖ»Äܱ£Ö¤ËûÃDzÙ×÷µÄiÊÇͬһ¿éÄڴ棬µ«ÒÀÈ»¿ÉÄܳöÏÖдÈëÔàÊý¾ÝµÄÇé¿ö¡£Èç¹ûÅäºÏJava 5Ôö¼ÓµÄatomic wrapper classes£¬¶ÔËüÃǵÄincreaseÖ®ÀàµÄ²Ù×÷¾Í²»ÐèÒªsychronized¡£
Reference£º
http://www.javamex.com/tutorials/synchronization_volatile.shtml
http://www.javamex.com/tutorials/synchronization_volatile_java_5.shtml
http://www.ibm.com/developerworks/cn/java/j-jtp06197.html
=========================·Ö¸îÏß2=================================
¿ÖűȽÏÒ»ÏÂvolatileºÍsynchronizedµÄ²»Í¬ÊÇ×îÈÝÒ×½âÊ
Ïà¹ØÎĵµ£º
JAVA±ÊÊÔÌ⾫»ª°æ1.0
public class Test2
{
public static void main(String[] args)
{
Float a = new Float(3.4);
&n ......
javaдÎļþÂÒÂë
StringBuffer sb = new StringBuffer();
sb.append("<table border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"1\" bgcolor=\"b5d6e6\"");
sb.append("<tr>");
sb.append("<td bgcolor=\"#FFFFFE\" height=\"42\" colspan=\""+ (jsd ......
1¡¢Java²ÉÓÃUnicode×Ö·û¼¯£¬Ôö¼ÓÁ˶ԷÇÀ¶¡ÓïϵµÄÖ§³Ö£¬Ò»¸öºº×Ö¿ÉÒÔÖ±½Ó×÷Ϊһ¸ö×Ö·û³£Á¿½øÐи³Öµ¶øÔÚC++ÖбØÐë×öΪ×Ö·û´®´¦Àí
char chTemp = 'ÎÒ';
System.out.println(chTemp);
Java×Ö·ûÊÇÒ»¸ö16ΪµÄÎÞ·ûºÅÕûÊý£¬C++ÖÐΪ8λ
2. Java±êʶ·û¿ÉÒÔÒÔÃÀÔª·û$¿ªÍ·£¬¿Éº¬ÓÐËùÓÐÊ®Áù½øÖÆÖµ´óÓÚ0xc0µ ......
java -verbose[:class|gc|jni] ÔÚÊä³öÉ豸ÉÏÏÔʾÐéÄâ»úÔËÐÐÐÅÏ¢¡£
1.java -verbose:class
ÔÚ³ÌÐòÔËÐеÄʱºò¾¿¾¹»áÓжàÉÙÀà±»¼ÓÔØÄØ£¬Ò»¸ö¼òµ¥³ÌÐò»á¼ÓÔØÉÏ°Ù¸öÀàµÄ£¡Äã¿ÉÒÔÓÃverbose:classÀ´¼àÊÓ£¬ÔÚÃüÁîÐÐÊäÈëjava -verbose:class XXX (XXXΪ³ÌÐòÃû)Äã»áÔÚ¿ØÖÆ̨¿´µ½¼ÓÔصÄÀàµÄÇé¿ö¡£
verboseºÍverbose:cla ......