ºÝºÝߣ

ºÝºÝߣ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

Viewers also liked (13)

Heredia, Costa RicaHeredia, Costa Rica
Heredia, Costa Rica
Marisa Moody
?
Row book summaries
Row book summariesRow book summaries
Row book summaries
hmfowler
?
Las normas reguladoras para el desarrollo curricularLas normas reguladoras para el desarrollo curricular
Las normas reguladoras para el desarrollo curricular
Geraldine Prado
?
El diagn¨®stico institucionalEl diagn¨®stico institucional
El diagn¨®stico institucional
wendy-mh
?
Classification
ClassificationClassification
Classification
carissaf
?
Ud reality showsUd reality shows
Ud reality shows
ireguero
?
Uh oh no_tinc_por
Uh oh no_tinc_porUh oh no_tinc_por
Uh oh no_tinc_por
Monprojects .
?
Bitacora Bitacora
Bitacora
ainatodila
?
ÉÌÎñÓ¢Óﺯµç-Project 6
ÉÌÎñÓ¢Óﺯµç-Project 6ÉÌÎñÓ¢Óﺯµç-Project 6
ÉÌÎñÓ¢Óﺯµç-Project 6
mootee
?
Ensayo ustEnsayo ust
Ensayo ust
javieeeera
?
Present perfect continuos
Present perfect continuosPresent perfect continuos
Present perfect continuos
Rey1991
?
Ley de la carrera docenteLey de la carrera docente
Ley de la carrera docente
Adalberto
?
Heredia, Costa RicaHeredia, Costa Rica
Heredia, Costa Rica
Marisa Moody
?
Row book summaries
Row book summariesRow book summaries
Row book summaries
hmfowler
?
Las normas reguladoras para el desarrollo curricularLas normas reguladoras para el desarrollo curricular
Las normas reguladoras para el desarrollo curricular
Geraldine Prado
?
El diagn¨®stico institucionalEl diagn¨®stico institucional
El diagn¨®stico institucional
wendy-mh
?
Classification
ClassificationClassification
Classification
carissaf
?
Ud reality showsUd reality shows
Ud reality shows
ireguero
?
Bitacora Bitacora
Bitacora
ainatodila
?
ÉÌÎñÓ¢Óﺯµç-Project 6
ÉÌÎñÓ¢Óﺯµç-Project 6ÉÌÎñÓ¢Óﺯµç-Project 6
ÉÌÎñÓ¢Óﺯµç-Project 6
mootee
?
Ensayo ustEnsayo ust
Ensayo ust
javieeeera
?
Present perfect continuos
Present perfect continuosPresent perfect continuos
Present perfect continuos
Rey1991
?
Ley de la carrera docenteLey de la carrera docente
Ley de la carrera docente
Adalberto
?

More from ArBing Xie (20)

[ʥ˼԰][Java SE]Java se lesson 3
[ʥ˼԰][Java SE]Java se lesson 3[ʥ˼԰][Java SE]Java se lesson 3
[ʥ˼԰][Java SE]Java se lesson 3
ArBing Xie
?
[ʥ˼԰][Java SE]Swing
[ʥ˼԰][Java SE]Swing[ʥ˼԰][Java SE]Swing
[ʥ˼԰][Java SE]Swing
ArBing Xie
?
[ʥ˼԰][Java SE]Java se lesson 15
[ʥ˼԰][Java SE]Java se lesson 15[ʥ˼԰][Java SE]Java se lesson 15
[ʥ˼԰][Java SE]Java se lesson 15
ArBing Xie
?
[ʥ˼԰][Java SE]Websphere.application.developer(wsad)ʹÓÃÍâÖÃwin cvs½â¾ö·½°¸
[ʥ˼԰][Java SE]Websphere.application.developer(wsad)ʹÓÃÍâÖÃwin cvs½â¾ö·½°¸[ʥ˼԰][Java SE]Websphere.application.developer(wsad)ʹÓÃÍâÖÃwin cvs½â¾ö·½°¸
[ʥ˼԰][Java SE]Websphere.application.developer(wsad)ʹÓÃÍâÖÃwin cvs½â¾ö·½°¸
ArBing Xie
?
[ʥ˼԰][Java SE]Strategy
[ʥ˼԰][Java SE]Strategy[ʥ˼԰][Java SE]Strategy
[ʥ˼԰][Java SE]Strategy
ArBing Xie
?
[ʥ˼԰][Java SE]Reflection
[ʥ˼԰][Java SE]Reflection[ʥ˼԰][Java SE]Reflection
[ʥ˼԰][Java SE]Reflection
ArBing Xie
?
[ʥ˼԰][Java SE]OoadÓëuml lesson 2
[ʥ˼԰][Java SE]OoadÓëuml lesson 2[ʥ˼԰][Java SE]OoadÓëuml lesson 2
[ʥ˼԰][Java SE]OoadÓëuml lesson 2
ArBing Xie
?
[ʥ˼԰][Java SE]Observer
[ʥ˼԰][Java SE]Observer[ʥ˼԰][Java SE]Observer
[ʥ˼԰][Java SE]Observer
ArBing Xie
?
[ʥ˼԰][Java SE]Network
[ʥ˼԰][Java SE]Network[ʥ˼԰][Java SE]Network
[ʥ˼԰][Java SE]Network
ArBing Xie
?
[ʥ˼԰][Java SE]Jdk5
[ʥ˼԰][Java SE]Jdk5[ʥ˼԰][Java SE]Jdk5
[ʥ˼԰][Java SE]Jdk5
ArBing Xie
?
[ʥ˼԰][Java SE]Java se lesson 22
[ʥ˼԰][Java SE]Java se lesson 22[ʥ˼԰][Java SE]Java se lesson 22
[ʥ˼԰][Java SE]Java se lesson 22
ArBing Xie
?
[ʥ˼԰][Java SE]Java se lesson 21
[ʥ˼԰][Java SE]Java se lesson 21[ʥ˼԰][Java SE]Java se lesson 21
[ʥ˼԰][Java SE]Java se lesson 21
ArBing Xie
?
[ʥ˼԰][Java SE]Java se lesson 20
[ʥ˼԰][Java SE]Java se lesson 20[ʥ˼԰][Java SE]Java se lesson 20
[ʥ˼԰][Java SE]Java se lesson 20
ArBing Xie
?
[ʥ˼԰][Java SE]Java se lesson 17
[ʥ˼԰][Java SE]Java se lesson 17[ʥ˼԰][Java SE]Java se lesson 17
[ʥ˼԰][Java SE]Java se lesson 17
ArBing Xie
?
[ʥ˼԰][Java SE]Java se lesson 13
[ʥ˼԰][Java SE]Java se lesson 13[ʥ˼԰][Java SE]Java se lesson 13
[ʥ˼԰][Java SE]Java se lesson 13
ArBing Xie
?
[ʥ˼԰][Java SE]Java se lesson 12
[ʥ˼԰][Java SE]Java se lesson 12[ʥ˼԰][Java SE]Java se lesson 12
[ʥ˼԰][Java SE]Java se lesson 12
ArBing Xie
?
[ʥ˼԰][Java SE]Java se lesson 11
[ʥ˼԰][Java SE]Java se lesson 11[ʥ˼԰][Java SE]Java se lesson 11
[ʥ˼԰][Java SE]Java se lesson 11
ArBing Xie
?
[ʥ˼԰][Java SE]Java se lesson 10
[ʥ˼԰][Java SE]Java se lesson 10[ʥ˼԰][Java SE]Java se lesson 10
[ʥ˼԰][Java SE]Java se lesson 10
ArBing Xie
?
[ʥ˼԰][Java SE]Java se lesson 9
[ʥ˼԰][Java SE]Java se lesson 9[ʥ˼԰][Java SE]Java se lesson 9
[ʥ˼԰][Java SE]Java se lesson 9
ArBing Xie
?
[ʥ˼԰][Java SE]Java se lesson 8
[ʥ˼԰][Java SE]Java se lesson 8[ʥ˼԰][Java SE]Java se lesson 8
[ʥ˼԰][Java SE]Java se lesson 8
ArBing Xie
?
[ʥ˼԰][Java SE]Java se lesson 3
[ʥ˼԰][Java SE]Java se lesson 3[ʥ˼԰][Java SE]Java se lesson 3
[ʥ˼԰][Java SE]Java se lesson 3
ArBing Xie
?
[ʥ˼԰][Java SE]Swing
[ʥ˼԰][Java SE]Swing[ʥ˼԰][Java SE]Swing
[ʥ˼԰][Java SE]Swing
ArBing Xie
?
[ʥ˼԰][Java SE]Java se lesson 15
[ʥ˼԰][Java SE]Java se lesson 15[ʥ˼԰][Java SE]Java se lesson 15
[ʥ˼԰][Java SE]Java se lesson 15
ArBing Xie
?
[ʥ˼԰][Java SE]Websphere.application.developer(wsad)ʹÓÃÍâÖÃwin cvs½â¾ö·½°¸
[ʥ˼԰][Java SE]Websphere.application.developer(wsad)ʹÓÃÍâÖÃwin cvs½â¾ö·½°¸[ʥ˼԰][Java SE]Websphere.application.developer(wsad)ʹÓÃÍâÖÃwin cvs½â¾ö·½°¸
[ʥ˼԰][Java SE]Websphere.application.developer(wsad)ʹÓÃÍâÖÃwin cvs½â¾ö·½°¸
ArBing Xie
?
[ʥ˼԰][Java SE]Strategy
[ʥ˼԰][Java SE]Strategy[ʥ˼԰][Java SE]Strategy
[ʥ˼԰][Java SE]Strategy
ArBing Xie
?
[ʥ˼԰][Java SE]Reflection
[ʥ˼԰][Java SE]Reflection[ʥ˼԰][Java SE]Reflection
[ʥ˼԰][Java SE]Reflection
ArBing Xie
?
[ʥ˼԰][Java SE]OoadÓëuml lesson 2
[ʥ˼԰][Java SE]OoadÓëuml lesson 2[ʥ˼԰][Java SE]OoadÓëuml lesson 2
[ʥ˼԰][Java SE]OoadÓëuml lesson 2
ArBing Xie
?
[ʥ˼԰][Java SE]Observer
[ʥ˼԰][Java SE]Observer[ʥ˼԰][Java SE]Observer
[ʥ˼԰][Java SE]Observer
ArBing Xie
?
[ʥ˼԰][Java SE]Network
[ʥ˼԰][Java SE]Network[ʥ˼԰][Java SE]Network
[ʥ˼԰][Java SE]Network
ArBing Xie
?
[ʥ˼԰][Java SE]Jdk5
[ʥ˼԰][Java SE]Jdk5[ʥ˼԰][Java SE]Jdk5
[ʥ˼԰][Java SE]Jdk5
ArBing Xie
?
[ʥ˼԰][Java SE]Java se lesson 22
[ʥ˼԰][Java SE]Java se lesson 22[ʥ˼԰][Java SE]Java se lesson 22
[ʥ˼԰][Java SE]Java se lesson 22
ArBing Xie
?
[ʥ˼԰][Java SE]Java se lesson 21
[ʥ˼԰][Java SE]Java se lesson 21[ʥ˼԰][Java SE]Java se lesson 21
[ʥ˼԰][Java SE]Java se lesson 21
ArBing Xie
?
[ʥ˼԰][Java SE]Java se lesson 20
[ʥ˼԰][Java SE]Java se lesson 20[ʥ˼԰][Java SE]Java se lesson 20
[ʥ˼԰][Java SE]Java se lesson 20
ArBing Xie
?
[ʥ˼԰][Java SE]Java se lesson 17
[ʥ˼԰][Java SE]Java se lesson 17[ʥ˼԰][Java SE]Java se lesson 17
[ʥ˼԰][Java SE]Java se lesson 17
ArBing Xie
?
[ʥ˼԰][Java SE]Java se lesson 13
[ʥ˼԰][Java SE]Java se lesson 13[ʥ˼԰][Java SE]Java se lesson 13
[ʥ˼԰][Java SE]Java se lesson 13
ArBing Xie
?
[ʥ˼԰][Java SE]Java se lesson 12
[ʥ˼԰][Java SE]Java se lesson 12[ʥ˼԰][Java SE]Java se lesson 12
[ʥ˼԰][Java SE]Java se lesson 12
ArBing Xie
?
[ʥ˼԰][Java SE]Java se lesson 11
[ʥ˼԰][Java SE]Java se lesson 11[ʥ˼԰][Java SE]Java se lesson 11
[ʥ˼԰][Java SE]Java se lesson 11
ArBing Xie
?
[ʥ˼԰][Java SE]Java se lesson 10
[ʥ˼԰][Java SE]Java se lesson 10[ʥ˼԰][Java SE]Java se lesson 10
[ʥ˼԰][Java SE]Java se lesson 10
ArBing Xie
?
[ʥ˼԰][Java SE]Java se lesson 9
[ʥ˼԰][Java SE]Java se lesson 9[ʥ˼԰][Java SE]Java se lesson 9
[ʥ˼԰][Java SE]Java se lesson 9
ArBing Xie
?
[ʥ˼԰][Java SE]Java se lesson 8
[ʥ˼԰][Java SE]Java se lesson 8[ʥ˼԰][Java SE]Java se lesson 8
[ʥ˼԰][Java SE]Java se lesson 8
ArBing Xie
?

[ʥ˼԰][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»»áÊͷŵô¶ÔÏóµÄËøµÄ¡£