¿ìËÙÒµÎñͨµÀ

¶ÔJava¶àÏ̼߳¼ÊõÖÐËùÓз½·¨µÄÏêϸ½âÎö - ±à³ÌÈëÃÅÍø

×÷Õß ØýÃû¼¼Êõ À´Ô´ NET±à³Ì ä¯ÀÀ ·¢²¼Ê±¼ä 2012-06-23

¶ÔJava¶àÏ̼߳¼ÊõÖÐËùÓз½·¨µÄÏêϸ½âÎö

ʱ¼ä:2010-12-09

Ò»¡¢run()ºÍstart()

ÕâÁ½¸ö·½·¨Ó¦¸Ã¶¼±È½ÏÊìϤ£¬°ÑÐèÒª²¢Ðд¦ÀíµÄ´úÂë·ÅÔÚrun()·½·¨ÖУ¬start()·½·¨Æô¶¯Ï߳̽«×Ô¶¯µ÷Óà run()·½·¨£¬ÕâÊÇÓÉJavaµÄÄÚ´æ»úÖƹ涨µÄ¡£²¢ÇÒrun()·½·¨±ØÐëÊÇpublic·ÃÎÊȨÏÞ£¬·µ»ØÖµÀàÐÍΪvoid¡£

¶þ¡¢¹Ø¼ü×ÖSynchronized

Õâ¸ö¹Ø¼ü×ÖÓÃÓÚ±£»¤¹²ÏíÊý¾Ý£¬µ±È»Ç°ÌáÊÇÒª·ÖÇåÄÄЩÊý¾ÝÊǹ²ÏíÊý¾Ý¡£Ã¿¸ö¶ÔÏó¶¼ÓÐÒ»¸öËø±êÖ¾£¬µ±Ò»¸öÏ̷߳ÃÎʸöÔÏóʱ£¬±»SynchronizedÐÞÊεÄÊý¾Ý½«±»“ÉÏËø”£¬×èÖ¹ÆäËûÏ̷߳ÃÎÊ¡£µ±Ç°Ï̷߳ÃÎÊÍêÕⲿ·ÖÊý¾ÝºóÊÍ·ÅËø±êÖ¾£¬ÆäËûÏ߳̾ͿÉÒÔ·ÃÎÊÁË¡£

¡¡public ThreadTest implements Runnable ¡¡¡¡{ ¡¡¡¡public synchronized void run(){ ¡¡¡¡for(int i=0;i<10;i++) ¡¡¡¡{ ¡¡¡¡System.out.println(" " + i); ¡¡¡¡} ¡¡¡¡} ¡¡¡¡public static void main(String[] args) ¡¡¡¡{ ¡¡¡¡Runnable r1 = new ThreadTest(); ¡¡¡¡Runnable r2 = new ThreadTest(); ¡¡¡¡Thread¡¡t1 = new Thread(r1); ¡¡¡¡Thread t2 = new Thread(r2); ¡¡¡¡t1.start(); ¡¡¡¡t2.start(); ¡¡¡¡} ¡¡¡¡}

ÒÔÉÏÕâ¶Î³ÌÐòÖÐµÄ 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»¿É·Ö¸îµÄÒ»²¿·Ö,Èç¹ûÄúҪתÔر¾ÎÄÕÂ,Çë±£ÁôÒÔÉÏÐÅÏ¢£¬Ð»Ð»!

·ÖÏíµ½£º ¸ü¶à

Copyright ©1999-2011 ÏÃÃÅÁèÖڿƼ¼ÓÐÏÞ¹«Ë¾ ÏÃÃÅÓÅͨ»¥Áª¿Æ¼¼¿ª·¢ÓÐÏÞ¹«Ë¾ All rights reserved

µØÖ·(ADD)£ºÏÃÃÅÈí¼þÔ°¶þÆÚÍûº£Â·63ºÅ701E£¨¶«ÄÏÈÚͨÅÔ£© Óʱà(ZIP)£º361008

µç»°£º0592-5908028 ´«Õ棺0592-5908039 ×ÉѯÐÅÏ䣺web@lingzhong.cn ×ÉѯOICQ£º173723134

¡¶ÖлªÈËÃñ¹²ºÍ¹úÔöÖµµçÐÅÒµÎñ¾­ÓªÐí¿ÉÖ¤¡·ÃöB2-20100024  ICP±¸°¸:ÃöICP±¸05037997ºÅ