Effective Java



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



CHAPTER 11
CONCURRENCY
318
@Override public boolean add(E element) {
boolean added = super.add(element);
if (added)
notifyElementAdded(element);
return added;
}
@Override public boolean addAll(Collection c) {
boolean result = false;
for (E element : c)
result |= add(element);
// Calls notifyElementAdded
return result;
}
}
Observers subscribe to notifications by invoking the 
addObserver
method
and unsubscribe by invoking the 
removeObserver
method. In both cases, an
instance of this 
callback
interface is passed to the method. 
@FunctionalInterface public interface SetObserver {
// Invoked when an element is added to the observable set
void added(ObservableSet set, E element);
}
This interface is structurally identical to 
BiConsumer,E>
. We
chose to define a custom functional interface because the interface and method
names make the code more readable and because the interface could evolve to
incorporate multiple callbacks. That said, a reasonable argument could also be
made for using 
BiConsumer
(Item 44).
On cursory inspection, 
ObservableSet
appears to work fine. For example, the
following program prints the numbers from 
0
through 
99
:
public static void main(String[] args) {
ObservableSet set = 
new ObservableSet<>(new HashSet<>());
set.addObserver((s, e) -> System.out.println(e));
for (int i = 0; i < 100; i++)
set.add(i);
}
Now let’s try something a bit fancier. Suppose we replace the 
addObserver
call with one that passes an observer that prints the 
Integer
value that was added
to the set and removes itself if the value is 
23
:


ITEM 79: AVOID EXCESSIVE SYNCHRONIZATION
319
set.addObserver(new SetObserver<>() {
public void added(ObservableSet s, Integer e) {
System.out.println(e);

Download 2,19 Mb.

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