Clean Code


Appendix A Concurrency II



Download 3,58 Mb.
Pdf ko'rish
bet295/384
Sana05.04.2022
Hajmi3,58 Mb.
#530298
1   ...   291   292   293   294   295   296   297   298   ...   384
Bog'liq
Clean Code

Appendix A
Concurrency II
by Brett L. Schuchert
This appendix supports and amplifies the 
Concurrency
chapter on page 177. It is written
as a series of independent topics and you can generally read them in any order. There is
some duplication between sections to allow for such reading.
Client/Server Example
Imagine a simple client/server application. A server sits and waits listening on a socket for
a client to connect. A client connects and sends a request.
The Server
Here is a simplified version of a server application. Full source for this example is avail-
able starting on page 343, 
Client/Server Nonthreaded
.
ServerSocket serverSocket = new ServerSocket(8009);
while (keepProcessing) {
try {
Socket socket = serverSocket.accept();
process(socket);
} catch (Exception e) {
handle(e);
}
}


318
Appendix A: Concurrency II
This simple application waits for a connection, processes an incoming message, and then
again waits for the next client request to come in. Here’s client code that connects to this
server:
private void connectSendReceive(int i) {
try {
Socket socket = new Socket("localhost", PORT);
MessageUtils.sendMessage(socket, Integer.toString(i));
MessageUtils.getMessage(socket);
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
How well does this client/server pair perform? How can we formally describe that perfor-
mance? Here’s a test that asserts that the performance is “acceptable”:
@Test(timeout = 10000)
public void shouldRunInUnder10Seconds() throws Exception {
Thread[] threads = createThreads();
startAllThreadsw(threads);
waitForAllThreadsToFinish(threads);
}
The setup is left out to keep the example simple (see “
ClientTest.java
” on page 344). This
test asserts that it should complete within 10,000 milliseconds.
This is a classic example of validating the throughput of a system. This system should
complete a series of client requests in ten seconds. So long as the server can process each
individual client request in time, the test will pass.
What happens if the test fails? Short of developing some kind of event polling loop,
there is not much to do within a single thread that will make this code any faster. Will
using multiple threads solve the problem? It might, but we need to know where the time is
being spent. There are two possibilities:

Download 3,58 Mb.

Do'stlaringiz bilan baham:
1   ...   291   292   293   294   295   296   297   298   ...   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