Clean Code



Download 3,58 Mb.
Pdf ko'rish
bet186/384
Sana05.04.2022
Hajmi3,58 Mb.
#530298
1   ...   182   183   184   185   186   187   188   189   ...   384
Bog'liq
Clean Code

Testing Threaded Code
Hand-Coded
You can insert calls to 
wait()
,
sleep()
,
yield()
, and 
priority()
in your code by hand. It
might be just the thing to do when you’re testing a particularly thorny piece of code. 
Here is an example of doing just that:
public synchronized String nextUrlOrNull() {
if(hasNext()) {
String url = urlGenerator.next();
Thread.yield(); // inserted for testing.
updateHasNext();
return url;

return null;
}
The inserted call to 
yield()
will change the execution pathways taken by the code and
possibly cause the code to fail where it did not fail before. If the code does break, it was
not because you added a call to 
yield()
.
17
Rather, your code was broken and this simply
made the failure evident.
There are many problems with this approach:
• You have to manually find appropriate places to do this.
• How do you know where to put the call and what kind of call to use?
• Leaving such code in a production environment unnecessarily slows the code down.
• It’s a shotgun approach. You may or may not find flaws. Indeed, the odds aren’t with you.
What we need is a way to do this during testing but not in production. We also need to
easily mix up configurations between different runs, which results in increased chances of
finding errors in the aggregate.
Clearly, if we divide our system up into POJOs that know nothing of threading and
classes that control the threading, it will be easier to find appropriate places to instrument
the code. Moreover, we could create many different test jigs that invoke the POJOs under
different regimes of calls to 
sleep
,
yield
, and so on. 
Automated
You could use tools like an Aspect-Oriented Framework, CGLIB, or ASM to programmat-
ically instrument your code. For example, you could use a class with a single method:
public class ThreadJigglePoint {
public static void jiggle() {
}
}
17. This is not strictly the case. Since the JVM does not guarantee preemptive threading, a particular algorithm might always 
work on an OS that does not preempt threads. The reverse is also possible but for different reasons.


190

Download 3,58 Mb.

Do'stlaringiz bilan baham:
1   ...   182   183   184   185   186   187   188   189   ...   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