Clean Code


Appendix A: Concurrency II



Download 3,58 Mb.
Pdf ko'rish
bet316/384
Sana05.04.2022
Hajmi3,58 Mb.
#530298
1   ...   312   313   314   315   316   317   318   319   ...   384
Bog'liq
Clean Code

Appendix A: Concurrency II
12: Runnable runnable = new Runnable() {
13: public void run() {
14: classWithThreadingProblem.takeNextId();
15: }
16: };
17: 
18: for (int i = 0; i < 50000; ++i) {
19: int startingId = classWithThreadingProblem.lastId;
20: int expectedResult = 2 + startingId;
21: 
22: Thread t1 = new Thread(runnable);
23: Thread t2 = new Thread(runnable);
24: t1.start();
25: t2.start();
26: t1.join();
27: t2.join();
28: 
29: int endingId = classWithThreadingProblem.lastId;
30: 
31: if (endingId != expectedResult)
32: return;
33: }
34: 
35: fail("Should have exposed a threading issue but it did not.");
36: }
37: }
Line
Description
10
Create a single instance of 
ClassWithThreadingProblem
. Note, we must use
the final keyword because we use it below in an anonymous inner class.
12–16
Create an anonymous inner class that uses the single instance of
ClassWithThreadingProblem
.
18
Run this code “enough” times to demonstrate that the code failed, but not
so much that the test “takes too long.” This is a balancing act; we don’t
want to wait too long to demonstrate failure. Picking this number is hard—
although later we’ll see that we can greatly reduce this number.
19
Remember the starting value. This test is trying to prove that the code in
ClassWithThreadingProblem
is broken. If this test passes, it proved that the
code was broken. If this test fails, the test was unable to prove that the code
is broken.
20
We expect the final value to be two more than the current value.
22–23
Create two threads, both of which use the object we created in lines 12–16.
This gives us the potential of two threads trying to use our single instance
of 
ClassWithThreadingProblem
and interfering with each other.

Download 3,58 Mb.

Do'stlaringiz bilan baham:
1   ...   312   313   314   315   316   317   318   319   ...   384




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