Clean Code


Concurrency Defense Principles



Download 3,58 Mb.
Pdf ko'rish
bet177/384
Sana05.04.2022
Hajmi3,58 Mb.
#530298
1   ...   173   174   175   176   177   178   179   180   ...   384
Bog'liq
Clean Code

Concurrency Defense Principles
What follows is a series of principles and techniques for defending your systems from the
problems of concurrent code. 
2. Cosmic-rays, glitches, and so on.
3. See “Digging Deeper” on page 323.
4. See “Possible Paths of Execution” on page 321.


181
Concurrency Defense Principles
Single Responsibility Principle
The SRP
5
states that a given method/class/component should have a single reason to
change. Concurrency design is complex enough to be a reason to change in it’s own right
and therefore deserves to be separated from the rest of the code. Unfortunately, it is all too
common for concurrency implementation details to be embedded directly into other pro-
duction code. Here are a few things to consider:

Concurrency-related code has its own life cycle of development,
change, and tuning.

Concurrency-related code has its own challenges,
which are different from and often 
more difficult than nonconcurrency-related code.
• The number of ways in which miswritten concurrency-based code can fail makes it 
challenging enough without the added burden of surrounding application code.
Recommendation_:__Keep_your_concurrency-related_code_separate_from_other_code_._6_Corollary:_Limit_the_Scope_of_Data'>Recommendation
:
Keep your concurrency-related code separate from other code
.
6
Corollary: Limit the Scope of Data
As we saw, two threads modifying the same field of a shared object can interfere with each
other, causing unexpected behavior. One solution is to use the 
synchronized
keyword to
protect a 
critical section
in the code that uses the shared object. It is important to restrict
the number of such critical sections. The more places shared data can get updated, the
more likely:
• You will forget to protect one or more of those places—effectively breaking all code 
that modifies that shared data.
• There will be duplication of effort required to make sure everything is effectively 
guarded (violation of DRY
7
).
• It will be difficult to determine the source of failures, which are already hard enough 
to find.
Recommendation
:
Take data encapsulation to heart; severely limit the access of any
data that may be shared.

Download 3,58 Mb.

Do'stlaringiz bilan baham:
1   ...   173   174   175   176   177   178   179   180   ...   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