Clean Code


• Where there are shared objects/values •



Download 3,58 Mb.
Pdf ko'rish
bet304/384
Sana05.04.2022
Hajmi3,58 Mb.
#530298
1   ...   300   301   302   303   304   305   306   307   ...   384
Bog'liq
Clean Code


Where there are shared objects/values

The code that can cause concurrent read/update issues

How to guard such concurrent issues from happening
As demonstrated in the 
ExecutorClientScheduler.java
on page 321, the 
Executor
frame-
work introduced in Java 5 allows for sophisticated execution using thread pools. This is a
class in the 
java.util.concurrent
package.
If you are creating threads and are not using a thread pool or 
are
using a hand-written
one, you should consider using the 
Executor
. It will make your code cleaner, easier to fol-
low, and smaller.
The 
Executor
framework will pool threads, resize automatically, and recreate threads
if necessary. It also supports 
futures,
a common concurrent programming construct. The
Executor
framework works with classes that implement 
Runnable
and also works with
classes that implement the 
Callable
interface. A 
Callable
looks like a 
Runnable
, but it can
return a result, which is a common need in multithreaded solutions.

future
is handy when code needs to execute multiple, independent operations and
wait for both to finish:
public String processRequest(String message) throws Exception {
Callable makeExternalCall = new Callable() {


327
Knowing Your Library
public String call() throws Exception {
String result = "";
// make external request
return result;
}
};
Future result = executorService.submit(makeExternalCall);
String partialResult = doSomeLocalProcessing();
return result.get() + partialResult;
}
In this example, the method starts executing the 
makeExternalCall
object. The method con-
tinues other processing. The final line calls 
result.get()
, which blocks until the future
completes.

Download 3,58 Mb.

Do'stlaringiz bilan baham:
1   ...   300   301   302   303   304   305   306   307   ...   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