Clean Code


Calculating the Possible Orderings (continued)



Download 3,58 Mb.
Pdf ko'rish
bet301/384
Sana05.04.2022
Hajmi3,58 Mb.
#530298
1   ...   297   298   299   300   301   302   303   304   ...   384
Bog'liq
Clean Code

Calculating the Possible Orderings (continued)


324
Appendix A: Concurrency II
01: public class Example {
02: int lastId;
03:
04: public void resetId() {
05: value = 0;
06: }
07: 
08: public int getNextId() {
09: ++value;
10: }
11:}
What happens if we change type of 
lastId
from 
int
to 
long
? Is line 5 still atomic?
Not according to the JVM specification. It could be atomic on a particular processor,
but according to the JVM specification, assignment to any 64-bit value requires two
32-bit assignments. This means that between the first 32-bit assignment and the second
32-bit assignment, some other thread could sneak in and change one of the values. 
What about the pre-increment operator, ++, on line 9? The pre-increment operator can
be interrupted, so it is not atomic. To understand, let’s review the byte-code of both of these
methods in detail. 
Before we go any further, here are three definitions that will be important:

Frame—Every method invocation requires a frame. The frame includes the return 
address, any parameters passed into the method and the local variables defined in the 
method. This is a standard technique used to define a call stack, which is used by 
modern languages to allow for basic function/method invocation and to allow for 
recursive invocation.

Local variable—Any variables defined in the scope of the method. All nonstatic meth-
ods have at least one variable,
 
this
, which represents the current object, the object 
that received the most recent message (in the current thread), which caused the 
method invocation.

Operand stack—Many of the instructions in the Java Virtual Machine take parame-
ters. The operand stack is where those parameters are put. The stack is a standard 
last-in, first-out (LIFO) data structure.
Here is the byte-code generated for 
resetId()
:

Download 3,58 Mb.

Do'stlaringiz bilan baham:
1   ...   297   298   299   300   301   302   303   304   ...   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