Clean Code


Testing Multithreaded Code



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

Testing Multithreaded Code

Sometimes you cannot impose an order on the acquisition of resources. If the ID of 
the second resource comes from an operation performed on the first, then ordering is 
not feasible.
So there are many ways to avoid deadlock. Some lead to starvation, whereas others
make heavy use of the CPU and reduce responsiveness. TANSTAAFL!
5
Isolating the thread-related part of your solution to allow for tuning and experimenta-
tion is a powerful way to gain the insights needed to determine the best strategies.
Testing Multithreaded Code
How can we write a test to demonstrate the following code is broken?
01: public class ClassWithThreadingProblem {
02: int nextId;
03:
04: public int takeNextId() {
05: return nextId++;
06: }
07:}
Here’s a description of a test that will prove the code is broken:

Remember the current value of 
nextId
.

Create two threads, both of which call 
takeNextId()
once.

Verify that 
nextId
is two more than what we started with.

Run this until we demonstrate that 
nextId
was only incremented by one instead 
of two.
Listing A-2 shows such a test:
5.
There ain’t no such thing as a free lunch.
Listing A-2 
ClassWithThreadingProblemTest.java
01: package example;
02: 
03: import static org.junit.Assert.fail;
04: 
05: import org.junit.Test;
06: 
07: public class ClassWithThreadingProblemTest {
08: @Test
09: public void twoThreadsShouldFailEventually() throws Exception {
10: final ClassWithThreadingProblem classWithThreadingProblem 
= new ClassWithThreadingProblem();
11: 


340

Download 3,58 Mb.

Do'stlaringiz bilan baham:
1   ...   311   312   313   314   315   316   317   318   ...   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