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

JavaÏß³Ì×ܽá

·¢²¼Ê±¼ä:2006-05-01 12:28:09À´Ô´:ºìÁª×÷Õß:ÏèÏè
×÷ÕßµÄblog:(http://blog.matrix.org.cn/page/Kaizen)

ÔÚÂÛ̳ÉÏÃæ³£³£¿´µ½³õѧÕ߶ÔÏ̵߳ÄÎÞ¿ÉÄκΣ¬ËùÒÔ×ܽá³öÁËÏÂÃæһƪÎÄÕ£¬Ï£Íû¶ÔһЩÕýÔÚѧϰʹÓÃjavaÏ̵߳ijõѧÕßÓÐËù°ïÖú¡£

Ê×ÏÈÒªÀí½âÏß³ÌÊ×ÏÈÐèÒªÁ˽âһЩ»ù±¾µÄ¶«Î÷£¬ÎÒÃÇÏÖÔÚËùʹÓõĴó¶àÊý²Ù×÷ϵͳ¶¼ÊôÓÚ¶àÈÎÎñ£¬·Öʱ²Ù×÷ϵͳ¡£ÕýÊÇÓÉÓÚÕâÖÖ²Ù×÷ϵͳµÄ³öÏÖ²ÅÓÐÁ˶àÏß³ÌÕâ¸ö¸ÅÄî¡£ÎÒÃÇʹÓõÄwindows,linux¾ÍÊôÓÚ´ËÁС£Ê²Ã´ÊÇ·Öʱ²Ù×÷ϵͳÄØ£¬Í¨Ë×Ò»µãÓë¾ÍÊÇ¿ÉÒÔͬһʱ¼äÖ´Ðжà¸ö³ÌÐòµÄ²Ù×÷ϵͳ£¬ÔÚ×Ô¼ºµÄµçÄÔÉÏÃ棬ÄãÊDz»ÊÇÒ»±ßÌý¸è£¬Ò»±ßÁÄÌ컹һ±ß¿´ÍøÒ³ÄØ£¿µ«Êµ¼ÊÉÏ£¬²¢²»ÉÏcpuÔÚͬʱִÐÐÕâЩ³ÌÐò£¬cpuÖ»Êǽ«Ê±¼äÇиîΪʱ¼äƬ£¬È»ºó½«Ê±¼äƬ·ÖÅä¸øÕâЩ³ÌÐò£¬»ñµÃʱ¼äƬµÄ³ÌÐò¿ªÊ¼Ö´ÐУ¬²»µÈÖ´ÐÐÍê±Ï£¬Ï¸ö³ÌÐòÓÖ»ñµÃʱ¼äƬ¿ªÊ¼Ö´ÐУ¬ÕâÑù¶à¸ö³ÌÐòÂÖÁ÷Ö´ÐÐÒ»¶Îʱ¼ä£¬ÓÉÓÚÏÖÔÚcpuµÄ¸ßËÙ¼ÆËãÄÜÁ¦£¬¸øÈ˵ĸоõ¾ÍÏñÊǶà¸ö³ÌÐòÔÚͬʱִÐÐÒ»Ñù¡£
Ò»°ã¿ÉÒÔÔÚͬһʱ¼äÄÚÖ´Ðжà¸ö³ÌÐòµÄ²Ù×÷ϵͳ¶¼Óнø³ÌµÄ¸ÅÄî.Ò»¸ö½ø³Ì¾ÍÊÇÒ»¸öÖ´ÐÐÖеijÌÐò,¶øÿһ¸ö½ø³Ì¶¼ÓÐ×Ô¼º¶ÀÁ¢µÄÒ»¿éÄÚ´æ¿Õ¼ä,Ò»×éϵͳ×ÊÔ´.ÔÚ½ø³Ì¸ÅÄîÖÐ,ÿһ¸ö½ø³ÌµÄÄÚ²¿Êý¾ÝºÍ״̬¶¼ÊÇÍêÈ«¶ÀÁ¢µÄ.Òò´Ë¿ÉÒÔÏëÏñ´´½¨²¢Ö´ÐÐÒ»¸ö½ø³ÌµÄϵͳ¿ªÏñÊDZȽϴóµÄ£¬ËùÒÔÏ̳߳öÏÖÁË¡£ÔÚjavaÖУ¬³ÌÐòͨ¹ýÁ÷¿ØÖÆÀ´Ö´ÐгÌÐòÁ÷,³ÌÐòÖе¥¸ö˳ÐòµÄÁ÷¿ØÖƳÆΪÏß³Ì,¶àÏß³ÌÔòÖ¸µÄÊÇÔÚµ¥¸ö³ÌÐòÖпÉÒÔͬʱÔËÐжà¸ö²»Í¬µÄÏß³Ì,Ö´Ðв»Í¬µÄÈÎÎñ.¶àÏß³ÌÒâζ×ÅÒ»¸ö³ÌÐòµÄ¶àÐÐÓï¾ä¿ÉÒÔ¿´ÉÏÈ¥¼¸ºõÔÚͬһʱ¼äÄÚͬʱÔËÐÐ.£¨Äã¿ÉÒÔ½«Ç°ÃæÒ»¾ä»°µÄ³ÌÐò»»³É½ø³Ì£¬½ø³ÌÊdzÌÐòµÄÒ»´ÎÖ´Ðйý³Ì,ÊÇϵͳÔËÐгÌÐòµÄ»ù±¾µ¥Î»£©

Ïß³ÌÓë½ø³ÌÏàËÆ,ÊÇÒ»¶ÎÍê³Éij¸öÌض¨¹¦ÄܵĴúÂë,ÊdzÌÐòÖе¥¸ö˳ÐòµÄÁ÷¿ØÖÆ;µ«Óë½ø³Ì²»Í¬µÄÊÇ,ͬÀàµÄ¶à¸öÏß³ÌÊǹ²ÏíÒ»¿éÄÚ´æ¿Õ¼äºÍÒ»×éϵͳ×ÊÔ´,¶øÏ̱߳¾ÉíµÄÊý¾Ýͨ³£Ö»ÓÐ΢´¦ÀíÆ÷µÄ¼Ä´æÆ÷Êý¾Ý,ÒÔ¼°Ò»¸ö¹©³ÌÐòÖ´ÐÐʱʹÓõĶÑÕ».ËùÒÔϵͳÔÚ²úÉúÒ»¸öÏß³Ì,»òÕßÔÚ¸÷¸öÏß³ÌÖ®¼äÇл»Ê±,¸ºµ£Òª±È½ø³ÌСµÄ¶à,ÕýÒòÈç´Ë,Ïß³ÌÒ²±»³ÆΪÇḺºÉ½ø³Ì(light-weight process).Ò»¸ö½ø³ÌÖпÉÒÔ°üº¬¶à¸öÏß³Ì.

¶àÈÎÎñÊÇÖ¸ÔÚÒ»¸öϵͳÖпÉÒÔͬʱÔËÐжà¸ö³ÌÐò,¼´Óжà¸ö¶ÀÁ¢ÔËÐеÄÈÎÎñ,ÿ¸öÈÎÎñ¶ÔÓ¦Ò»¸ö½ø³Ì£¬Í¬½ø³ÌÒ»Ñù,Ò»¸öÏß³ÌÒ²ÓдӴ´½¨,ÔËÐе½ÏûÍöµÄ¹ý³Ì,³ÆΪÏ̵߳ÄÉúÃüÖÜÆÚ.ÓÃÏ̵߳Ä״̬(state)±íÃ÷Ï̴߳¦ÔÚÉúÃüÖÜÆÚµÄÄĸö½×¶Î.Ïß³ÌÓд´½¨,¿ÉÔËÐÐ,ÔËÐÐÖÐ,×èÈû,ËÀÍöÎåÖÐ״̬.ͨ¹ýÏ̵߳ĿØÖÆÓëµ÷¶È¿ÉʹÏß³ÌÔÚÕ⼸ÖÖ״̬¼äת»¯Ã¿¸ö³ÌÐòÖÁÉÙ×Ô¶¯ÓµÓÐÒ»¸öÏß³Ì,³ÆΪÖ÷Ïß³Ì.µ±³ÌÐò¼ÓÔص½ÄÚ´æʱ,Æô¶¯Ö÷Ïß³Ì.

[Ï̵߳ÄÔËÐлúÖÆÒÔ¼°µ÷¶ÈÄ£ÐÍ]
java ÖжàÏ߳̾ÍÊÇÒ»¸öÀà»òÒ»¸ö³ÌÐòÖ´Ðлò¹ÜÀí¶à¸öÏß³ÌÖ´ÐÐÈÎÎñµÄÄÜÁ¦£¬Ã¿¸öÏ߳̿ÉÒÔ¶ÀÁ¢ÓÚÆäËûÏ̶߳ø¶ÀÁ¢ÔËÐУ¬µ±È»Ò²¿ÉÒÔºÍÆäËûÏß³ÌЭͬÔËÐУ¬Ò»¸öÀà¿ØÖÆ×ÅËüµÄËùÓÐỊ̈߳¬¿ÉÒÔ¾ö¶¨ÄĸöÏ̵߳õ½ÓÅÏȼ¶£¬ÄĸöÏ߳̿ÉÒÔ·ÃÎÊÆäËûÀàµÄ×ÊÔ´£¬ÄĸöÏ߳̿ªÊ¼Ö´ÐУ¬Äĸö±£³ÖÐÝÃß״̬¡£
ÏÂÃæÊÇÏ̵߳ĻúÖÆͼ£º




Ï̵߳Ä״̬±íʾÏß³ÌÕýÔÚ½øÐеĻÒÔ¼°ÔÚ´Ëʱ¼ä¶ÎÄÚËùÄÜÍê³ÉµÄÈÎÎñ.Ïß³ÌÓд´½¨,¿ÉÔËÐÐ,ÔËÐÐÖÐ,×èÈû,ËÀÍöÎåÖÐ״̬.Ò»¸ö¾ßÓÐÉúÃüµÄÏß³Ì,×ÜÊÇ´¦ÓÚÕâÎåÖÖ״̬֮һ£º
1.´´½¨×´Ì¬
ʹÓÃnewÔËËã·û´´½¨Ò»¸öÏ̺߳ó,¸ÃÏ߳̽ö½öÊÇÒ»¸ö¿Õ¶ÔÏó,ϵͳûÓзÖÅä×ÊÔ´,³Æ¸ÃÏ̴߳¦ÓÚ´´½¨×´Ì¬(new thread)
2.¿ÉÔËÐÐ״̬
ʹÓÃstart()·½·¨Æô¶¯Ò»¸öÏ̺߳ó,ϵͳΪ¸ÃÏ̷߳ÖÅäÁ˳ýCPUÍâµÄËùÐè×ÊÔ´,ʹ¸ÃÏ̴߳¦ÓÚ¿ÉÔËÐÐ״̬(Runnable)
3.ÔËÐÐÖÐ״̬
JavaÔËÐÐϵͳͨ¹ýµ÷¶ÈÑ¡ÖÐÒ»¸öRunnableµÄÏß³Ì,ʹÆäÕ¼ÓÐCPU²¢×ªÎªÔËÐÐÖÐ״̬(Running).´Ëʱ,ϵͳÕæÕýÖ´ÐÐÏ̵߳Ärun()·½·¨.
4.×èÈû״̬
Ò»¸öÕýÔÚÔËÐеÄÏß³ÌÒòijÖÖÔ­Òò²»ÄܼÌÐøÔËÐÐʱ,½øÈë×èÈû״̬(Blocked)
5.ËÀÍö״̬
Ï߳̽áÊøºóÊÇËÀÍö״̬(Dead)

ͬһʱ¿ÌÈç¹ûÓжà¸öÏ̴߳¦ÓÚ¿ÉÔËÐÐ״̬,ÔòËûÃÇÐèÒªÅŶӵȴýCPU×ÊÔ´.´Ëʱÿ¸öÏß³Ì×Ô¶¯»ñµÃÒ»¸öÏ̵߳ÄÓÅÏȼ¶(priority),ÓÅÏȼ¶µÄ¸ßµÍ·´Ó³Ï̵߳ÄÖØÒª»ò½ô¼±³Ì¶È.¿ÉÔËÐÐ״̬µÄḬ̈߳´ÓÅÏȼ¶ÅŶÓ,Ï̵߳÷¶ÈÒÀ¾ÝÓÅÏȼ¶»ù´¡ÉϵÄ"Ïȵ½ÏÈ·þÎñ"Ô­Ôò.
Ï̵߳÷¶È¹ÜÀíÆ÷¸ºÔðÏß³ÌÅŶӺÍCPUÔÚÏ̼߳äµÄ·ÖÅä,²¢ÓÉÏ̵߳÷¶ÈËã·¨½øÐе÷¶È.µ±Ï̵߳÷¶È¹ÜÀíÆ÷Ñ¡ÖÖij¸öÏß³Ìʱ,¸ÃÏ̻߳ñµÃCPU×ÊÔ´¶ø½øÈëÔËÐÐ״̬.

Ï̵߳÷¶ÈÊÇÏÈռʽµ÷¶È,¼´Èç¹ûÔÚµ±Ç°Ïß³ÌÖ´Ðйý³ÌÖÐÒ»¸ö¸ü¸ßÓÅÏȼ¶µÄÏ߳̽øÈë¿ÉÔËÐÐ״̬,ÔòÕâ¸öÏß³ÌÁ¢¼´±»µ÷¶ÈÖ´ÐÐ.ÏÈռʽµ÷¶È·ÖΪ:¶ÀռʽºÍ·Öʱ·½Ê½.
¶ÀÕ¼·½Ê½ÏÂ,µ±Ç°Ö´ÐÐÏ߳̽«Ò»Ö±Ö´ÐÐÏÂÈ¥,Ö± µ½Ö´ÐÐÍê±Ï»òÓÉÓÚijÖÖÔ­ÒòÖ÷¶¯·ÅÆúCPU,»òCPU±»Ò»¸ö¸ü¸ßÓÅÏȼ¶µÄÏß³ÌÇÀÕ¼
·Öʱ·½Ê½ÏÂ,µ±Ç°ÔËÐÐÏ̻߳ñµÃÒ»¸öʱ¼äƬ,ʱ¼äµ½Ê±,¼´Ê¹Ã»ÓÐÖ´ÐÐÍêÒ²ÒªÈóöCPU,½øÈë¿ÉÔËÐÐ״̬,µÈ´ýÏÂÒ»¸öʱ¼äƬµÄµ÷¶È.ϵͳѡÖÐÆäËû¿ÉÔËÐÐ״̬µÄÏß³ÌÖ´ÐÐ
·Öʱ·½Ê½µÄϵͳʹÿ¸öÏ̹߳¤×÷Èô¸É²½,ʵÏÖ¶àÏß³ÌͬʱÔËÐÐ

ÁíÍâÇë×¢ÒâÏÂÃæµÄÏ̵߳÷¶È¹æÔò£¨Èç¹ûÓв»Àí½â£¬²»¼±£¬ÍùÏ¿´£©£º
¢ÙÈç¹ûÁ½¸ö»òÊÇÁ½¸öÒÔÉϵÄÏ̶߳¼ÐÞ¸ÄÒ»¸ö¶ÔÏó£¬ÄÇô°ÑÖ´ÐÐÐ޸ĵķ½·¨¶¨ÒåΪ±»Í¬²½µÄ£¨Synchronized£©,Èç¹û¶ÔÏó¸üÐÂÓ°Ïìµ½Ö»¶Á·½·¨£¬ÄÇôֻ¶È·½·¨Ò²Ó¦¸Ã¶¨ÒåΪͬ²½µÄ
¢ÚÈç¹ûÒ»¸öÏ̱߳ØÐëµÈ´ýÒ»¸ö¶ÔÏó״̬·¢Éú±ä»¯£¬ÄÇôËüÓ¦¸ÃÔÚ¶ÔÏóÄÚ²¿µÈ´ý£¬¶ø²»ÊÇÔÚÍⲿµÈ´ý£¬Ëü¿ÉÒÔµ÷ÓÃÒ»¸ö±»Í¬²½µÄ·½·¨£¬²¢ÈÃÕâ¸ö·½·¨µ÷ÓÃwait()
¢Ûÿµ±Ò»¸ö·½·¨¸Ä±äij¸ö¶ÔÏóµÄ״̬µÄʱºò£¬ËüÓ¦¸Ãµ÷ÓÃnotifyAll()·½·¨£¬Õâ¸øµÈ´ý¶ÓÁеÄÏß³ÌÌṩ»ú»áÀ´¿´Ò»¿´Ö´Ðл·¾³ÊÇ·ñÒÑ·¢Éú¸Ä±ä
¢Ü ¼Çסwait(),notify(),notifyAll()·½·¨ÊôÓÚObjectÀ࣬¶ø²»ÊÇThreadÀ࣬×Ðϸ¼ì²é¿´ÊÇ·ñÿ´ÎÖ´ÐÐwait()·½·¨¶¼ÓÐÏàÓ¦µÄnotify()»ònotifyAll()·½·¨£¬ÇÒËüÃÇ×÷ÓÃÓëÏàͬµÄ¶ÔÏóÔÚjavaÖÐÿ¸öÀ඼ÓÐÒ»¸öÖ÷Ị̈߳¬ÒªÖ´ÐÐÒ»¸ö³ÌÐò£¬ÄÇôÕâ¸öÀ൱ÖÐÒ»¶¨ÒªÓÐmain·½·¨£¬Õâ¸öman·½·¨Ò²¾ÍÊÇjava classÖеÄÖ÷Ï̡߳£Äã¿ÉÒÔ×Ô¼º´´½¨Ị̈߳¬ÓÐÁ½ÖÖ·½·¨£¬Ò»ÊǼ̳ÐThreadÀ࣬»òÊÇʵÏÖRunnable½Ó¿Ú¡£Ò»°ãÇé¿öÏ£¬×îºÃ±ÜÃâ¼Ì³Ð£¬ÒòΪ javaÖÐÊǵ¥¸ù¼Ì³Ð£¬Èç¹ûÄãÑ¡Óü̳У¬ÄÇôÄãµÄÀà¾ÍʧȥÁ˵¯ÐÔ£¬µ±È»Ò²²»ÄÜÈ«È»·ñ¶¨¼Ì³ÐThread,¸Ã·½·¨±àд¼òµ¥,¿ÉÒÔÖ±½Ó²Ù×÷Ïß³Ì,ÊÊÓÃÓÚµ¥Öؼ̳ÐÇé¿ö¡£ÖÁÓÚÑ¡ÓÃÄÇÒ»ÖÖ£¬¾ßÌåÇé¿ö¾ßÌå·ÖÎö¡£


eg.¼Ì³ÐThread

public class MyThread_1 extends Thread
{
public void run()
{
//some code
}
}



eg.ʵÏÖRunnable½Ó¿Ú

public class MyThread_2 implements Runnable
{
public void run()
{
//some code
}
}




µ±Ê¹Óü̳д´½¨Ị̈߳¬ÕâÑùÆô¶¯Ị̈߳º

new MyThread_1().start()



µ±Ê¹ÓÃʵÏÖ½Ó¿Ú´´½¨Ị̈߳¬ÕâÑùÆô¶¯Ị̈߳º

new Thread(new MyThread_2()).start()



×¢Ò⣬ÆäʵÊÇ´´½¨Ò»¸öÏß³ÌʵÀý£¬²¢ÒÔʵÏÖÁËRunnable½Ó¿ÚµÄÀàΪ²ÎÊý´«ÈëÕâ¸öʵÀý£¬µ±Ö´ÐÐÕâ¸öÏ̵߳Äʱºò£¬MyThread_2ÖÐrunÀïÃæµÄ´úÂ뽫±»Ö´ÐС£
ÏÂÃæÊÇÍê³ÉµÄÀý×Ó£º

public class MyThread implements Runnable
{

public void run()
{
System.out.println("My Name is "+Thread.currentThread().getName());
}
public static void main(String[] args)
{
new Thread(new MyThread()).start();
}
}




Ö´Ðк󽫴òÓ¡³ö£º
My Name is Thread-0

ÄãÒ²¿ÉÒÔ´´½¨¶à¸öỊ̈߳¬ÏñÏÂÃæÕâÑù

new Thread(new MyThread()).start();
new Thread(new MyThread()).start();
new Thread(new MyThread()).start();




ÄÇô»á´òÓ¡³ö£º
My Name is Thread-0
My Name is Thread-1
My Name is Thread-2

¿´ÁËÉÏÃæµÄ½á¹û£¬Äã¿ÉÄÜ»áÈÏΪÏ̵߳ÄÖ´ÐÐ˳ÐòÊÇÒÀ´ÎÖ´Ðеģ¬µ«ÊÇÄÇÖ»ÊÇÒ»°ãÇé¿ö£¬Ç§Íò²»ÒªÓÃÒÔΪÊÇÏ̵߳ÄÖ´ÐлúÖÆ£»Ó°ÏìÏß³ÌÖ´ÐÐ˳ÐòµÄÒòËØÓм¸µã£ºÊ×ÏÈ¿´¿´Ç°ÃæÌáµ½µÄÓÅÏȼ¶±ð


public class MyThread implements Runnable
{

public void run()
{
System.out.println("My Name is "+Thread.currentThread().getName());
}
public static void main(String[] args)
{
Thread t1=new Thread(new MyThread());
Thread t2=new Thread(new MyThread());
Thread t3=new Thread(new MyThread());
t2.setPriority(Thread.MAX_PRIORITY);//¸³Óè×î¸ßÓÅÏȼ¶
t1.start();
t2.start();
t3.start();
}
}



ÔÙ¿´¿´½á¹û£º
My Name is Thread-1
My Name is Thread-0
My Name is Thread-2


Ï̵߳ÄÓÅÏȼ¶·ÖΪ10¼¶£¬·Ö±ðÓÃ1µ½10µÄÕûÊý´ú±í£¬Ä¬ÈÏÇé¿öÊÇ5¡£ÉÏÃæµÄt2.setPriority(Thread.MAX_PRIORITY)µÈ¼ÛÓët2.setPriority(10£©
È»ºóÊÇÏ̳߳ÌÐò±¾ÉíµÄÉè¼Æ£¬±ÈÈçʹÓÃsleep,yield,join£¬waitµÈ·½·¨£¨ÏêÇéÇë¿´JDKDocument)

public class MyThread implements Runnable
{
public void run()
{
try
{
int sleepTime=(int)(Math.random()*100);//²úÉúËæ»úÊý×Ö£¬
Thread.currentThread().sleep(sleepTime);//ÈÃÆäÐÝÃßÒ»¶¨Ê±¼ä£¬Ê±¼äÓÖÉÏÃæsleepTime¾ö¶¨
//public static void sleep(long millis)throw InterruptedException £¨API£©
System.out.println(Thread.currentThread().getName()+" ˯ÁË "+sleepTime);
}catch(InterruptedException ie)//ÓÉÓÚÏß³ÌÔÚÐÝÃß¿ÉÄܱ»Öжϣ¬ËùÒÔµ÷ÓÃsleep·½·¨µÄʱºòÐèÒª²¶×½Òì³£
{
ie.printStackTrace();
}
}
public static void main(String[] args)
{
Thread t1=new Thread(new MyThread());
Thread t2=new Thread(new MyThread());
Thread t3=new Thread(new MyThread());
t1.start();
t2.start();
t3.start();
}
}



Ö´Ðкó¹Û²ìÆäÊä³ö£º

Thread-0 ˯ÁË 11
Thread-2 ˯ÁË 48
Thread-1 ˯ÁË 69



ÉÏÃæµÄÖ´Ðнá¹ûÊÇËæ»úµÄ£¬ÔÙÖ´ÐкܿÉÄܳöÏÖ²»Í¬µÄ½á¹û¡£ÓÉÓÚÉÏÃæÎÒÔÚrunÖÐÌí¼ÓÁËÐÝÃßÓï¾ä£¬µ±Ïß³ÌÐÝÃßµÄʱºò¾Í»áÈóöcpu£¬cpu½«»áÑ¡ÔñÖ´Ðд¦ÓÚ runnable״̬ÖеÄÆäËûỊ̈߳¬µ±È»Ò²¿ÉÄܳöÏÖÕâÖÖÇé¿ö£¬ÐÝÃßµÄThreadÁ¢¼´½øÈëÁËrunnable״̬£¬cpuÔÙ´ÎÖ´ÐÐËü¡£
[Ïß³Ì×é¸ÅÄî]
Ïß³ÌÊÇ¿ÉÒÔ±»×éÖ¯µÄ£¬javaÖдæÔÚÏß³Ì×éµÄ¸ÅÄÿ¸öÏ̶߳¼ÊÇÒ»¸öÏß³Ì×éµÄ³ÉÔ±,Ïß³Ì×é°Ñ¶à¸öÏ̼߳¯³ÉΪһ¸ö¶ÔÏó,ͨ¹ýÏß³Ì×é¿ÉÒÔͬʱ¶ÔÆäÖеĶà¸öÏ߳̽øÐвÙ×÷,ÈçÆô¶¯Ò»¸öÏß³Ì×éµÄËùÓÐÏ̵߳È.JavaµÄÏß³Ì×éÓÉjava.lang°üÖеÄThread----GroupÀàʵÏÖ.
ThreadGroup ÀàÓÃÀ´¹ÜÀíÒ»×éÏß³Ì,°üÀ¨:Ï̵߳ÄÊýÄ¿,Ï̼߳äµÄ¹Øϵ,Ïß³ÌÕýÔÚÖ´ÐеIJÙ×÷,ÒÔ¼°Ï߳̽«ÒªÆô¶¯»òÖÕֹʱ¼äµÈ.Ïß³Ì×黹¿ÉÒÔ°üº¬Ïß³Ì×é.ÔÚJavaµÄÓ¦ÓóÌÐòÖÐ,×î¸ß²ãµÄÏß³Ì×éÊÇÃûλmainµÄÏß³Ì×é,ÔÚmainÖл¹¿ÉÒÔ¼ÓÈëÏ̻߳òÏß³Ì×é,ÔÚmianµÄ×ÓÏß³Ì×éÖÐÒ²¿ÉÒÔ¼ÓÈëÏ̺߳ÍÏß³Ì×é,ÐγÉÏß³Ì×éºÍÏß³ÌÖ®¼äµÄÊ÷×´¼Ì³Ð¹Øϵ¡£ÏñÉÏÃæ´´½¨µÄÏ̶߳¼ÊÇÊôÓÚmainÕâ¸öÏß³Ì×éµÄ¡£
½èÓÃÉÏÃæµÄÀý×Ó£¬mainÀïÃæ¿ÉÒÔÕâÑùд£º

public static void main(String[] args)
{
/***************************************
ThreadGroup(String name)
ThreadGroup(ThreadGroup parent, String name)
***********************************/
ThreadGroup group1=new ThreadGroup("group1");
ThreadGroup group2=new ThreadGroup(group1,"group2");
Thread t1=new Thread(group2,new MyThread());
Thread t2=new Thread(group2,new MyThread());
Thread t3=new Thread(group2,new MyThread());
t1.start();
t2.start();
t3.start();
}




Ïß³Ì×éµÄǶÌ×£¬t1,t2,t3±»¼ÓÈëgroup2,group2¼ÓÈëgroup1¡£
ÁíÍâÒ»¸ö±È½Ï¶à¾ÍÊǹØÓÚÏß³Ìͬ²½·½ÃæµÄ£¬ÊÔÏëÕâÑùÒ»ÖÖÇé¿ö£¬ÄãÓÐÒ»±Ê´æ¿îÔÚÒøÐУ¬ÄãÔÚÒ»¼ÒÒøÐÐΪÄãµÄÕË»§´æ¿î£¬¶øÄãµÄÆÞ×ÓÔÚÁíÒ»¼ÒÒøÐдÓÕâ¸öÕË»§Ìá¿î£¬ÏÖÔÚÄãÓÐ1000¿éÔÚÄãµÄÕË»§ÀïÃæ¡£Äã´æÈëÁË1000£¬µ«ÊÇÓÉÓÚÁíÒ»·½Ò²ÔÚ¶ÔÕâ±Ê´æ¿î½øÐвÙ×÷£¬È˼ҿªÊ¼Ö´ÐеÄʱºòÖ»¿´µ½ÕË»§ÀïÃæÔ­À´µÄ1000Ôª£¬µ±ÄãµÄÆÞ×ÓÌá¿î1000Ôªºó£¬ÄãÆÞ×ÓËùÔÚµÄÒøÐоÍÈÏΪÄãµÄÕË»§ÀïÃæûÓÐÇ®ÁË£¬¶øÄãËùÔÚµÄÒøÐÐÈ´ÈÏΪÄ㻹ÓÐ2000Ôª¡£
¿´¿´ÏÂÃæµÄÀý×Ó£º

class BlankSaving //´¢ÐîÕË»§
{
private static int money=10000;
public void add(int i)
{
money=money+i;
System.out.println("Husband ÏòÒøÐдæÈëÁË [£¤"+i+"]");
}
public void get(int i)
{
money=money-i;
System.out.println("Wife ÏòÒøÐÐÈ¡×ßÁË [£¤"+i+"]");
if(money<0)
System.out.println("Óà¶î²»×㣡");
}
public int showMoney()
{
return money;
}
}


class Operater implements Runnable
{
String name;
BlankSaving bs;
public Operater(BlankSaving b,String s)
{
name=s;
bs=b;



}
public static void oper(String name,BlankSaving bs)
{



if(name.equals("husband"))
{
try
{
for(int i=0;i<10;i++)
{
Thread.currentThread().sleep((int)(Math.random()*300));
bs.add(1000);
}
}catch(InterruptedException e){}
}else
{
try
{



for(int i=0;i<10;i++)
{
Thread.currentThread().sleep((int)(Math.random()*300));
bs.get(1000);
}
}catch(InterruptedException e){}
}
}
public void run()
{
oper(name,bs);
}
}
public class BankTest
{
public static void main(String[] args)throws InterruptedException
{
BlankSaving bs=new BlankSaving();
Operater o1=new Operater(bs,"husband");
Operater o2=new Operater(bs,"wife");
Thread t1=new Thread(o1);
Thread t2=new Thread(o2);
t1.start();
t2.start();
Thread.currentThread().sleep(500);
}



}




ÏÂÃæÊÇÆäÖÐÒ»´ÎµÄÖ´Ðнá¹û£º



---------first--------------
Husband ÏòÒøÐдæÈëÁË [£¤1000]
Wife ÏòÒøÐÐÈ¡×ßÁË [£¤1000]
Wife ÏòÒøÐÐÈ¡×ßÁË [£¤1000]
Husband ÏòÒøÐдæÈëÁË [£¤1000]
Wife ÏòÒøÐÐÈ¡×ßÁË [£¤1000]
Husband ÏòÒøÐдæÈëÁË [£¤1000]
Wife ÏòÒøÐÐÈ¡×ßÁË [£¤1000]
Husband ÏòÒøÐдæÈëÁË [£¤1000]
Wife ÏòÒøÐÐÈ¡×ßÁË [£¤1000]
Husband ÏòÒøÐдæÈëÁË [£¤1000]
Husband ÏòÒøÐдæÈëÁË [£¤1000]
Wife ÏòÒøÐÐÈ¡×ßÁË [£¤1000]
Husband ÏòÒøÐдæÈëÁË [£¤1000]
Husband ÏòÒøÐдæÈëÁË [£¤1000]
Wife ÏòÒøÐÐÈ¡×ßÁË [£¤1000]
Wife ÏòÒøÐÐÈ¡×ßÁË [£¤1000]
Husband ÏòÒøÐдæÈëÁË [£¤1000]
Wife ÏòÒøÐÐÈ¡×ßÁË [£¤1000]
Wife ÏòÒøÐÐÈ¡×ßÁË [£¤1000]
Husband ÏòÒøÐдæÈëÁË [£¤1000]

¿´µ½ÁËÂð£¬Õâ¿É²»ÊÇÕýÈ·µÄÐèÇó£¬ÔÚhusband»¹Ã»ÓнáÊø²Ù×÷µÄʱºò£¬wife¾Í²åÁ˽øÀ´£¬ÕâÑùºÜ¿ÉÄܵ¼ÖÂÒâÍâµÄ½á¹û¡£½â¾ö°ì·¨ºÜ¼òµ¥£¬¾ÍÊǽ«¶ÔÊý¾Ý½øÐвÙ×÷·½·¨ÉùÃ÷Ϊsynchronized,µ±·½·¨±»¸Ã¹Ø¼ü×ÖÉùÃ÷ºó£¬Ò²¾ÍÒâζ×Å£¬Èç¹ûÕâ¸öÊý¾Ý±»¼ÓËø£¬Ö»ÓÐÒ»¸ö¶ÔÏóµÃµ½Õâ¸öÊý¾ÝµÄËøµÄʱºò¸Ã¶ÔÏó²ÅÄܶÔÕâ¸öÊý¾Ý½øÐвÙ×÷¡£Ò²¾ÍÊǵ±Äã´æ¿îµÄʱºò£¬Õâ±ÊÕË»§ÔÚÆäËûµØ·½ÊDz»ÄܽøÐвÙ×÷µÄ£¬Ö»ÓÐÄã´æ¿îÍê±Ï£¬ÒøÐйÜÀíÈËÔ±½«ÕË»§½âËø£¬ÆäËûÈ˲ÅÄܶÔÕâ¸öÕË»§½øÐвÙ×÷¡£
ÐÞ¸Äpublic static void oper(String name,BlankSaving bs)Ϊpublic static void oper(String name,BlankSaving bs)£¬ÔÙ¿´¿´½á¹û:

Husband ÏòÒøÐдæÈëÁË [£¤1000]
Husband ÏòÒøÐдæÈëÁË [£¤1000]
Husband ÏòÒøÐдæÈëÁË [£¤1000]
Husband ÏòÒøÐдæÈëÁË [£¤1000]
Husband ÏòÒøÐдæÈëÁË [£¤1000]
Husband ÏòÒøÐдæÈëÁË [£¤1000]
Husband ÏòÒøÐдæÈëÁË [£¤1000]
Husband ÏòÒøÐдæÈëÁË [£¤1000]
Husband ÏòÒøÐдæÈëÁË [£¤1000]
Husband ÏòÒøÐдæÈëÁË [£¤1000]
Wife ÏòÒøÐÐÈ¡×ßÁË [£¤1000]
Wife ÏòÒøÐÐÈ¡×ßÁË [£¤1000]
Wife ÏòÒøÐÐÈ¡×ßÁË [£¤1000]
Wife ÏòÒøÐÐÈ¡×ßÁË [£¤1000]
Wife ÏòÒøÐÐÈ¡×ßÁË [£¤1000]
Wife ÏòÒøÐÐÈ¡×ßÁË [£¤1000]
Wife ÏòÒøÐÐÈ¡×ßÁË [£¤1000]
Wife ÏòÒøÐÐÈ¡×ßÁË [£¤1000]
Wife ÏòÒøÐÐÈ¡×ßÁË [£¤1000]
Wife ÏòÒøÐÐÈ¡×ßÁË [£¤1000]



µ±ÕÉ·òÍê³É²Ù×÷ºó£¬ÆÞ×ӲſªÊ¼Ö´ÐвÙ×÷£¬ÕâÑùµÄ»°£¬¶Ô¹²Ïí¶ÔÏóµÄ²Ù×÷¾Í²»»áÓÐÎÊÌâÁË¡£
[wait and notify]
Äã¿ÉÒÔÀûÓÃÕâÁ½¸ö·½·¨ºÜºÃµÄ¿ØÖÆÏ̵߳ÄÖ´ÐÐÁ÷³Ì£¬µ±Ï̵߳÷ÓÃwait·½·¨ºó£¬Ï߳̽«±»¹ÒÆð£¬Ö±µ½±»ÁíÒ»Ï̻߳½ÐÑ£¨notify£©»òÔòÊÇÈç¹ûwait·½·¨Ö¸¶¨ÓÐʱ¼äµÃ»°£¬ÔÚûÓб»»½ÐѵÄÇé¿öÏ£¬Ö¸¶¨Ê±¼äʱ¼ä¹ýºóÒ²½«×Ô¶¯±»»½ÐÑ¡£µ«ÊÇҪעÒâÒ»¶¨£¬±»»½ÐѲ¢²»ÊÇÖ¸ÂíÉÏÖ´ÐУ¬¶øÊÇ´Ó×éÈû״̬±äΪ¿ÉÔËÐÐ״̬£¬ÆäÊÇ·ñÔËÐл¹Òª¿´cpuµÄµ÷¶È¡£
ÊÂÀý´úÂ룺

class MyThread_1 extends Thread
{
Object lock;
public MyThread_1(Object o)
{
lock=o;
}
public void run()
{
try
{
synchronized(lock)
{
System.out.println("Enter Thread_1 and wait");
lock.wait();
System.out.println("be notified");
}
}catch(InterruptedException e){}
}
}
class MyThread_2 extends Thread
{
Object lock;
public MyThread_2(Object o)
{
lock=o;
}
public void run()
{
synchronized(lock)
{
System.out.println("Enter Thread_2 and notify");
lock.notify();
}
}
}
public class MyThread
{
public static void main(String[] args)
{
int[] in=new int[0];//notice
MyThread_1 t1=new MyThread_1(in);
MyThread_2 t2=new MyThread_2(in);
t1.start();
t2.start();
}
}





Ö´Ðнá¹ûÈçÏ£º
Enter Thread_1 and wait
Enter Thread_2 and notify
Thread_1 be notified

¿ÉÄÜÄã×¢Òâµ½ÁËÔÚʹÓÃwait and notify·½·¨µÃʱºòÎÒʹÓÃÁËsynchronized¿éÀ´°ü×°ÕâÁ½¸ö·½·¨£¬ÕâÊÇÓÉÓÚµ÷ÓÃÕâÁ½¸ö·½·¨µÄʱºòÏ̱߳ØÐë»ñµÃËø£¬Ò²¾ÍÊÇÉÏÃæ´úÂëÖеÄlock []£¬Èç¹ûÄã²»ÓÃsynchronized°ü×°ÕâÁ½¸ö·½·¨µÄµÃ»°£¬ÓÖ»òÔòËø²»Ò»ÊÇͬһ°Ñ£¬±ÈÈçÔÚMyThread_2ÖÐsynchronized (lock)¸ÄΪsynchronized(this),ÄÇôִÐÐÕâ¸ö³ÌÐòµÄʱºò½«»áÅ׳ö java.lang.IllegalMonitorStateExceptionÖ´ÐÐÆÚÒì³£¡£ÁíÍâwait and notify·½·¨ÊÇObjectÖеģ¬²¢²»ÔÚThreadÕâ¸öÀàÖС£×îºóÄã¿ÉÄÜ×¢Òâµ½ÁËÕâµã£ºint[] in=new int[0];Ϊʲô²»ÊÇ´´½¨new Object¶øÊÇÒ»¸ö0³¤¶ÈµÄÊý×飬ÄÇÊÇÒòΪÔÚjavaÖд´½¨Ò»¸ö0³¤¶ÈµÄÊý×éÀ´³äµ±Ëø¸ü¼Ó¸ßЧ¡£

Thread×÷ΪjavaÖÐÒ»ÖØÒª×é³É²¿·Ö£¬µ±È»»¹ÓкܶàµØ·½ÐèÒª¸üÉî¿ÌµÄÈÏʶ£¬ÉÏÃæÖ»ÊǶÔThreadµÄһЩ³£Ê¶ºÍÒ×´íÎÊÌâ×öÁËÒ»¸ö¼òÒªµÄ×ܽᣬÈôÒªÕæÕýµÄÕÆÎÕjavaµÄỊ̈߳¬»¹ÐèÒª×Ô¼º¶à×ö×ܽá
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ