¡¡¡¡¼¸ºõËùÓÐʹÓà AWT »ò Swing ±àдµÄ»Í¼³ÌÐò¶¼ÐèÒª¶àÏ̡߳£µ«¶àÏ̳߳ÌÐò»áÔì³ÉÐí¶àÀ§ÄÑ£¬¸Õ¿ªÊ¼±à³ÌµÄ¿ª·¢Õß³£³£»á·¢ÏÖËûÃDZ»Ò»Ð©ÎÊÌâËùÕÛÄ¥£¬ÀýÈç²»ÕýÈ·µÄ³ÌÐòÐÐΪ»òËÀËø¡£
??ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«Ì½ÌÖʹÓöàÏß³ÌʱÓöµ½µÄÎÊÌ⣬²¢Ìá³öÄÇЩ³£¼ûÏÝÚåµÄ½â¾ö·½°¸¡£
??Ïß³ÌÊÇʲô£¿
??Ò»¸ö³ÌÐò»ò½ø³ÌÄܹ»°üº¬¶à¸öỊ̈߳¬ÕâЩÏ߳̿ÉÒÔ¸ù¾Ý³ÌÐòµÄ´úÂëÖ´ÐÐÏàÓ¦µÄÖ¸Áî¡£¶àÏ߳̿´ÉÏÈ¥ËƺõÔÚ²¢ÐÐÖ´ÐÐËüÃǸ÷×ԵŤ×÷£¬¾ÍÏñÔÚһ̨¼ÆËã»úÉÏÔËÐÐ×Ŷà¸ö´¦Àí»úÒ»Ñù¡£ÔڶദÀí»ú¼ÆËã»úÉÏʵÏÖ¶àÏß³Ìʱ£¬ËüÃÇȷʵ¿ÉÒÔ²¢Ðй¤×÷¡£ºÍ½ø³Ì²»Í¬µÄÊÇ£¬Ï̹߳²ÏíµØÖ·¿Õ¼ä¡£Ò²¾ÍÊÇ˵£¬¶à¸öÏß³ÌÄܹ»¶ÁдÏàͬµÄ±äÁ¿»òÊý¾Ý½á¹¹¡£
??±àд¶àÏ̳߳ÌÐòʱ£¬Äã±ØÐë×¢Òâÿ¸öÏß³ÌÊÇ·ñ¸ÉÈÅÁËÆäËûÏ̵߳Ť×÷¡£¿ÉÒÔ½«³ÌÐò¿´×÷Ò»¸ö°ì¹«ÊÒ£¬Èç¹û²»ÐèÒª¹²Ïí°ì¹«ÊÒ×ÊÔ´»òÓëÆäËûÈ˽»Á÷£¬ËùÓÐÖ°Ô±¾Í»á¶ÀÁ¢²¢Ðеع¤×÷¡£Ä³¸öÖ°Ô±ÈôÒªºÍÆäËûÈ˽»Ì¸£¬µ±ÇÒ½öµ±¸ÃÖ°Ô±ÔÚ¡°Ìý¡±ÇÒËûÃÇÁ½ËµÍ¬ÑùµÄÓïÑÔ¡£´ËÍ⣬ֻÓÐÔÚ¸´Ó¡»ú¿ÕÏÐÇÒ´¦ÓÚ¿ÉÓÃ״̬£¨Ã»ÓнöÍê³ÉÒ»°ëµÄ¸´Ó¡¹¤×÷£¬Ã»ÓÐÖ½ÕÅ×èÈûµÈÎÊÌ⣩ʱ£¬Ö°Ô±²ÅÄܹ»Ê¹ÓÃËü¡£ÔÚÕâƪÎÄÕÂÖÐÄ㽫¿´µ½£¬ÔÚ Java ³ÌÐòÖл¥ÏàÐ×÷µÄÏ߳̾ͺÃÏñÊÇÔÚÒ»¸ö×éÖ¯Á¼ºÃµÄ»ú¹¹Öй¤×÷µÄÖ°Ô±¡£
??ÔÚ¶àÏ̳߳ÌÐòÖУ¬Ï߳̿ÉÒÔ´Ó×¼±¸¾ÍÐ÷¶ÓÁÐÖеõ½£¬²¢ÔÚ¿É»ñµÃµÄϵͳ CPU ÉÏÔËÐС£²Ù×÷ϵͳ¿ÉÒÔ½«Ï̴߳Ӵ¦ÀíÆ÷ÒƵ½×¼±¸¾ÍÐ÷¶ÓÁлò×èÈû¶ÓÁÐÖУ¬ÕâÖÖÇé¿ö¿ÉÒÔÈÏΪÊÇ´¦ÀíÆ÷¡°¹ÒÆð¡±Á˸ÃÏ̡߳£Í¬Ñù£¬Java ÐéÄâ»ú (JVM) Ò²¿ÉÒÔ¿ØÖÆÏ̵߳ÄÒƶ¯----ÔÚÐ×÷»òÇÀÏÈÄ£ÐÍÖÐ----´Ó×¼±¸¾ÍÐ÷¶ÓÁÐÖн«½ø³ÌÒƵ½´¦ÀíÆ÷ÖУ¬ÓÚÊǸÃÏ߳̾ͿÉÒÔ¿ªÊ¼Ö´ÐÐËüµÄ³ÌÐò´úÂë¡£
??Ð×÷ʽÏß³ÌÄ£ÐÍÔÊÐíÏß³Ì×Ô¼º¾ö¶¨Ê²Ã´Ê±ºò·ÅÆú´¦ÀíÆ÷À´µÈ´ýÆäËûµÄÏ̡߳£³ÌÐò¿ª·¢Ô±¿ÉÒÔ¾«È·µØ¾ö¶¨Ä³¸öÏ̺߳Îʱ»á±»ÆäËûÏ̹߳ÒÆð£¬ÔÊÐíËüÃÇÓë¶Ô·½ÓÐЧµØºÏ×÷¡£È±µãÔÚÓÚijЩ¶ñÒâ»òÊÇдµÃ²»ºÃµÄÏ̻߳áÏûºÄËùÓпɻñµÃµÄ CPU ʱ¼ä£¬µ¼ÖÂÆäËûÏ̡߳°¼¢¶ö¡±¡£
??ÔÚÇÀռʽÏß³ÌÄ£ÐÍÖУ¬²Ù×÷ϵͳ¿ÉÒÔÔÚÈκÎʱºò´ò¶ÏÏ̡߳£Í¨³£»áÔÚËüÔËÐÐÁËÒ»¶Îʱ¼ä£¨¾ÍÊÇËùνµÄÒ»¸öʱ¼äƬ£©ºó²Å´ò¶ÏËü¡£ÕâÑùµÄ½á¹û×ÔÈ»ÊÇûÓÐÏß³ÌÄܹ»²»¹«Æ½µØ³¤Ê±¼ä°ÔÕ¼´¦ÀíÆ÷¡£È»¶ø£¬Ëæʱ¿ÉÄÜ´ò¶ÏÏ߳̾ͻá¸ø³ÌÐò¿ª·¢Ô±´øÀ´ÆäËûÂé·³¡£Í¬ÑùʹÓð칫ÊÒµÄÀý×Ó£¬¼ÙÉèij¸öÖ°Ô±ÇÀÔÚÁíÒ»ÈËǰʹÓø´Ó¡»ú£¬µ«´òÓ¡¹¤×÷ÔÚδÍê³ÉµÄʱºòÀ뿪ÁË£¬ÁíÒ»È˽Ó×ÅʹÓø´Ó¡»úʱ£¬¸Ã¸´Ó¡»úÉÏ¿ÉÄܾͻ¹ÓÐÏÈÇ°ÄÇÃûÖ°Ô±ÁôÏÂÀ´µÄ×ÊÁÏ¡£ÇÀռʽÏß³ÌÄ£ÐÍÒªÇóÏß³ÌÕýÈ·¹²Ïí×ÊÔ´£¬Ð×÷ʽģÐÍÈ´ÒªÇóÏ̹߳²ÏíÖ´ÐÐʱ¼ä¡£ÓÉÓÚ JVM ¹æ·¶²¢Ã»ÓÐÌرð¹æ¶¨Ïß³ÌÄ£ÐÍ£¬Java ¿ª·¢Ô±±ØÐë±àд¿ÉÔÚÁ½ÖÖÄ£ÐÍÉÏÕýÈ·ÔËÐеijÌÐò¡£ÔÚÁ˽âÏß³ÌÒÔ¼°Ï̼߳äͨѶµÄһЩ·½ÃæÖ®ºó£¬ÎÒÃÇ¿ÉÒÔ¿´µ½ÈçºÎΪÕâÁ½ÖÖÄ£ÐÍÉè¼Æ³ÌÐò¡£
??Ïß³ÌºÍ Java ÓïÑÔ
??ΪÁËʹÓà Java ÓïÑÔ´´½¨Ị̈߳¬Äã¿ÉÒÔÉú³ÉÒ»¸ö Thread Àࣨ»òÆä×ÓÀࣩµÄ¶ÔÏ󣬲¢¸øÕâ¸ö¶ÔÏó·¢ËÍ start() ÏûÏ¢¡££¨³ÌÐò¿ÉÒÔÏòÈκÎÒ»¸öÅÉÉú×Ô Runnable ½Ó¿ÚµÄÀà¶ÔÏó·¢ËÍ start() ÏûÏ¢¡££©Ã¿¸öÏ̶߳¯×÷µÄ¶¨Òå°üº¬ÔÚ¸ÃÏ̶߳ÔÏóµÄ run() ·½·¨ÖС£run ·½·¨¾ÍÏ൱ÓÚ´«Í³³ÌÐòÖÐµÄ main() ·½·¨£»Ï̻߳á³ÖÐøÔËÐУ¬Ö±µ½ run() ·µ»ØΪֹ£¬´Ëʱ¸ÃÏ̱߳ãËÀÁË¡£
??ÉÏËø
??´ó¶àÊýÓ¦ÓóÌÐòÒªÇóÏ̻߳¥ÏàͨÐÅÀ´Í¬²½ËüÃǵĶ¯×÷¡£ÔÚ Java ³ÌÐòÖÐ×î¼òµ¥ÊµÏÖͬ²½µÄ·½·¨¾ÍÊÇÉÏËø¡£ÎªÁË·Àֹͬʱ·ÃÎʹ²Ïí×ÊÔ´£¬Ïß³ÌÔÚʹÓÃ×ÊÔ´µÄÇ°ºó¿ÉÒÔ¸ø¸Ã×ÊÔ´ÉÏËøºÍ¿ªËø¡£¼ÙÏë¸ø¸´Ó¡»úÉÏËø£¬ÈÎһʱ¿ÌÖ»ÓÐÒ»¸öÖ°Ô±ÓµÓÐÔ¿³×¡£ÈôûÓÐÔ¿³×¾Í²»ÄÜʹÓø´Ó¡»ú¡£¸ø¹²Ïí±äÁ¿ÉÏËø¾ÍʹµÃ Java Ïß³ÌÄܹ»¿ìËÙ·½±ãµØͨÐźÍͬ²½¡£Ä³¸öÏß³ÌÈô¸øÒ»¸ö¶ÔÏóÉÏÁËËø£¬¾Í¿ÉÒÔÖªµÀûÓÐÆäËûÏß³ÌÄܹ»·ÃÎʸöÔÏó¡£¼´Ê¹ÔÚÇÀռʽģÐÍÖУ¬ÆäËûÏß³ÌÒ²²»Äܹ»·ÃÎʴ˶ÔÏó£¬Ö±µ½ÉÏËøµÄÏ̱߳»»½ÐÑ¡¢Íê³É¹¤×÷²¢¿ªËø¡£ÄÇЩÊÔͼ·ÃÎÊÒ»¸öÉÏËø¶ÔÏóµÄÏß³Ìͨ³£»á½øÈë˯Ãß״̬£¬Ö±µ½ÉÏËøµÄÏ߳̿ªËø¡£Ò»µ©Ëø±»´ò¿ª£¬ÕâЩ˯Ãß½ø³Ì¾Í»á±»»½ÐѲ¢ÒƵ½×¼±¸¾ÍÐ÷¶ÓÁÐÖС£
??ÔÚ Java ±à³ÌÖУ¬ËùÓеĶÔÏó¶¼ÓÐËø¡£Ï߳̿ÉÒÔʹÓà synchronized ¹Ø¼ü×ÖÀ´»ñµÃËø¡£ÔÚÈÎһʱ¿Ì¶ÔÓÚ¸ø¶¨µÄÀàµÄʵÀý£¬·½·¨»òͬ²½µÄ´úÂë¿éÖ»Äܱ»Ò»¸öÏß³ÌÖ´ÐС£ÕâÊÇÒòΪ´úÂëÔÚÖ´ÐÐ֮ǰҪÇó»ñµÃ¶ÔÏóµÄËø¡£¼ÌÐøÎÒÃǹØÓÚ¸´Ó¡»úµÄ±ÈÓ÷£¬ÎªÁ˱ÜÃ⸴ӡ³åÍ»£¬ÎÒÃÇ¿ÉÒÔ¼òµ¥µØ¶Ô¸´Ó¡×ÊԴʵÐÐͬ²½¡£ÈçͬÏÂÁеĴúÂëÀý×Ó£¬ÈÎһʱ¿ÌÖ»ÔÊÐíһλְԱʹÓø´Ó¡×ÊÔ´¡£Í¨¹ýʹÓ÷½·¨£¨ÔÚ Copier ¶ÔÏóÖУ©À´Ð޸ĸ´Ó¡»ú״̬¡£Õâ¸ö·½·¨¾ÍÊÇͬ²½·½·¨¡£Ö»ÓÐÒ»¸öÏß³ÌÄܹ»Ö´ÐÐÒ»¸ö Copier ¶ÔÏóÖÐͬ²½´úÂ룬Òò´ËÄÇЩÐèҪʹÓà Copier ¶ÔÏóµÄÖ°Ô±¾Í±ØÐëÅŶӵȺò¡£
class CopyMachine {
public synchronized void makeCopies(Document d, int nCopies) {
// only one thread executes this at a time
}
public void loadPaper() {
// multiple threads could access this at once!
synchronized(this) {
// only one thread accesses this at a time
// feel free to use shared resources, overwrite members, etc.
}
}
}
??Fine-grain Ëø
??ÔÚ¶ÔÏó¼¶Ê¹ÓÃËøͨ³£ÊÇÒ»ÖֱȽϴֲڵķ½·¨¡£ÎªÊ²Ã´Òª½«Õû¸ö¶ÔÏó¶¼ÉÏËø£¬¶ø²»ÔÊÐíÆäËûÏ̶߳ÌÔݵØʹÓöÔÏóÖÐÆäËûͬ²½·½·¨À´·ÃÎʹ²Ïí×ÊÔ´£¿Èç¹ûÒ»¸ö¶ÔÏóÓµÓжà¸ö×ÊÔ´£¬¾Í²»ÐèҪֻΪÁËÈÃÒ»¸öÏß³ÌʹÓÃÆäÖÐÒ»²¿·Ö×ÊÔ´£¬¾Í½«ËùÓÐÏ̶߳¼ËøÔÚÍâÃæ¡£ÓÉÓÚÿ¸ö¶ÔÏó¶¼ÓÐËø£¬¿ÉÒÔÈçÏÂËùʾʹÓÃÐéÄâ¶ÔÏóÀ´ÉÏËø£º
class FineGrainLock {
MyMemberClass x, y;
Object xlock = new Object(), ylock = new Object();
public void foo() {
synchronized(xlock) {
// access x here
}
// do something here - but don't use shared resources
synchronized(ylock) {
// access y here
}
}
public void bar() {
synchronized(this) {
// access both x and y here
}
// do something here - but don't use shared resources
}
}
??ÈôΪÁËÔÚ·½·¨¼¶ÉÏͬ²½£¬²»Äܽ«Õû¸ö·½·¨ÉùÃ÷Ϊ synchronized ¹Ø¼ü×Ö¡£ËüÃÇʹÓõÄÊdzÉÔ±Ëø£¬¶ø²»ÊÇ synchronized ·½·¨Äܹ»»ñµÃµÄ¶ÔÏó¼¶Ëø¡£
??ÐźÅÁ¿
??ͨ³£Çé¿öÏ£¬¿ÉÄÜÓжà¸öÏß³ÌÐèÒª·ÃÎÊÊýÄ¿ºÜÉÙµÄ×ÊÔ´¡£¼ÙÏëÔÚ·þÎñÆ÷ÉÏÔËÐÐ×ÅÈô¸É¸ö»Ø´ð¿Í»§¶ËÇëÇóµÄÏ̡߳£ÕâЩÏß³ÌÐèÒªÁ¬½Óµ½Í¬Ò»Êý¾Ý¿â£¬µ«ÈÎһʱ¿ÌÖ»ÄÜ»ñµÃÒ»¶¨ÊýÄ¿µÄÊý¾Ý¿âÁ¬½Ó¡£ÄãÒªÔõÑù²ÅÄܹ»ÓÐЧµØ½«ÕâЩ¹Ì¶¨ÊýÄ¿µÄÊý¾Ý¿âÁ¬½Ó·ÖÅä¸ø´óÁ¿µÄỊ̈߳¿Ò»ÖÖ¿ØÖÆ·ÃÎÊÒ»×é×ÊÔ´µÄ·½·¨£¨³ýÁ˼òµ¥µØÉÏËøÖ®Í⣩£¬¾ÍÊÇʹÓÃÖÚËùÖÜÖªµÄÐźÅÁ¿¼ÆÊý (counting semaphore)¡£ÐźÅÁ¿¼ÆÊý½«Ò»×é¿É»ñµÃ×ÊÔ´µÄ¹ÜÀí·â×°ÆðÀ´¡£ÐźÅÁ¿ÊÇÔÚ¼òµ¥ÉÏËøµÄ»ù´¡ÉÏʵÏÖµÄ,Ï൱ÓÚÄÜÁîḬ̈߳²È«Ö´ÐУ¬²¢³õʼ»¯Îª¿ÉÓÃ×ÊÔ´¸öÊýµÄ¼ÆÊýÆ÷¡£ÀýÈçÎÒÃÇ¿ÉÒÔ½«Ò»¸öÐźÅÁ¿³õʼ»¯Îª¿É»ñµÃµÄÊý¾Ý¿âÁ¬½Ó¸öÊý¡£Ò»µ©Ä³¸öÏ̻߳ñµÃÁËÐźÅÁ¿£¬¿É»ñµÃµÄÊý¾Ý¿âÁ¬½ÓÊý¼õÒ»¡£Ïß³ÌÏûºÄÍê×ÊÔ´²¢ÊͷŸÃ×ÊԴʱ£¬¼ÆÊýÆ÷¾Í»á¼ÓÒ»¡£µ±ÐźÅÁ¿¿ØÖƵÄËùÓÐ×ÊÔ´¶¼Òѱ»Õ¼ÓÃʱ£¬ÈôÓÐÏß³ÌÊÔͼ·ÃÎÊ´ËÐźÅÁ¿£¬Ôò»á½øÈë×èÈû״̬£¬Ö±µ½ÓпÉÓÃ×ÊÔ´±»ÊÍ·Å¡£
??ÐźÅÁ¿×î³£¼ûµÄÓ÷¨Êǽâ¾ö¡°Ïû·ÑÕߣÉú²úÕßÎÊÌ⡱¡£µ±Ò»¸öÏ߳̽øÐй¤×÷ʱ£¬ÈôÁíÍâÒ»¸öÏ̷߳ÃÎÊͬһ¹²Ïí±äÁ¿£¬¾Í¿ÉÄܲúÉú´ËÎÊÌâ¡£Ïû·ÑÕßÏß³ÌÖ»ÄÜÔÚÉú²úÕßÏß³ÌÍê³ÉÉú²úºó²ÅÄܹ»·ÃÎÊÊý¾Ý¡£Ê¹ÓÃÐźÅÁ¿À´½â¾öÕâ¸öÎÊÌ⣬¾ÍÐèÒª´´½¨Ò»¸ö³õʼ»¯ÎªÁãµÄÐźÅÁ¿£¬´Ó¶øÈÃÏû·ÑÕßÏ̷߳ÃÎÊ´ËÐźÅÁ¿Ê±·¢Éú×èÈû¡£Ã¿µ±Íê³Éµ¥Î»¹¤×÷ʱ£¬Éú²úÕßÏ߳̾ͻáÏò¸ÃÐźÅÁ¿·¢Ðźţ¨ÊÍ·Å×ÊÔ´£©¡£Ã¿µ±Ïû·ÑÕßÏß³ÌÏû·ÑÁ˵¥Î»Éú²ú½á¹û²¢ÐèҪеÄÊý¾Ýµ¥ÔªÊ±£¬Ëü¾Í»áÊÔͼÔٴλñÈ¡ÐźÅÁ¿¡£Òò´ËÐźÅÁ¿µÄÖµ¾Í×ÜÊǵÈÓÚÉú²úÍê±Ï¿É¹©Ïû·ÑµÄÊý¾Ýµ¥ÔªÊý¡£ÕâÖÖ·½·¨±È²ÉÓÃÏû·ÑÕßÏ̲߳»Í£¼ì²éÊÇ·ñÓпÉÓÃÊý¾Ýµ¥ÔªµÄ·½·¨Òª¸ßЧµÃ¶à¡£ÒòΪÏû·ÑÕßÏß³ÌÐÑÀ´ºó£¬ÌÈÈôûÓÐÕÒµ½¿ÉÓõÄÊý¾Ýµ¥Ôª£¬¾Í»áÔٶȽøÈë˯Ãß״̬£¬ÕâÑùµÄ²Ù×÷ϵͳ¿ªÏúÊǷdz£°º¹óµÄ¡£
??¾¡¹ÜÐźÅÁ¿²¢Î´Ö±½Ó±» Java ÓïÑÔËùÖ§³Ö£¬È´ºÜÈÝÒ×ÔÚ¸ø¶ÔÏóÉÏËøµÄ»ù´¡ÉÏʵÏÖ¡£Ò»¸ö¼òµ¥µÄʵÏÖ·½·¨ÈçÏÂËùʾ£º
class Semaphore {
private int count;
public Semaphore(int n) {
this.count = n;
}
public synchronized void acquire() {
while(count == 0) {
try {
wait();
} catch (InterruptedException e) {
// keep trying
}
}
count--;
}
public synchronized void release() {
count++;
notify(); // alert a thread that's blocking on this semaphore
}
}
??³£¼ûµÄÉÏËøÎÊÌâ
??²»ÐÒµÄÊÇ£¬Ê¹ÓÃÉÏËø»á´øÀ´ÆäËûÎÊÌâ¡£ÈÃÎÒÃÇÀ´¿´Ò»Ð©³£¼ûÎÊÌâÒÔ¼°ÏàÓ¦µÄ½â¾ö·½·¨£º
??ËÀËø¡£ËÀËøÊÇÒ»¸ö¾µäµÄ¶àÏß³ÌÎÊÌ⣬ÒòΪ²»Í¬µÄÏ̶߳¼ÔڵȴýÄÇЩ¸ù±¾²»¿ÉÄܱ»ÊͷŵÄËø£¬´Ó¶øµ¼ÖÂËùÓеŤ×÷¶¼ÎÞ·¨Íê³É¡£¼ÙÉèÓÐÁ½¸öỊ̈߳¬·Ö±ð´ú±íÁ½¸ö¼¢¶öµÄÈË£¬ËûÃDZØÐë¹²Ïíµ¶²æ²¢ÂÖÁ÷³Ô·¹¡£ËûÃǶ¼ÐèÒª»ñµÃÁ½¸öËø£º¹²Ïíµ¶ºÍ¹²Ïí²æµÄËø¡£¼ÙÈçÏß³Ì "A" »ñµÃÁ˵¶£¬¶øÏß³Ì "B" »ñµÃÁ˲档Ïß³Ì A ¾Í»á½øÈë×èÈû״̬À´µÈ´ý»ñµÃ²æ£¬¶øÏß³Ì B Ôò×èÈûÀ´µÈ´ý A ËùÓµÓеĵ¶¡£ÕâÖ»ÊÇÈËΪÉè¼ÆµÄÀý×Ó£¬µ«¾¡¹ÜÔÚÔËÐÐʱºÜÄÑ̽²âµ½£¬ÕâÀàÇé¿öȴʱ³£·¢Éú¡£ËäȻҪ̽²â»òÍÆÇø÷ÖÖÇé¿öÊǷdz£À§Äѵģ¬µ«Ö»Òª°´ÕÕÏÂÃ漸Ìõ¹æÔòÈ¥Éè¼Æϵͳ£¬¾ÍÄܹ»±ÜÃâËÀËøÎÊÌ⣺
??ÈÃËùÓеÄḬ̈߳´ÕÕͬÑùµÄ˳Ðò»ñµÃÒ»×éËø¡£ÕâÖÖ·½·¨Ïû³ýÁË X ºÍ Y µÄÓµÓÐÕß·Ö±ðµÈ´ý¶Ô·½µÄ×ÊÔ´µÄÎÊÌâ¡£
??½«¶à¸öËø×é³ÉÒ»×é²¢·Åµ½Í¬Ò»¸öËøÏ¡£Ç°ÃæËÀËøµÄÀý×ÓÖУ¬¿ÉÒÔ´´½¨Ò»¸öÒøÆ÷¶ÔÏóµÄËø¡£ÓÚÊÇÔÚ»ñµÃµ¶»ò²æ֮ǰ¶¼±ØÐë»ñµÃÕâ¸öÒøÆ÷µÄËø¡£
??½«ÄÇЩ²»»á×èÈûµÄ¿É»ñµÃ×ÊÔ´ÓñäÁ¿±êÖ¾³öÀ´¡£µ±Ä³¸öÏ̻߳ñµÃÒøÆ÷¶ÔÏóµÄËøʱ£¬¾Í¿ÉÒÔͨ¹ý¼ì²é±äÁ¿À´ÅжÏÊÇ·ñÕû¸öÒøÆ÷¼¯ºÏÖеĶÔÏóËø¶¼¿É»ñµÃ¡£Èç¹ûÊÇ£¬Ëü¾Í¿ÉÒÔ»ñµÃÏà¹ØµÄËø£¬·ñÔò£¬¾ÍÒªÊͷŵôÒøÆ÷Õâ¸öËø²¢ÉÔºóÔÙ³¢ÊÔ¡£
??×îÖØÒªµÄÊÇ£¬ÔÚ±àд´úÂëÇ°ÈÏÕæ×ÐϸµØÉè¼ÆÕû¸öϵͳ¡£¶àÏß³ÌÊÇÀ§Äѵģ¬ÔÚ¿ªÊ¼±à³Ì֮ǰÏêϸÉè¼ÆϵͳÄܹ»°ïÖúÄã±ÜÃâÄÑÒÔ·¢ÏÖËÀËøµÄÎÊÌâ¡£
??Volatile ±äÁ¿. volatile ¹Ø¼ü×ÖÊÇ Java ÓïÑÔΪÓÅ»¯±àÒëÆ÷Éè¼ÆµÄ¡£ÒÔÏÂÃæµÄ´úÂëΪÀý£º
class VolatileTest {
public void foo() {
boolean flag = false;
if(flag) {
// this could happen
}
}
}
??Ò»¸öÓÅ»¯µÄ±àÒëÆ÷¿ÉÄÜ»áÅжϳö if ²¿·ÖµÄÓï¾äÓÀÔ¶²»»á±»Ö´ÐУ¬¾Í¸ù±¾²»»á±àÒëÕⲿ·ÖµÄ´úÂë¡£Èç¹ûÕâ¸öÀà±»¶àÏ̷߳ÃÎÊ£¬flag ±»Ç°Ãæij¸öÏß³ÌÉèÖÃÖ®ºó£¬ÔÚËü±» if Óï¾ä²âÊÔ֮ǰ£¬¿ÉÒÔ±»ÆäËûÏß³ÌÖØÐÂÉèÖá£Óà volatile ¹Ø¼ü×ÖÀ´ÉùÃ÷±äÁ¿£¬¾Í¿ÉÒÔ¸æËß±àÒëÆ÷ÔÚ±àÒëµÄʱºò£¬²»ÐèҪͨ¹ýÔ¤²â±äÁ¿ÖµÀ´ÓÅ»¯Õⲿ·ÖµÄ´úÂë¡£
??ÎÞ·¨·ÃÎʵÄÏß³Ì ÓÐʱºòËäÈ»»ñÈ¡¶ÔÏóËøûÓÐÎÊÌ⣬Ïß³ÌÒÀÈ»ÓпÉÄܽøÈë×èÈû״̬¡£ÔÚ Java ±à³ÌÖÐ IO ¾ÍÊÇÕâÀàÎÊÌâ×îºÃµÄÀý×Ó¡£µ±Ïß³ÌÒòΪ¶ÔÏóÄÚµÄ IO µ÷Óöø×èÈûʱ£¬´Ë¶ÔÏóÓ¦µ±ÈÔÄܱ»ÆäËûÏ̷߳ÃÎÊ¡£¸Ã¶ÔÏóͨ³£ÓÐÔðÈÎÈ¡ÏûÕâ¸ö×èÈûµÄ IO ²Ù×÷¡£Ôì³É×èÈûµ÷ÓõÄÏ̳߳£³£»áÁîͬ²½ÈÎÎñʧ°Ü¡£Èç¹û¸Ã¶ÔÏóµÄÆäËû·½·¨Ò²ÊÇͬ²½µÄ£¬µ±Ï̱߳»×èÈûʱ£¬´Ë¶ÔÏóÒ²¾ÍÏ൱ÓÚ±»À䶳סÁË¡£ÆäËûµÄÏß³ÌÓÉÓÚ²»ÄÜ»ñµÃ¶ÔÏóµÄËø£¬¾Í²»Äܸø´Ë¶ÔÏó·¢ÏûÏ¢£¨ÀýÈ磬ȡÏû IO ²Ù×÷£©¡£±ØÐëÈ·±£²»ÔÚͬ²½´úÂëÖаüº¬ÄÇЩ×èÈûµ÷Ó㬻òÈ·ÈÏÔÚÒ»¸öÓÃͬ²½×èÈû´úÂëµÄ¶ÔÏóÖдæÔÚ·Çͬ²½·½·¨¡£¾¡¹ÜÕâÖÖ·½·¨ÐèÒª»¨·ÑһЩעÒâÁ¦À´±£Ö¤½á¹û´úÂ밲ȫÔËÐУ¬µ«ËüÔÊÐíÔÚÓµÓжÔÏóµÄÏ̷߳¢Éú×èÈûºó£¬¸Ã¶ÔÏóÈÔÄܹ»ÏìÓ¦ÆäËûÏ̡߳£
??Ϊ²»Í¬µÄÏß³ÌÄ£ÐͽøÐÐÉè¼Æ
??ÅжÏÊÇÇÀռʽ»¹ÊÇÐ×÷ʽµÄÏß³ÌÄ£ÐÍ£¬È¡¾öÓÚÐéÄâ»úµÄʵÏÖÕߣ¬²¢¸ù¾Ý¸÷ÖÖʵÏÖ¶ø²»Í¬¡£Òò´Ë£¬Java ¿ª·¢Ô±±ØÐë±àдÄÇЩÄܹ»ÔÚÁ½ÖÖÄ£ÐÍÉϹ¤×÷µÄ³ÌÐò¡£
??ÕýÈçÇ°ÃæËùÌáµ½µÄ£¬ÔÚÇÀռʽģÐÍÖÐÏ߳̿ÉÒÔÔÚ´úÂëµÄÈκÎÒ»¸ö²¿·ÖµÄÖм䱻´ò¶Ï£¬³ý·ÇÄÇÊÇÒ»¸öÔ×Ó²Ù×÷´úÂë¿é¡£Ô×Ó²Ù×÷´úÂë¿éÖеĴúÂë¶ÎÒ»µ©¿ªÊ¼Ö´ÐУ¬¾ÍÒªÔÚ¸ÃÏ̱߳»»»³ö´¦ÀíÆ÷֮ǰִÐÐÍê±Ï¡£ÔÚ Java ±à³ÌÖУ¬·ÖÅäÒ»¸öСÓÚ 32 λµÄ±äÁ¿¿Õ¼äÊÇÒ»ÖÖÔ×Ó²Ù×÷£¬¶ø´ËÍâÏó double ºÍ long ÕâÁ½¸ö 64 λÊý¾ÝÀàÐ͵ķÖÅä¾Í²»ÊÇÔ×ӵġ£Ê¹ÓÃËøÀ´Õýȷͬ²½¹²Ïí×ÊÔ´µÄ·ÃÎÊ£¬¾Í×ãÒÔ±£Ö¤Ò»¸ö¶àÏ̳߳ÌÐòÔÚÇÀռʽģÐÍÏÂÕýÈ·¹¤×÷¡£
??¶øÔÚÐ×÷ʽģÐÍÖУ¬ÊÇ·ñÄܱ£Ö¤Ïß³ÌÕý³£·ÅÆú´¦ÀíÆ÷£¬²»ÂÓ¶áÆäËûÏ̵߳ÄÖ´ÐÐʱ¼ä£¬ÔòÍêÈ«È¡¾öÓÚ³ÌÐòÔ±¡£µ÷Óà yield() ·½·¨Äܹ»½«µ±Ç°µÄÏ̴߳Ӵ¦ÀíÆ÷ÖÐÒƳöµ½×¼±¸¾ÍÐ÷¶ÓÁÐÖС£ÁíÒ»¸ö·½·¨ÔòÊǵ÷Óà sleep() ·½·¨£¬Ê¹Ï̷߳ÅÆú´¦ÀíÆ÷£¬²¢ÇÒÔÚ sleep ·½·¨ÖÐÖ¸¶¨µÄʱ¼ä¼ä¸ôÄÚ˯Ãß¡£
??ÕýÈçÄãËùÏëµÄÄÇÑù£¬½«ÕâЩ·½·¨ËæÒâ·ÅÔÚ´úÂëµÄij¸öµØ·½£¬²¢²»Äܹ»±£Ö¤Õý³£¹¤×÷¡£Èç¹ûÏß³ÌÕýÓµÓÐÒ»¸öËø£¨ÒòΪËüÔÚÒ»¸öͬ²½·½·¨»ò´úÂë¿éÖУ©£¬Ôòµ±Ëüµ÷Óà yield() ʱ²»Äܹ»ÊÍ·ÅÕâ¸öËø¡£Õâ¾ÍÒâζ׿´Ê¹Õâ¸öÏß³ÌÒѾ±»¹ÒÆ𣬵ȴýÕâ¸öËøÊͷŵÄÆäËûÏß³ÌÒÀÈ»²»ÄܼÌÐøÔËÐС£ÎªÁË»º½âÕâ¸öÎÊÌ⣬×îºÃ²»ÔÚͬ²½·½·¨Öе÷Óà yield ·½·¨¡£½«ÄÇЩÐèҪͬ²½µÄ´úÂë°üÔÚÒ»¸öͬ²½¿éÖУ¬ÀïÃæ²»º¬ÓзÇͬ²½µÄ·½·¨£¬²¢ÇÒÔÚÕâЩͬ²½´úÂë¿éÖ®Íâ²Åµ÷Óà yield¡£
??ÁíÍâÒ»¸ö½â¾ö·½·¨ÔòÊǵ÷Óà wait() ·½·¨£¬Ê¹´¦ÀíÆ÷·ÅÆúËüµ±Ç°ÓµÓеĶÔÏóµÄËø¡£Èç¹û¶ÔÏóÔÚ·½·¨¼¶±ðÉÏʹͬ²½µÄ£¬ÕâÖÖ·½·¨Äܹ»ºÜºÃµÄ¹¤×÷¡£ÒòΪËü½ö½öʹÓÃÁËÒ»¸öËø¡£Èç¹ûËüʹÓà fine-grained Ëø£¬Ôò wait() ½«ÎÞ·¨·ÅÆúÕâЩËø¡£´ËÍ⣬һ¸öÒòΪµ÷Óà wait() ·½·¨¶ø×èÈûµÄỊ̈߳¬Ö»Óе±ÆäËûÏ̵߳÷Óà notifyAll() ʱ²Å»á±»»½ÐÑ¡£
??Ï̺߳ÍAWT/Swing
??ÔÚÄÇЩʹÓà Swing ºÍ/»ò AWT °ü´´½¨ GUI £¨Óû§Í¼ÐνçÃ棩µÄ Java ³ÌÐòÖУ¬AWT ʼþ¾ä±úÔÚËü×Ô¼ºµÄÏß³ÌÖÐÔËÐС£¿ª·¢Ô±±ØÐë×¢Òâ±ÜÃ⽫ÕâЩ GUI Ïß³ÌÓë½ÏºÄʱ¼äµÄ¼ÆË㹤×÷°óÔÚÒ»Æð£¬ÒòΪÕâЩÏ̱߳ØÐ븺Ôð´¦ÀíÓû§Ê±¼ä²¢ÖØ»æÓû§Í¼ÐνçÃæ¡£»»¾ä»°À´Ëµ£¬Ò»µ© GUI Ï̴߳¦ÓÚ·±Ã¦£¬Õû¸ö³ÌÐò¿´ÆðÀ´¾ÍÏóÎÞÏìӦ״̬¡£Swing Ïß³Ìͨ¹ýµ÷ÓúÏÊÊ·½·¨£¬Í¨ÖªÄÇЩ Swing callback £¨ÀýÈç Mouse Listener ºÍ Action Listener £©¡£ ÕâÖÖ·½·¨Òâζ×Å listener ÎÞÂÛÒª×ö¶àÉÙÊÂÇ飬¶¼Ó¦µ±ÀûÓà listener callback ·½·¨²úÉúÆäËûÏß³ÌÀ´Íê³É´ËÏ×÷¡£Ä¿µÄ±ãÔÚÓÚÈà listener callback ¸ü¿ìËÙ·µ»Ø£¬´Ó¶øÔÊÐí Swing Ïß³ÌÏìÓ¦ÆäËûʼþ¡£
??Èç¹ûÒ»¸ö Swing Ï̲߳»Äܹ»Í¬²½ÔËÐС¢ÏìӦʼþ²¢ÖØ»æÊä³ö£¬ÄÇÔõôÄܹ»ÈÃÆäËûµÄḬ̈߳²È«µØÐÞ¸Ä Swing µÄ״̬£¿ÕýÈçÉÏÃæÌáµ½µÄ£¬Swing callback ÔÚ Swing Ïß³ÌÖÐÔËÐС£Òò´ËËûÃÇÄÜÐÞ¸Ä Swing Êý¾Ý²¢»æµ½ÆÁÄ»ÉÏ¡£
??µ«ÊÇÈç¹û²»ÊÇ Swing callback ²úÉúµÄ±ä»¯¸ÃÔõô°ìÄØ£¿Ê¹ÓÃÒ»¸ö·Ç Swing Ïß³ÌÀ´ÐÞ¸Ä Swing Êý¾ÝÊDz»°²È«µÄ¡£Swing ÌṩÁËÁ½¸ö·½·¨À´½â¾öÕâ¸öÎÊÌ⣺invokeLater() ºÍ invokeAndWait()¡£ÎªÁËÐÞ¸Ä Swing ״̬£¬Ö»Òª¼òµ¥µØµ÷ÓÃÆäÖÐÒ»¸ö·½·¨£¬Èà Runnable µÄ¶ÔÏóÀ´×öÕâЩ¹¤×÷¡£ÒòΪ Runnable ¶ÔÏóͨ³£¾ÍÊÇËüÃÇ×ÔÉíµÄỊ̈߳¬Äã¿ÉÄÜ»áÈÏΪÕâЩ¶ÔÏó»á×÷ΪÏß³ÌÀ´Ö´ÐС£µ«ÄÇÑù×öÆäʵҲÊDz»°²È«µÄ¡£ÊÂʵÉÏ£¬Swing »á½«ÕâЩ¶ÔÏó·Åµ½¶ÓÁÐÖУ¬²¢ÔÚ½«À´Ä³¸öʱ¿ÌÖ´ÐÐËüµÄ run ·½·¨¡£ÕâÑù²ÅÄܹ»°²È«ÐÞ¸Ä Swing ״̬¡£
??×ܽá
??Java ÓïÑÔµÄÉè¼Æ£¬Ê¹µÃ¶àÏ̶߳Լ¸ºõËùÓÐµÄ Applet ¶¼ÊDZØÒªµÄ¡£ÌرðÊÇ£¬IO ºÍ GUI ±à³Ì¶¼ÐèÒª¶àÏß³ÌÀ´ÎªÓû§ÌṩÍêÃÀµÄÌåÑé¡£Èç¹ûÒÀÕÕ±¾ÎÄËùÌáµ½µÄÈô¸É»ù±¾¹æÔò£¬²¢ÔÚ¿ªÊ¼±à³ÌÇ°×ÐϸÉè¼Æϵͳ----°üÀ¨Ëü¶Ô¹²Ïí×ÊÔ´µÄ·ÃÎʵȣ¬Äã¾Í¿ÉÒÔ±ÜÃâÐí¶à³£¼ûºÍÄÑÒÔ·¢¾õµÄÏß³ÌÏÝÚå¡£
dangzhiming ÓÚ 2006-11-15 21:27:30·¢±í:
Â¥Ö÷ÐÁ¿àÁË