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

JAVA µÄ¶àÏß³ÌdzÎö

·¢²¼Ê±¼ä:2006-05-03 19:07:30À´Ô´:ºìÁª×÷Õß:ÌìɽÀÏͯ
? Ò»¡¢JAVA ÓïÑÔµÄÀ´Ô´¡¢¼°Ìصã
ÔÚÕâ¸ö¸ßËÙÐÅÏ¢µÄʱ´ú£¬É̼ÒÃÇ·×·×°ÑÐÅÏ¢¡¢²úÆ·×öµ½Internet¹ú¼Ê»¥Á¬ÍøÒ³ÉÏ¡£ÔÙÕâЩ²»Ñ°³£ÍøÒ³µÄ±³ºó£¬ÒªÊô¹¦ÄÜÆëÈ«¡¢°²È«¿É¿¿µÄ±à³ÌÓïÑÔ£¬JavaÊǵ±Ö®ÎÞÀ¢µÄ¡£JavaÊÇÓÉSun Microsystem¿ª·¢µÄÒ»ÖÖ¹¦ÄÜÇ¿´óµÄÐÂÐͳÌÐòÉè¼ÆÓïÑÔ¡£ÊÇÓëƽ̨Î޹صıà³ÌÓïÑÔ¡£ËüÊÇÒ»ÖÖ¼òµ¥µÄ¡¢ÃæÏó¶ÔÏóµÄ¡¢·Ö²¼Ê½µÄ¡¢½âÊ͵ġ¢¼ü׳µÄ¡¢°²È«µÄ¡¢½á¹¹µÄÖÐÁ¢µÄ¡¢¿ÉÒÆÖ²µÄ¡¢ÐÔÄܺÜÓÅÒìµÄ¡¢¶àÏ̵߳ġ¢¶¯Ì¬µÄ¡¢ÓïÑÔ¡£
Java×ÔÎÊÊÀÒÔºó£¬ÒÔÆä±à³Ì¼òµ¥¡¢´úÂë¸ßЧ¡¢¿ÉÒÆÖ²ÐÔÇ¿£¬ºÜ¿ìÊܵ½Á˹ã´ó¼ÆËã»ú±à³ÌÈËÊ¿µÄÇàíù¡£JavaÓïÑÔÊÇInternetÉϾßÓиïÃüÐԵıà³ÌÓïÑÔ£¬Ëü¾ßÓÐÇ¿´óµÄ¶¯»­¡¢¶àýÌåºÍ½»»¥¹¦ÄÜ£¬ËûʹWorld Web½øÈëÁËÒ»¸öȫеÄʱ´ú¡£JavaÓïÑÔÓëC++¼«ÎªÀàËÆ£¬¿ÉÓÃËüÀ´´´½¨°²È«µÄ¡¢¿ÉÒÆÖ²µÄ¡¢¶àÏ̵߳Ľ»»¥Ê½³ÌÐò¡£ÁíÍâÓÃJava¿ª·¢³öÀ´µÄ³ÌÐòÓëƽ̨Î޹أ¬¿ÉÔÚ¶àÖÖƽ̨ÉÏÔËÐС£ºǫ́¿ª·¢£¬ÊÇÒ»ÖÖ¸ßЧ¡¢ÊµÓõıà³Ì·½·¨¡£ÈËÃÇÔÚÆÁĻǰֻÄÜ¿´µ½ÀýÈçͼ°¸¡¢¼ÆËãµÄ½á¹ûµÈ¡£Êµ¼ÊÉϲÙ×÷ϵͳÍùÍùÔÚºǫ́À´µ÷¶ÈһЩʼþ¡¢¹ÜÀí³ÌÐòµÄÁ÷ÏòµÈ¡£ÀýÈç²Ù×÷ϵͳÖеĶÑÕ»£¬Ï̼߳äµÄ×ÊÔ´·ÖÅäÓë¹ÜÀí£¬ÄÚ´æµÄ´´½¨¡¢·ÃÎÊ¡¢¹ÜÀíµÈ¡£¿Éν¾Ù²»Ê¢¾Ù¡£ÏÂÃæ¾Í¶àÏß³ÌÀ´Ì¸Ò»Ì¸¡£
? ¶þ¡¢JAVAµÄ¶àÏß³ÌÀíÂÛ
2.1ÒýÈë
JavaÌṩµÄ¶àÏ̹߳¦ÄÜʹµÃÔÚÒ»¸ö³ÌÐòÀï¿ÉͬʱִÐжà¸öСÈÎÎñ¡£Ïß³ÌÓÐʱҲ³ÆС½ø³ÌÊÇÒ»¸ö´ó½ø³ÌÀï·Ö³öÀ´µÄСµÄ¶ÀÁ¢µÄ½ø³Ì¡£ÒòΪJavaʵÏֵĶàÏ̼߳¼Êõ£¬ËùÒÔ±ÈCºÍC++¸ü¼ü׳¡£¶àÏ̴߳øÀ´µÄ¸ü´óµÄºÃ´¦ÊǸüºÃµÄ½»»¥ÐÔÄܺÍʵʱ¿ØÖÆÐÔÄÜ¡£µ±È»ÊµÊ±¿ØÖÆÐÔÄÜ»¹È¡¾öÓÚϵͳ±¾Éí(UNIX, Windows,MacintoshµÈ)£¬ÔÚ¿ª·¢ÄÑÒ׳̶ȺÍÐÔÄÜÉ϶¼±Èµ¥Ïß³ÌÒªºÃ¡£´«Í³±à³Ì»·¾³Í¨³£Êǵ¥Ï̵߳ģ¬ÓÉÓÚJAVAÊǶàÏ̵߳ġ£¾¡¹Ü¶àÏß³ÌÊÇÇ¿´ó¶øÁéÇɵıà³Ì¹¤¾ß£¬µ«ÒªÓúÃÈ´²»ÈÝÒ×£¬ÇÒÓÐÐí¶àÏÝÚ壬¼´Ê¹±à³ÌÀÏÊÖÒ²ÄÑÃâÎóÓá£ÎªÁ˸üºÃµÄÁ˽âỊ̈߳¬Óð칫ÊÒ¹¤×÷ÈËÔ±×÷±ÈÓ÷¡£°ì¹«ÊÒ¹¤×÷ÈËÔ±¾ÍÏó CPU£¬¸ù¾ÝÉϼ¶Ö¸Ê¾×ö¹¤×÷£¬¾ÍÏóÖ´ÐÐÒ»¸öÏ̡߳£ÔÚµ¥Ï̻߳·¾³ÖУ¬Ã¿¸ö³ÌÐò±àдºÍÖ´Ðеķ½Ê½ÊÇÈκÎʱºò³ÌÐòÖ»¿¼ÂÇÒ»¸ö´¦Àí˳Ðò¡£ÓÃÎÒÃǵıÈÓ÷£¬¾ÍÏó°ì¹«ÊÒ¹¤×÷ÈËÔ±´ÓÍ·µ½Î²²»ÊÜ´òÈźͷÖÐÄ£¬Ö»°²ÅÅ×öÒ»¸ö¹¤×÷¡£µ±È»£¬Êµ¼ÊÉú»îÖй¤×÷ÈËÔ±ºÜÄÑÒ»´ÎÖ»ÓÐÒ»¸öÈÎÎñ£¬¸ü³£¼ûµÄÊǹ¤×÷ÈËԱҪͬʱ×ö¼¸¼þÊ¡£Àϰ彫¹¤×÷½»¸ø¹¤×÷ÈËÔ±£¬Ï£Íû¹¤×÷ÈËÔ±×öÒ»Õâ¸ö¹¤×÷£¬ÔÙ×öµãÄǸö¹¤×÷£¬µÈµÈ¡£Èç¹ûÒ»¸öÈÎÎñÎÞ·¨×öÏÂÈ¥ÁË£¬±ÈÈ繤×÷ÈËÔ±µÈ´ýÁíÒ»²¿ÃŵÄÐÅÏ¢£¬Ôò¹¤×÷ÈËÔ±½«Õâ¸ö¹¤×÷·ÅÔÚÒ»±ß£¬×ªÈëÁíÒ»¸ö¹¤×÷¡£Ò»°ãÀ´Ëµ£¬ÀÏ°åÏ£Íû¹¤×÷ÈËÔ±ÊÖÍ·µÄ¸÷¸öÈÎÎñÿһÌ춼ÓÐһЩ½øÕ¹¡£ÕâÑù¾ÍÒýÈëÁ˶àÏ̵߳ĸÅÄî¡£¶àÏ̱߳à³Ì»·¾³ÓëÕâ¸öµäÐ͵İ칫Êҷdz£ÏàËÆ£¬Í¬Ê±¸øCPU·ÖÅäÁ˼¸¸öÈÎÎñ»òÏ̡߳£ºÍ°ì¹«ÊÒÈËÔ±Ò»Ñù£¬¼ÆËã»úCPUʵ¼ÊÉϲ»¿ÉÄÜͬһʱ¼ä×ö¼¸¼þÊ£¬¶øÊÇ°Ñʱ¼ä·ÖÅäµ½²»Í¬µÄỊ̈߳¬Ê¹Ã¿¸öÏ̶߳¼Óеã½øÕ¹¡£Èç¹ûÒ»¸öÏß³ÌÎÞ·¨½øÐУ¬±ÈÈçÏß³ÌÒªÇóµÄ¼üÅÌÊäÈëÉÐδȡµÃ£¬ÔòתÈëÁíÒ»Ï̵߳Ť×÷¡£Í¨³££¬CPUÔÚÏ̼߳äµÄÇл»·Ç³£Ñ¸ËÙ£¬Ê¹ÈËÃǸоõµ½ºÃÏóËùÓÐÏß³ÌÊÇͬʱ½øÐеġ£
Èκδ¦Àí»·¾³£¬ÎÞÂÛÊǵ¥Ï̻߳¹ÊǶàỊ̈߳¬¶¼ÓÐÈý¸ö¹Ø¼ü·½Ãæ¡£µÚÒ»¸öÊÇCPU£¬Ëüʵ¼ÊÉϽøÐмÆËã»ú»î¶¯£»µÚ¶þ¸öÊÇÖ´ÐеijÌÐòµÄ´úÂ룻µÚÈý¸öÊdzÌÐò²Ù×÷µÄÊý¾Ý.¡£
ÔÚ¶àÏ̱߳à³ÌÖУ¬Ã¿¸öÏ̶߳¼ÓñàÂëÌṩÏ̵߳ÄÐÐΪ£¬ÓÃÊý¾Ý¹©¸ø±àÂë²Ù×÷¡£¶à¸öÏ߳̿ÉÒÔͬʱ´¦Àíͬһ±àÂëºÍÊý¾Ý£¬²»Í¬µÄÏß³ÌÒ²¿ÉÄܸ÷Óв»Í¬µÄ±àÂëºÍÊý¾Ý¡£ÊÂʵÉϱàÂëºÍÊý¾Ý²¿·ÖÊÇÏ൱¶ÀÁ¢µÄ£¬ÐèҪʱ¼´¿ÉÏòÏß³ÌÌṩ¡£Òò´Ë¾­³£ÊǼ¸¸öÏß³ÌʹÓÃͬһ±àÂëºÍ²»Í¬µÄÊý¾Ý¡£Õâ¸ö˼ÏëÒ²¿ÉÒÔÓð칫ÊÒ¹¤×÷ÈËÔ±À´±ÈÓ÷¡£»á¼Æ¿ÉÄÜÒª×öÒ»¸ö²¿ÃŵÄÕÊ»ò¼¸¸ö»ò¼¸¸ö²¿ÃŵÄÕÊ¡£ÈκÎÇé¿öµÄ×öÕʵÄÈÎÎñÊÇÏàͬµÄ³ÌÐò´úÂ룬µ«Ã¿¸ö²¿ÃŵÄÊý¾ÝÊDz»Í¬µÄ¡£»á¼Æ¿ÉÄÜÒª×öÕû¸ö¹«Ë¾µÄÕÊ£¬ÕâʱÓм¸¸öÈÎÎñ£¬µ«ÓÐЩÊý¾ÝÊǹ²ÏíµÄ£¬ÒòΪ¹«Ë¾ÕÊÐèÒªÀ´×Ô¸÷¸ö²¿ÃŵÄÊý¾Ý¡£
¶àÏ̱߳à³Ì»·¾³Ó÷½±ãµÄÄ£ÐÍÒþ²ØCPUÔÚÈÎÎñÇл»¼äµÄÊÂʵ¡£Ä£ÐÍÔÊÐí¼Ù×°³ÉÓжà¸ö¿ÉÓõÄCPU¡£ÎªÁ˽¨Á¢ÁíÒ»¸öÈÎÎñ£¬±à³ÌÈËÔ±ÒªÇóÁíÒ»¸öÐéÄâCPU£¬Ö¸Ê¾Ëü¿ªÊ¼ÓÃij¸öÊý¾Ý×éÖ´ÐÐij¸ö³ÌÐò¶Î¡£ÏÂÃæÎÒÃÇÀ´½¨Á¢Ï̡߳£
½¨Á¢Ïß³Ì
ÔÚJAVAÖн¨Á¢Ï̲߳¢²»À§ÄÑ£¬ËùÐèÒªµÄÈý¼þÊ£ºÖ´ÐеĴúÂë¡¢´úÂëËù²Ù×÷µÄÊý¾ÝºÍÖ´ÐдúÂëµÄÐéÄâCPU¡£ÐéÄâCPU°ü×°ÔÚThreadÀàµÄʵÀýÖС£½¨Á¢Thread¶ÔÏóʱ£¬±ØÐëÌṩִÐеĴúÂëºÍ´úÂëËù´¦ÀíµÄÊý¾Ý¡£JAVAµÄÃæÏò¶ÔÏóÄ£ÐÍÒªÇó³ÌÐò´úÂëÖ»ÄÜд³ÉÀàµÄ³ÉÔ±·½·¨¡£Êý¾ÝÖ»ÄÜ×÷Ϊ·½·¨ÖеÄ×Ô¶¯£¨»ò±¾µØ£©±äÁ¿»òÀàµÄ³ÉÔ±´æÔÚ¡£ÕâЩ¹æÔòÒªÇóΪÏß³ÌÌṩµÄ´úÂëºÍÊý¾ÝÓ¦ÒÔÀàµÄʵÀýµÄÐÎʽ³öÏÖ¡£
Public class SimpleRunnable implemants Runable{
Private String message;
Public static void main(String args[]){
SimpleRunnable r1=new SimpleRunnable(¡°Hello¡±);
Thread t1=new Thread(r1);
t1.start();
}
public SimpleRunnable(String message){
this.message=message;
}
public void run(){
for(;;){
System.out.println(message);
}
}
}
Ï߳̿ªÊ¼Ö´ÐÐʱ£¬ËüÔÚpublic void run()·½·¨ÖÐÖ´ÐС£ÕâÖÖ·½·¨ÊǶ¨ÒåµÄÏß³ÌÖ´ÐеÄÆðµã£¬¾ÍÏóÓ¦ÓóÌÐò´Ómain()¿ªÊ¼¡¢Ð¡³ÌÐò´Óinit()¿ªÊ¼Ò»Ñù¡£Ï̲߳Ù×÷µÄ±¾µØÊý¾ÝÊÇ´«ÈëÏ̵߳ĶÔÏóµÄ³ÉÔ±¡£
Ê×ÏÈ£¬main()·½·¨¹¹ÔìSimpleRunnableÀàµÄʵÀý¡£×¢Ò⣬ʵÀýÓÐ×Ô¼ºµÄÊý¾Ý£¬ÕâÀïÊÇÒ»¸öString,³õʼ»¯Îª¡±Hello¡±.ÓÉÓÚʵÀýr1´«ÈëThreadÀ๹ÔìÆ÷£¬ÕâÊÇÏß³ÌÔËÐÐʱ´¦ÀíµÄÊý¾Ý¡£Ö´ÐеĴúÂëÊÇʵÀý·½·¨run()¡£
2.2 Ï̵߳ĹÜÀí
µ¥Ï̵߳ijÌÐò¶¼ÓÐÒ»¸ömainÖ´ÐÐÌ壬ËüÔËÐÐһЩ´úÂ룬µ±³ÌÐò½áÊøÖ´Ðкó£¬ËüÕýºÃÍ˳ö£¬³ÌÐòͬʱ½áÊøÔËÐС£ÔÚJAVAÖÐÎÒÃÇÒªµÃµ½ÏàͬµÄÓ¦´ð£¬±ØÐëÉÔ΢½øÐиĶ¯¡£Ö»Óе±ËùÓеÄÏß³ÌÍ˳öºó£¬³ÌÐò²ÅÄܽáÊø¡£Ö»ÒªÓÐÒ»¸öÏß³ÌÒ»Ö±ÔÚÔËÐУ¬³ÌÐò¾ÍÎÞ·¨Í˳ö¡£Ḭ̈߳üÀ¨Ëĸö״̬£ºnew(¿ªÊ¼),running (ÔËÐÐ),wait(µÈºò)ºÍdone(½áÊø)¡£µÚÒ»´Î´´½¨Ïß³Ìʱ£¬¶¼Î»ÓÚnew״̬£¬ÔÚÕâ¸ö״̬Ï£¬²»ÄÜÔËÐÐỊ̈߳¬Ö»Äܵȴý¡£È»ºó£¬Ï̻߳òÕßÓÉ·½·¨ start¿ªÊ¼»òÕßËÍÍùdone״̬£¬Î»ÓÚdoneÖеÄÏß³ÌÒѾ­½áÊøÖ´ÐУ¬ÕâÊÇÏ̵߳Ä×îºóÒ»¸ö״̬¡£Ò»µ©Ïß³ÌλÓÚÕâ¸ö״̬£¬¾Í²»ÄÜÔٴγöÏÖ£¬¶øÇÒµ± JAVAÐéÄâ»úÖеÄËùÓÐÏ̶߳¼Î»ÓÚdone״̬ʱ£¬³ÌÐò¾ÍÇ¿ÐÐÖÐÖ¹¡£µ±Ç°ÕýÔÚÖ´ÐеÄËùÓÐÏ̶߳¼Î»ÓÚrunning״̬£¬ÔÚ³ÌÐòÖ®¼äÓÃijÖÖ·½·¨°Ñ´¦ÀíÆ÷µÄÖ´ÐÐʱ¼ä·Ö³Éʱ¼äƬ£¬Î»ÓÚrunning״̬µÄÿ¸öÏ̶߳¼ÊÇÄÜÔËÐеģ¬µ«ÔÚÒ»¸ö¸ø¶¨µÄʱ¼äÄÚ£¬Ã¿¸öϵͳ´¦ÀíÆ÷Ö»ÄÜÔËÐÐÒ»¸öÏ̡߳£ÓëλÓÚrunning״̬µÄÏ̲߳»Í¬£¬ÓÉÓÚijÖÖÔ­Òò£¬¿ÉÒÔ°ÑÒѾ­Î»ÓÚwaiting״̬µÄÏ̴߳ÓÒ»×é¿ÉÖ´ÐÐÏß³ÌÖÐɾ³ý¡£Èç¹ûÏ̵߳ÄÖ´Ðб»Öжϣ¬¾Í»Øµ½waiting״̬¡£ÓöàÖÖ·½·¨ÄÜÖжÏÒ»¸öÏ̡߳£Ïß³ÌÄܱ»¹ÒÆð£¬ÔÚϵͳ×ÊÔ´ÉϵȺò£¬»òÕß±»¸æÖª½øÈëÐÝÃß״̬¡£¸Ã״̬µÄÏ߳̿ÉÒÔ·µ»Øµ½running״̬£¬Ò²ÄÜÓÉ·½·¨stopËÍÈëdone ״̬£¬
·½·¨
ÃèÊö
ÓÐЧ״̬
Ä¿µÄ״̬
Start()
¿ªÊ¼Ö´ÐÐÒ»¸öÏß³Ì
New
Running
Stop()
½áÊøÖ´ÐÐÒ»¸öÏß³Ì
New»òrunning
Done
Sleep(long)
ÔÝÍ£Ò»¶Îʱ¼ä£¬Õâ¸öʱ¼äΪ¸ø¶¨µÄºÁÃë
Running
Wait
Sleep(long,int)
ÔÝͣƬ¿Ì£¬¿ÉÒÔ¾«È·µ½ÄÉÃë
Running
Wait
Suspend()
¹ÒÆðÖ´ÐÐ
Running
Wait
Resume()
»Ö¸´Ö´ÐÐ
Wait
Running
Yield()
Ã÷È··ÅÆúÖ´ÐÐ
Running
Running
2.3Ï̵߳ĵ÷¶È
Ïß³ÌÔËÐеÄ˳ÐòÒÔ¼°´Ó´¦ÀíÆ÷ÖлñµÃµÄʱ¼äÊýÁ¿Ö÷Ҫȡ¾öÓÚ¿ª·¢Õߣ¬´¦ÀíÆ÷¸øÿ¸öÏ̷߳ÖÅäÒ»¸öʱ¼äƬ£¬¶øÇÒÏ̵߳ÄÔËÐв»ÄÜÓ°ÏìÕû¸öϵͳ¡£´¦ÀíÆ÷Ï̵߳Äϵͳ»òÕßÊÇÇÀռʽµÄ£¬»òÕßÊÇ·ÇÇÀռʽµÄ¡£ÇÀռʽϵͳÔÚÈκθø¶¨µÄʱ¼äÄÚ½«ÔËÐÐ×î¸ßÓÅÏȼ¶µÄỊ̈߳¬ÏµÍ³ÖеÄËùÓÐÏ̶߳¼ÓÐ×Ô¼ºµÄÓÅÏȼ¶¡£ Thread.NORM_PRIORITYÊÇÏ̵߳Äȱʡֵ£¬ThreadÀàÌṩÁËsetPriorityºÍgetPriority·½·¨À´ÉèÖúͶÁÈ¡ÓÅÏÈȨ£¬Ê¹ÓÃsetPriority·½·¨ÄܸıäJavaÐéÄâ»úÖеÄÏ̵߳ÄÖØÒªÐÔ£¬Ëüµ÷ÓÃÒ»¸öÕûÊý£¬Àà±äÁ¿Thread.MIN_PRIORITYºÍ Thread.MAX_PRIORITY¾ö¶¨Õâ¸öÕûÊýµÄÓÐЧ·¶Î§¡£JavaÐéÄâ»úÊÇÇÀռʽµÄ£¬ËüÄܱ£Ö¤ÔËÐÐÓÅÏȼ¶×î¸ßµÄÏ̡߳£ÔÚJAVAÐéÄâ»úÖÐÎÒÃÇ°ÑÒ»¸öÏ̵߳ÄÓÅÏȼ¶¸ÄΪ×î¸ß£¬ÄÇôËû½«È¡´úµ±Ç°ÕýÔÚÔËÐеÄỊ̈߳¬³ý·ÇÕâ¸öÏ߳̽áÊøÔËÐлòÕß±»Ò»ÌõÐÝÃßÃüÁî·ÅÈëwaiting״̬£¬·ñÕß½«Ò»Ö±Õ¼ÓÃËùÓеĴ¦ÀíÆ÷µÄʱ¼ä¡£Èç¹ûÓöµ½Á½¸öÓÅÏȼ¶ÏàͬµÄỊ̈߳¬²Ù×÷ϵͳ¿ÉÄÜÓ°ÏìÏ̵߳ÄÖ´ÐÐ˳Ðò¡£¶øÇÒÕâ¸öÇø±ðÈ¡¾öÓÚʱ¼äƬ£¨time slicing£©µÄ¸ÅÄî¡£
¹ÜÀí¼¸¸öÏ̲߳¢²»ÊÇÕæÕýµÄÄÑÌ⣬¶ÔÓÚÉÏ°Ù¸öÏß³ÌËüÊÇÔõÑù¹ÜÀíµÄÄØ£¿µ±È»¿ÉÒÔͨ¹ýÑ­»·£¬À´Ö´ÐÐÿһ¸öỊ̈߳¬µ«ÊÇÕâÏÔÈ»ÊÇÈß³¤¡¢·¦Î¶¡£JAVA´´½¨ÁËÏß³Ì×é¡£Ïß³Ì×éÊÇÏ̵߳ÄÒ»¸öÆ×ϵ×飬ÿ¸ö×é°üº¬µÄÏß³ÌÊý²»ÊÜÏÞÖÆ£¬ÄܶÔÿ¸öÏß³ÌÃüÃû²¢ÄÜÔÚÕû¸öÏß³Ì×éÖÐÖ´ÐÐ(Suspend)ºÍÍ£Ö¹(Stop)ÕâÑùµÄ²Ù×÷¡£
2.4ÐźűêÖ¾£º±£»¤ÆäËü¹²Ïí×ÊÔ´
ÕâÖÖÀàÐ͵ı£»¤±»³ÆΪ»¥³âËø¡£Ä³¸öʱ¼äÖ»ÄÜÓÐÒ»¸öÏ̶߳ÁÈ¡»òÐÞ¸ÄÕâ¸öÊý¾ÝÖµ¡£ÔÚ¶ÔÎļþÓÈÆäÊÇÐÅÏ¢Êý¾Ý¿â½øÐд¦Àíʱ£¬¶ÁÈ¡µÄÊý¾Ý×ÜÊǶàÓÚдÊý¾Ý£¬¸ù¾ÝÕâ¸öÇé¿ö£¬¿ÉÒÔ¼ò»¯³ÌÐò¡£ÏÂÃæ¾ÙÒ»Àý£¬¼ÙÉèÓÐÒ»¸ö¹ÍÔ±ÐÅÏ¢µÄÊý¾Ý¿â£¬ÆäÖаüÀ¨¹ÍÔ±µÄµØÖ·ºÍµç»°ºÅÂëµÈÐÅÏ¢£¬ÓÐʱҪ½øÐÐÐ޸ģ¬µ«Òª¸ü¶àµÄ»¹ÊǶÁÊý¾Ý£¬Òò´ËÒª¾¡¿ÉÄÜ·ÀÖ¹Êý¾Ý±»ÆÆ»µ»òÈÎÒâɾ¸Ä¡£ÎÒÃÇÒýÈëÇ°Ã滥³âËøµÄ¸ÅÄÔÊÐíÒ»¸ö¶ÁÈ¡Ëø£¨red lock£©ºÍдÈëËø£¨write lock£©,¿É¸ù¾ÝÐèҪȷ¶¨ÓÐȨ¶ÁÈ¡Êý¾ÝµÄÈËÔ±£¬¶øÇÒµ±Ä³ÈËҪдÊý¾Ýʱ£¬±ØÐëÓл¥³âËø£¬Õâ¾ÍÊÇÐźűêÖ¾µÄ¸ÅÄî¡£ÐźűêÖ¾ÓÐÁ½ÖÖ״̬£¬Ê×ÏÈÊÇempty() ״̬£¬±íʾûÓÐÈκÎÏß³ÌÕýÔÚ¶Á»òд£¬¿ÉÒÔ½ÓÊܶÁºÍдµÄÇëÇ󣬲¢ÇÒÁ¢¼´Ìṩ·þÎñ£»µÚ¶þÖÖ״̬ÊÇreading()״̬£¬±íʾÓÐÏß³ÌÕýÔÚ´ÓÊý¾Ý¿âÖжÁÐÅÏ¢£¬²¢¼Ç¼½øÐжÁ²Ù×÷µÄÏß³ÌÊý£¬µ±ËüΪ0ʱ£¬·µ»Øempty״̬£¬Ò»¸öдÇëÇ󽫵¼ÖÂÕâ¸öÏ߳̽øÈëµÈ´ý״̬¡£
Ö»ÄÜ´Óempty״̬½øÈëwriting״̬£¬Ò»µ©½øÈëwriting״̬ºó£¬ÆäËüÏ̶߳¼²»ÄÜд²Ù×÷£¬ÈκÎд»ò¶ÁÇëÇ󶼱ØÐëµÈµ½Õâ¸öÏß³ÌÍê³Éд²Ù×÷Ϊֹ£¬¶øÇÒwaiting״̬ÖеĽø³ÌÒ²±ØÐëÒ»Ö±µÈµ½Ð´²Ù×÷½áÊø¡£Íê³É²Ù×÷ºó£¬·µ»Øµ½empty״̬£¬·¢ËÍÒ»¸ö֪ͨÐźţ¬µÈ´ýµÄÏ߳̽«µÃµ½·þÎñ¡£
ÏÂÃæʵÏÖÁËÕâ¸öÐźűêÖ¾
class Semaphore{
final static int EMPTY=0;
final static int READING=1;
final static int WRITING=2;
protected int state=EMPTY;
protected int readCnt=0;
public synchronized void readLock(){
if(state==EMPTY){
state=READING;
}
else if(state==READING){
}
else if(state==WRITING){
while(state==WRITING){
try {wait();}
catch(InterruptedException e){;}
}
state=READING;
}
readCnt++;
return;
}
public synchronized void writeLock(){
if(state==EMPTY){
state=WRITING;
}
else{
while(state!=EMPTY){
try {wait();}
catch(InterruptedException e) {;}
}
}
}
public synchronized void readUnlock(){
readCnt--;
if(readCnt==0){
state=EMPTY;
notify();
}
}
public synchronized void writeUnlock(){
state=EMPTY;
notify();
}
}
ÏÖÔÚÊDzâÊÔÐźűêÖ¾µÄ³ÌÐò£º
class Process extends Thread{
String op;
Semaphore sem;
Process(String name,String op,Semaphore sem){
super(name);
this.op=op;
this.sem=sem;
start();
}
public void run(){
if(op
catch(InterruptedException e){;}
System.out.println("Unlocking readLock:"+getName());
sem.readUnlock();
}
else if(op
catch(InterruptedException e){;}
System.out.println("Unlocking writeLock:"+getName());
sem.writeUnlock();
}
}
}
public class testSem{
public static void main(String argv[]){
Semaphore lock = new Semaphore();
new Process("1","read",lock);
new Process("2","read",lock);
new Process("3","write",lock);
new Process("4","read",lock);
}
}
testSem Àà´ÓprocessÀàµÄËĸöʵÀý¿ªÊ¼£¬ËüÊǸöỊ̈߳¬ÓÃÀ´¶Á»òдһ¸ö¹²ÏíÎļþ¡£SemaphoreÀà±£Ö¤·ÃÎʲ»»áÆÆ»µÎļþ£¬Ö´ÐгÌÐò£¬Êä³ö½á¹ûÈçÏ£º
Trying to get readLock:1
Read op:1
Trying to get readLock:2
Read op:2
Trying to get writeLock:3
Trying to get readLock:4
Read op:4
Unlocking readLock:1
Unlocking readLock:2
Unlocking readLock:4
Write op:3
Unlocking writeLock:3
´ÓÕâ¿É¿´µ½£¬
2.5ËÀËøÒÔ¼°ÔõÑù±ÜÃâËÀËø£º
ΪÁË·ÀÖ¹Êý¾ÝÏîÄ¿µÄ²¢·¢·ÃÎÊ£¬Ó¦½«Êý¾ÝÏîÄ¿±êΪרÓã¬Ö»ÓÐͨ¹ýÀà±¾ÉíµÄʵÀý·½·¨µÄͬ²½Çø·ÃÎÊ¡£ÎªÁ˽øÈë¹Ø¼üÇø£¬Ï̱߳ØÐëÈ¡µÃ¶ÔÏóµÄËø¡£¼ÙÉèÏß³ÌÒª¶ÀÕ¼·ÃÎÊÁ½¸ö²»Í¬¶ÔÏóµÄÊý¾Ý£¬Ôò±ØÐë´Óÿ¸ö¶ÔÏó¸÷È¡Ò»¸ö²»Í¬µÄËø¡£ÏÖÔÚ¼ÙÉèÁíÒ»¸öÏß³ÌÒ²Òª¶ÀÕ¼·ÃÎÊÕâÁ½¸ö¶ÔÏó£¬Ôò¸Ã½ø³Ì±ØÐëµÃµ½ÕâÁ½°ÑËøÖ®ºó²ÅÄܽøÈë¡£ÓÉÓÚÐèÒªÁ½°ÑËø£¬±à³ÌÈç¹û²»Ð¡ÐľͿÉÄܳöÏÖËÀËø¡£¼ÙÉèµÚÒ»¸öÏß³ÌÈ¡µÃ¶ÔÏóAµÄËø£¬×¼±¸È¡¶ÔÏóBµÄËø£¬¶øµÚ¶þ¸öÏß³ÌÈ¡µÃÁ˶ÔÏóBµÄËø£¬×¼±¸È¡¶ÔÏóAµÄËø£¬Á½¸öÏ̶߳¼²»ÄܽøÈ룬ÒòΪÁ½Õ߶¼²»ÄÜÀ뿪½øÈëµÄͬ²½¿é£¬¼ÈÁ½Õ߶¼²»ÄÜ·ÅÆúÄ¿Ç°³ÖÓеÄËø¡£±ÜÃâËÀËøÒªÈÏÕæÉè¼Æ¡£Ïß³ÌÒòΪij¸öÏȾöÌõ¼þ¶øÊÜ×èʱ£¬ÈçÐèÒªËø±ê¼Çʱ£¬²»ÄÜÈÃÏ̵߳ÄÍ£Ö¹±¾Éí½ûÖ¹Ìõ¼þµÄ±ä»¯¡£Èç¹ûҪȡµÃ¶à¸ö×ÊÔ´£¬ÈçÁ½¸ö²»Í¬¶ÔÏóµÄËø£¬±ØÐ붨ÒåÈ¡µÃ×ÊÔ´µÄ˳Ðò¡£Èç¹û¶ÔÏóAºÍBµÄËø×ÜÊÇ°´×Öĸ˳ÐòÈ¡µÃ£¬Ôò²»»á³öÏÖÇ°Ãæ˵µÀµÄ¶öËÀÌõ¼þ¡£
? Èý¡¢Java¶àÏ̵߳ÄÓÅȱµã
ÓÉÓÚJAVAµÄ¶àÏ̹߳¦ÄÜÆëÈ«£¬¸÷ÖÖÇé¿öÃæÃæ¾ßµ½£¬Ëü´øÀ´µÄºÃ´¦Ò²ÊÇÏÔÈ»Ò×¼ûµÄ¡£¶àÏ̴߳øÀ´µÄ¸ü´óµÄºÃ´¦ÊǸüºÃµÄ½»»¥ÐÔÄܺÍʵʱ¿ØÖÆÐÔÄÜ¡£µ±È»ÊµÊ±¿ØÖÆÐÔÄÜ»¹È¡¾öÓÚϵͳ±¾Éí(UNIX,Windows,Macintosh µÈ)£¬ÔÚ¿ª·¢ÄÑÒ׳̶ȺÍÐÔÄÜÉ϶¼±Èµ¥Ïß³ÌÒªºÃ¡£µ±È»Ò»¸öºÃµÄ³ÌÐòÉè¼ÆÓïÑԿ϶¨Ò²ÄÑÃâÓв»×ãÖ®´¦¡£ÓÉÓÚ¶àÏ̻߳¹Ã»Óгä·ÖÀûÓûù±¾OSµÄÕâÒ»¹¦ÄÜ¡£ÕâµãÎÒÔÚÇ°ÃæÒѾ­Ìáµ½£¬¶ÔÓÚ²»Í¬µÄϵͳ£¬ÉÏÃæµÄ³ÌÐò¿ÉÄÜ»á³öÏÖ½ØÈ»²»Í¬µÄ½á¹û£¬Õâʹ±à³ÌÕßż»á¸Ðµ½ÃԻ󲻽⡣ϣÍûÔÚ²»¾ÃµÄ½«À´JAVAµÄ¶àÏß³ÌÄܳä·ÖÀûÓõ½²Ù×÷ϵͳ£¬¼õÉÙ¶Ô±à³ÌÕßµÄÀ§»ó¡£ÎÒÆÚ´ý×ÅJAVA»á¸üºÃ¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ