Effective Java


never depend on a finalizer or cleaner to update persistent state



Download 2,19 Mb.
Pdf ko'rish
bet38/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   34   35   36   37   38   39   40   41   ...   341
Bog'liq
Effective Java

never
depend on a finalizer or cleaner to update persistent state.
For example,
depending on a finalizer or cleaner to release a persistent lock on a shared
resource such as a database is a good way to bring your entire distributed system
to a grinding halt.
Don’t be seduced by the methods 
System.gc
and 
System.runFinalization
.
They may increase the odds of finalizers or cleaners getting executed, but they don’t
guarantee it. Two methods once claimed to make this guarantee: 
System.run-
FinalizersOnExit
and its evil twin, 
Runtime.runFinalizersOnExit
. These
methods are fatally flawed and have been deprecated for decades [ThreadStop].
Another problem with finalizers is that an uncaught exception thrown during
finalization is ignored, and finalization of that object terminates [JLS, 12.6].
Uncaught exceptions can leave other objects in a corrupt state. If another thread
attempts to use such a corrupted object, arbitrary nondeterministic behavior may
result. Normally, an uncaught exception will terminate the thread and print a stack
trace, but not if it occurs in a finalizer—it won’t even print a warning. Cleaners do
not have this problem because a library using a cleaner has control over its thread.
There is a 
severe
 performance penalty for using finalizers and cleaners.
On
my machine, the time to create a simple 
AutoCloseable
object, to close it using
try
-with-resources, and to have the garbage collector reclaim it is about 12 ns.
Using a finalizer instead increases the time to 550 ns. In other words, it is about 50
times slower to create and destroy objects with finalizers. This is primarily because
finalizers inhibit efficient garbage collection. Cleaners are comparable in speed to
finalizers if you use them to clean all instances of the class (about 500 ns per
instance on my machine), but cleaners are much faster if you use them only as a
safety net, as discussed below. Under these circumstances, creating, cleaning, and
destroying an object takes about 66 ns on my machine, which means you pay a
factor of five (not fifty) for the insurance of a safety net 
if
you don’t use it.

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   34   35   36   37   38   39   40   41   ...   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