ºìÁªLinuxÃÅ»§
Linux°ïÖú

±àд¶àÏ̵߳ÄJavaÓ¦ÓóÌÐò-ÈçºÎ±ÜÃ⵱ǰ±à³ÌÖÐ×î³£¼ûµÄÎÊÌ⣨1£©

·¢²¼Ê±¼ä:2006-05-03 19:27:47À´Ô´:ºìÁª×÷Õß:ÌìɽÀÏͯ
¼¸ºõËùÓÐʹÓà 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 ·½·¨Äܹ»»ñµÃµÄ¶ÔÏó¼¶Ëø¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ