¶ÔJava¶àÏ̼߳¼ÊõÖÐËùÓз½·¨µÄÏêϸ½âÎö - ±à³ÌÈëÃÅÍø
¶ÔJava¶àÏ̼߳¼ÊõÖÐËùÓз½·¨µÄÏêϸ½âÎöʱ¼ä:2010-12-09Ò»¡¢run()ºÍstart() ÕâÁ½¸ö·½·¨Ó¦¸Ã¶¼±È½ÏÊìϤ£¬°ÑÐèÒª²¢Ðд¦ÀíµÄ´úÂë·ÅÔÚrun()·½·¨ÖУ¬start()·½·¨Æô¶¯Ï߳̽«×Ô¶¯µ÷Óà run()·½·¨£¬ÕâÊÇÓÉJavaµÄÄÚ´æ»úÖƹ涨µÄ¡£²¢ÇÒrun()·½·¨±ØÐëÊÇpublic·ÃÎÊȨÏÞ£¬·µ»ØÖµÀàÐÍΪvoid¡£ ¶þ¡¢¹Ø¼ü×ÖSynchronized Õâ¸ö¹Ø¼ü×ÖÓÃÓÚ±£»¤¹²ÏíÊý¾Ý£¬µ±È»Ç°ÌáÊÇÒª·ÖÇåÄÄЩÊý¾ÝÊǹ²ÏíÊý¾Ý¡£Ã¿¸ö¶ÔÏó¶¼ÓÐÒ»¸öËø±êÖ¾£¬µ±Ò»¸öÏ̷߳ÃÎʸöÔÏóʱ£¬±»SynchronizedÐÞÊεÄÊý¾Ý½«±»“ÉÏËø”£¬×èÖ¹ÆäËûÏ̷߳ÃÎÊ¡£µ±Ç°Ï̷߳ÃÎÊÍêÕⲿ·ÖÊý¾ÝºóÊÍ·ÅËø±êÖ¾£¬ÆäËûÏ߳̾ͿÉÒÔ·ÃÎÊÁË¡£
ÒÔÉÏÕâ¶Î³ÌÐòÖÐµÄ i ±äÁ¿²¢²»Êǹ²ÏíÊý¾Ý£¬Ò²¾ÍÊÇÕâÀïµÄSynchronized¹Ø¼ü×Ö²¢Î´Æð×÷Óá£ÒòΪt1,t2Á½¸öÏß³ÌÊÇÁ½¸ö¶ÔÏó(r1,r2)µÄÏ̡߳£²»Í¬µÄ¶ÔÏóÆäÊý¾ÝÊDz»Í¬µÄ£¬ËùÒÔr1ºÍr2Á½¸ö¶ÔÏóµ**±äÁ¿ÊDz¢²»Êǹ²ÏíÊý¾Ý¡£ µ±°Ñ´úÂë¸Ä³ÉÈçÏ£ºSynchronized¹Ø¼ü×ֲŻáÆð×÷Óà Runnable r = new ThreadTest(); Thread t1 = new Thread(r); Thread t2 = new Thread(r); t1.start(); t2.start(); ¶ÔJava¶àÏ̼߳¼ÊõÖÐËùÓз½·¨µÄÏêϸ½âÎö(2)ʱ¼ä:2010-12-09Èý¡¢sleep() ʹµ±Ç°Ïß³Ì(¼´µ÷Óø÷½·¨µÄÏß³Ì)ÔÝÍ£Ö´ÐÐÒ»¶Îʱ¼ä£¬ÈÃÆäËûÏß³ÌÓлú»á¼ÌÐøÖ´ÐУ¬µ«Ëü²¢²»ÊͷŶÔÏóËø¡£Ò²¾ÍÊÇÈç¹ûÓÐSynchronizedͬ²½¿é£¬ÆäËûÏß³ÌÈÔÈ»²»Í¬·ÃÎʹ²ÏíÊý¾Ý¡£×¢Òâ¸Ã·½·¨Òª²¶»ñÒì³£ ±ÈÈçÓÐÁ½¸öÏß³ÌͬʱִÐÐ(ûÓÐSynchronized)£¬Ò»¸öÏß³ÌÓÅÏȼ¶ÎªMAX_PRIORITY£¬ÁíÒ»¸öΪMIN_PRIORITY£¬Èç¹ûûÓÐSleep()·½·¨£¬Ö»ÓиßÓÅÏȼ¶µÄÏß³ÌÖ´ÐÐÍê³Éºó£¬µÍÓÅÏȼ¶µÄÏ̲߳ÅÄÜÖ´ÐУ»µ«µ±¸ßÓÅÏȼ¶µÄÏß³Ìsleep(5000)ºó£¬µÍÓÅÏȼ¶¾ÍÓлú»áÖ´ÐÐÁË¡£ ×ÜÖ®£¬sleep()¿ÉÒÔʹµÍÓÅÏȼ¶µÄÏ̵߳õ½Ö´ÐеĻú»á£¬µ±È»Ò²¿ÉÒÔÈÃͬÓÅÏȼ¶¡¢¸ßÓÅÏȼ¶µÄÏß³ÌÓÐÖ´ÐеĻú»á¡£ ËÄ¡¢join() join()·½·¨Ê¹µ÷Óø÷½·¨µÄÏß³ÌÔÚ´Ë֮ǰִÐÐÍê±Ï£¬Ò²¾ÍÊǵȴýµ÷Óø÷½·¨µÄÏß³ÌÖ´ÐÐÍê±ÏºóÔÙÍùϼÌÐøÖ´ÐС£×¢Òâ¸Ã·½·¨Ò²Òª²¶»ñÒì³£¡£ Îå¡¢yield() ËüÓësleep()ÀàËÆ£¬Ö»ÊDz»ÄÜÓÉÓû§Ö¸¶¨ÔÝÍ£¶à³¤Ê±¼ä£¬²¢ÇÒyield()·½·¨Ö»ÄÜÈÃͬÓÅÏȼ¶µÄÏß³ÌÓÐÖ´ÐеĻú»á¡£ Áù¡¢wait()ºÍnotify()¡¢notifyAll() ÕâÈý¸ö·½·¨ÓÃÓÚе÷¶à¸öÏ̶߳Թ²ÏíÊý¾ÝµÄ´æÈ¡£¬ËùÒÔ±ØÐëÔÚSynchronizedÓï¾ä¿éÄÚʹÓÃÕâÈý¸ö·½·¨¡£Ç°Ãæ˵¹ýSynchronizedÕâ¸ö¹Ø¼ü×ÖÓÃÓÚ±£»¤¹²ÏíÊý¾Ý£¬×èÖ¹ÆäËûÏ̶߳Թ²ÏíÊý¾ÝµÄ´æÈ¡¡£µ«ÊÇÕâÑù³ÌÐòµÄÁ÷³Ì¾ÍºÜ²»Áé»îÁË£¬ÈçºÎ²ÅÄÜÔÚµ±Ç°Ï̻߳¹Ã»Í˳öSynchronizedÊý¾Ý¿éʱÈÃÆäËûÏß³ÌÒ²Óлú»á·ÃÎʹ²ÏíÊý¾ÝÄØ£¿´Ëʱ¾ÍÓÃÕâÈý¸ö·½·¨À´Áé»î¿ØÖÆ¡£ wait()·½·¨Ê¹µ±Ç°Ïß³ÌÔÝÍ£Ö´Ðв¢ÊͷŶÔÏóËø±êÖ¾£¬ÈÃÆäËûÏ߳̿ÉÒÔ½øÈëSynchronizedÊý¾Ý¿é£¬µ±Ç°Ï̱߳»·ÅÈë¶ÔÏóµÈ´ý³ØÖС£µ±µ÷Óà notify()·½·¨ºó£¬½«´Ó¶ÔÏóµÄµÈ´ý³ØÖÐÒÆ×ßÒ»¸öÈÎÒâµÄÏ̲߳¢·Åµ½Ëø±êÖ¾µÈ´ý³ØÖУ¬Ö»ÓÐ Ëø±êÖ¾µÈ´ý³ØÖеÄÏß³ÌÄܹ»»ñÈ¡Ëø±êÖ¾£»Èç¹ûËø±êÖ¾µÈ´ý³ØÖÐûÓÐỊ̈߳¬Ôònotify()²»Æð×÷ÓᣠnotifyAll()Ôò´Ó¶ÔÏóµÈ´ý³ØÖÐÒÆ×ßËùÓеȴýÄǸö¶ÔÏóµÄÏ̲߳¢·Åµ½Ëø±êÖ¾µÈ´ý³ØÖС£ ×¢Òâ ÕâÈý¸ö·½·¨¶¼ÊÇjava.lang.OjbectµÄ·½·¨! ÎÄÕÂÀ´Ô´:http://java.ccidnet.com/art/3539/20070529/1093639_1.html |
ÁèÖڿƼ¼×¨ÒµÌṩ·þÎñÆ÷×âÓᢷþÎñÆ÷Íйܡ¢ÆóÒµÓʾ֡¢ÐéÄâÖ÷»úµÈ·þÎñ£¬¹«Ë¾ÍøÕ¾£ºhttp://www.lingzhong.cn ΪÁ˸ø¹ã´ó¿Í»§Á˽â¸ü¶àµÄ¼¼ÊõÐÅÏ¢£¬±¾¼¼ÊõÎÄÕÂÊÕ¼¯À´Ô´ÓÚÍøÂç,ÁèÖڿƼ¼×ðÖØÎÄÕÂ×÷ÕߵİæȨ£¬Èç¹ûÓÐÉæ¼°ÄãµÄ°æȨÓбØҪɾ³ýÄãµÄÎÄÕ£¬ÇëºÍÎÒÃÇÁªÏµ¡£ÒÔÉÏÐÅÏ¢ÓëÎÄÕÂÕýÎÄÊDz»¿É·Ö¸îµÄÒ»²¿·Ö,Èç¹ûÄúҪתÔر¾ÎÄÕÂ,Çë±£ÁôÒÔÉÏÐÅÏ¢£¬Ð»Ð»! |
- ʹÓÃEclipse Ganymede½øÐÐ×ÀÃæ¡¢WebºÍÒƶ¯É豸¿ª·¢£¬µÚ2²¿·Ö£¨¶þ£© - ±à³ÌÈëÃÅÍø
- Photoshop»ù´¡¹¤¾ßÖÆ×÷ÖʸÐÀ¶****Ò³°´Å¥
- dz̸¹Ç¸É½»»»»úºÍ×ÀÃæ½»»»»úµÄÇø±ð
- °ÑÈ«½ÇÊý×ÖתΪ°ë½ÇÊý×Ö
- ʹÓÃÓÃFlashÖÆ×÷дÒâ»-Öñ
- JSʵÀý½Ì³Ì:ÓÃ6N¡À1·¨ÇóËØÊý
- C++±àÒëÆ÷ÈçºÎʵÏÖÒì³£´¦Àí
- CSS½Ì³Ì,ÈÃÍøÒ³¶ÔËÑË÷ÒýÇæ¸üÓѺÃ
- VBS½Ì³Ì£ºÊôÐÔ-ShareName ÊôÐÔ
- PSͼƬ½Ì³Ì:Ïļ¾ÕÕƬ±äº®ÀäÑ©¾°