Effective Java



Download 2,19 Mb.
Pdf ko'rish
bet298/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   294   295   296   297   298   299   300   301   ...   341
Bog'liq
Effective Java



ITEM 83: USE LAZY INITIALIZATION JUDICIOUSLY
335
This code may appear a bit convoluted. In particular, the need for the local
variable (
result
) may be unclear. What this variable does is to ensure that 
field
is read only once in the common case where it’s already initialized. While not
strictly necessary, this may improve performance and is more elegant by the stan-
dards applied to low-level concurrent programming. On my machine, the method
above is about 1.4 times as fast as the obvious version without a local variable.
While you can apply the double-check idiom to static fields as well, there is
no reason to do so: the lazy initialization holder class idiom is a better choice.
Two variants of the double-check idiom bear noting. Occasionally, you may
need to lazily initialize an instance field that can tolerate repeated initialization. If
you find yourself in this situation, you can use a variant of the double-check idiom
that dispenses with the second check. It is, not surprisingly, known as the 
single-
check idiom
. Here is how it looks. Note that 
field
is still declared 
volatile
:

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   294   295   296   297   298   299   300   301   ...   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