Effective Java



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



CHAPTER 11
CONCURRENCY
334
Both of these idioms (
normal initialization
and 
lazy initialization with a
synchronized accessor
) are unchanged when applied to static fields, except that
you add the 
static
modifier to the field and accessor declarations.
If you need to use lazy initialization for performance on a static field, use
the 
lazy initialization holder class idiom
.
This idiom exploits the guarantee that a
class will not be initialized until it is used [JLS, 12.4.1]. Here’s how it looks:
// Lazy initialization holder class idiom for static fields
private static class FieldHolder {
static final FieldType field = computeFieldValue();
}
private static FieldType getField() { return FieldHolder.field; }
When 
getField
is invoked for the first time, it reads 
FieldHolder.field
for
the first time, causing the initialization of the 
FieldHolder
class. The beauty of
this idiom is that the 
getField
method is not synchronized and performs only a
field access, so lazy initialization adds practically nothing to the cost of access. A
typical VM will synchronize field access only to initialize the class. Once the class
is initialized, the VM patches the code so that subsequent access to the field does
not involve any testing or synchronization.

Download 2,19 Mb.

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