Effective Java



Download 2,19 Mb.
Pdf ko'rish
bet286/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   282   283   284   285   286   287   288   289   ...   341
Bog'liq
Effective Java



ITEM 79: AVOID EXCESSIVE SYNCHRONIZATION
321
In fact, there’s a better way to move the alien method invocations out of the
synchronized block. The libraries provide a 
concurrent collection
(Item 81)
known as 
CopyOnWriteArrayList
that is tailor-made for this purpose. This 
List
implementation is a variant of 
ArrayList
in which all modification operations are
implemented by making a fresh copy of the entire underlying array. Because the
internal array is never modified, iteration requires no locking and is very fast. For
most uses, the performance of 
CopyOnWriteArrayList
would be atrocious, but
it’s perfect for observer lists, which are rarely modified and often traversed.
The 
add
and 
addAll
methods of 
ObservableSet
need not be changed if the
list is modified to use 
CopyOnWriteArrayList
. Here is how the remainder of the
class looks. Notice that there is no explicit synchronization whatsoever:
// Thread-safe observable set with CopyOnWriteArrayList
private final List> observers = 
new CopyOnWriteArrayList<>();
public void addObserver(SetObserver observer) {
observers.add(observer);
}
public boolean removeObserver(SetObserver observer) {
return observers.remove(observer);
}
private void notifyElementAdded(E element) {
for (SetObserver observer : observers)
observer.added(this, element);
}
An alien method invoked outside of a synchronized region is known as an
open call
[Goetz06, 10.1.4]. Besides preventing failures, open calls can greatly
increase concurrency. An alien method might run for an arbitrarily long period. If
the alien method were invoked from a synchronized region, other threads would
be denied access to the protected resource unnecessarily.

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   282   283   284   285   286   287   288   289   ...   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