ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Java SE Lesson 16



1. Èç¹ûij¸ö synchronized ·½·¨ÊÇ static µÄ£¬ÄÇôµ±Ï̷߳ÃÎʸ÷½·¨Ê±£¬ËüËøµÄ²¢²»ÊÇ
   synchronized ·½·¨ËùÔڵĶÔÏ󣬶øÊÇ synchronized ·½·¨ËùÔڵĶÔÏóËù¶ÔÓ¦µÄ Class ¶Ô
   Ïó£¬ÒòΪ Java ÖÐÎÞÂÛÒ»¸öÀàÓжàÉÙ¸ö¶ÔÏó£¬ÕâЩ¶ÔÏó»á¶ÔӦΨһһ¸ö Class ¶ÔÏó£¬
   Òò´Ëµ±Ï̷ֱ߳ð·ÃÎÊͬһ¸öÀàµÄÁ½¸ö¶ÔÏóµÄÁ½¸ö static£¬synchronized ·½·¨Ê±£¬ËûÃÇ
   µÄÖ´ÐÐ˳ÐòÒ²ÊÇ˳ÐòµÄ£¬Ò²¾ÍÊÇ˵һ¸öÏß³ÌÏÈÈ¥Ö´Ðз½·¨£¬Ö´ÐÐÍê±ÏºóÁíÒ»¸öÏß³Ì
   ²Å¿ªÊ¼Ö´ÐС£
2. synchronized ¿é£¬Ð´·¨£º
   synchronized(object)
   {

   }
   ±íʾÏß³ÌÔÚÖ´ÐеÄʱºò»á¶Ô object ¶ÔÏóÉÏËø¡£
3. synchronized ·½·¨ÊÇÒ»ÖÖ´ÖÁ£¶ÈµÄ²¢·¢¿ØÖÆ£¬Ä³Ò»Ê±¿Ì£¬Ö»ÄÜÓÐÒ»¸öÏß³ÌÖ´ÐиÃ
   synchronized ·½·¨;synchronized ¿éÔòÊÇÒ»ÖÖϸÁ£¶ÈµÄ²¢·¢¿ØÖÆ£¬Ö»»á½«¿éÖеĴúÂëͬ
   ²½£¬Î»ÓÚ·½·¨ÄÚ¡¢synchronized ¿éÖ®ÍâµÄ´úÂëÊÇ¿ÉÒÔ±»¶à¸öÏß³Ìͬʱ·ÃÎʵ½µÄ¡£




4. ËÀËø£¨deadlock£©
5. wait Óë notify ·½·¨¶¼ÊǶ¨ÒåÔÚ Object ÀàÖУ¬¶øÇÒÊÇ final µÄ£¬Òò´Ë»á±»ËùÓÐµÄ Java
   ÀàËù¼Ì³Ð²¢ÇÒÎÞ·¨ÖØд¡£ÕâÁ½¸ö·½·¨ÒªÇóÔÚµ÷ÓÃʱÏß³ÌÓ¦¸ÃÒѾ­»ñµÃÁ˶ÔÏóµÄËø£¬
   Òò´Ë¶ÔÕâÁ½¸ö·½·¨µÄµ÷ÓÃÐèÒª·ÅÔÚ synchronized ·½·¨»ò¿éµ±ÖС£µ±Ïß³ÌÖ´ÐÐÁË wait
   ·½·¨Ê±£¬Ëü»áÊͷŵô¶ÔÏóµÄËø¡£
6. ÁíÒ»¸ö»áµ¼ÖÂÏß³ÌÔÝÍ£µÄ·½·¨¾ÍÊÇ Thread ÀàµÄ sleep ·½·¨£¬Ëü»áµ¼ÖÂÏß³Ì˯ÃßÖ¸¶¨
   µÄºÁÃëÊý£¬µ«Ïß³ÌÔÚ˯ÃߵĹý³ÌÖÐÊDz»»áÊͷŵô¶ÔÏóµÄËøµÄ¡£

More Related Content

[ʥ˼԰][Java SE]Java se lesson 16

  • 1. Java SE Lesson 16 1. Èç¹ûij¸ö synchronized ·½·¨ÊÇ static µÄ£¬ÄÇôµ±Ï̷߳ÃÎʸ÷½·¨Ê±£¬ËüËøµÄ²¢²»ÊÇ synchronized ·½·¨ËùÔڵĶÔÏ󣬶øÊÇ synchronized ·½·¨ËùÔڵĶÔÏóËù¶ÔÓ¦µÄ Class ¶Ô Ïó£¬ÒòΪ Java ÖÐÎÞÂÛÒ»¸öÀàÓжàÉÙ¸ö¶ÔÏó£¬ÕâЩ¶ÔÏó»á¶ÔӦΨһһ¸ö Class ¶ÔÏó£¬ Òò´Ëµ±Ï̷ֱ߳ð·ÃÎÊͬһ¸öÀàµÄÁ½¸ö¶ÔÏóµÄÁ½¸ö static£¬synchronized ·½·¨Ê±£¬ËûÃÇ µÄÖ´ÐÐ˳ÐòÒ²ÊÇ˳ÐòµÄ£¬Ò²¾ÍÊÇ˵һ¸öÏß³ÌÏÈÈ¥Ö´Ðз½·¨£¬Ö´ÐÐÍê±ÏºóÁíÒ»¸öÏß³Ì ²Å¿ªÊ¼Ö´ÐС£ 2. synchronized ¿é£¬Ð´·¨£º synchronized(object) { } ±íʾÏß³ÌÔÚÖ´ÐеÄʱºò»á¶Ô object ¶ÔÏóÉÏËø¡£ 3. synchronized ·½·¨ÊÇÒ»ÖÖ´ÖÁ£¶ÈµÄ²¢·¢¿ØÖÆ£¬Ä³Ò»Ê±¿Ì£¬Ö»ÄÜÓÐÒ»¸öÏß³ÌÖ´Ðиà synchronized ·½·¨;synchronized ¿éÔòÊÇÒ»ÖÖϸÁ£¶ÈµÄ²¢·¢¿ØÖÆ£¬Ö»»á½«¿éÖеĴúÂëͬ ²½£¬Î»ÓÚ·½·¨ÄÚ¡¢synchronized ¿éÖ®ÍâµÄ´úÂëÊÇ¿ÉÒÔ±»¶à¸öÏß³Ìͬʱ·ÃÎʵ½µÄ¡£ 4. ËÀËø£¨deadlock£© 5. wait Óë notify ·½·¨¶¼ÊǶ¨ÒåÔÚ Object ÀàÖУ¬¶øÇÒÊÇ final µÄ£¬Òò´Ë»á±»ËùÓÐµÄ Java ÀàËù¼Ì³Ð²¢ÇÒÎÞ·¨ÖØд¡£ÕâÁ½¸ö·½·¨ÒªÇóÔÚµ÷ÓÃʱÏß³ÌÓ¦¸ÃÒѾ­»ñµÃÁ˶ÔÏóµÄËø£¬ Òò´Ë¶ÔÕâÁ½¸ö·½·¨µÄµ÷ÓÃÐèÒª·ÅÔÚ synchronized ·½·¨»ò¿éµ±ÖС£µ±Ïß³ÌÖ´ÐÐÁË wait ·½·¨Ê±£¬Ëü»áÊͷŵô¶ÔÏóµÄËø¡£ 6. ÁíÒ»¸ö»áµ¼ÖÂÏß³ÌÔÝÍ£µÄ·½·¨¾ÍÊÇ Thread ÀàµÄ sleep ·½·¨£¬Ëü»áµ¼ÖÂÏß³Ì˯ÃßÖ¸¶¨ µÄºÁÃëÊý£¬µ«Ïß³ÌÔÚ˯ÃߵĹý³ÌÖÐÊDz»»áÊͷŵô¶ÔÏóµÄËøµÄ¡£