Effective Java



Download 2,19 Mb.
Pdf ko'rish
bet280/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   276   277   278   279   280   281   282   283   ...   341
Bog'liq
Effective Java



CHAPTER 11
CONCURRENCY
314
Note that both the write method (
requestStop
) and the read method (
stop-
Requested
) are synchronized. It is 
not
sufficient to synchronize only the write
method! 
Synchronization is not guaranteed to work unless both read and
write operations are synchronized.
Occasionally a program that synchronizes
only writes (or reads) may 
appear
to work on some machines, but in this case,
appearances are deceiving.
The actions of the synchronized methods in 
StopThread
would be atomic
even without synchronization. In other words, the synchronization on these
methods is used 
solely
for its communication effects, not for mutual exclusion.
While the cost of synchronizing on each iteration of the loop is small, there is a
correct alternative that is less verbose and whose performance is likely to be
better. The locking in the second version of 
StopThread
can be omitted if
stopRequested
is declared volatile. While the 
volatile
modifier performs no
mutual exclusion, it guarantees that any thread that reads the field will see the
most recently written value:

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   276   277   278   279   280   281   282   283   ...   341




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish