Volatile in java with long, int, boolean and many different write cases

I have an exam that appears in a parallel programming class. The class is pretty well structured, but I feel that I should not understand the “mutable” keyword as I thought. I read other posts about how this works, and it seems to make sense, but my understanding of Java as a whole limits me. These are T / F practical questions, can anyone answer them and explain why they are true or false? I put my best guesses and explanations in

a. ___ _The int counter principle is used for several threads, where the only actions on the account are reading its value and increasing it. Suffice it to say that quantity is considered volatile.

false, the volatile keyword guarantees that there are no local caches for this variable, but the race condition may still occur because the new value depends on the previous one for count (and therefore the operation is not atomic), so two threads can read the value, increase it and have another race condition when recording. If the counter was not dependent on the previous value (for example, only the identification number that was recorded), this will work.

b. ____ . The amount of a long variable is divided between several threads, where the only counting steps are to read its value and increase it. It is enough to note the account as unstable.

the same as above, except that even if the value is independent of the previous values, it is not thread safe, since it is NEVER an atomic operation, since it takes two write cycles to write and read the upper and lower 32- bit text

with. ____ The boolean variable b, originally false, is split between multiple threads. One particular thread sets its value to true, another thread reads the value. It is sufficient to note b as volatile.

true, a logic element change is atomic and may occur before an interrupt and update occurs for all other threads.

. ____ b . . b true, . b .

true, , .

!

+4
2

. (.. )

. " , 32 " = > , : . , , 64- , Java . .

. ( , " , " )

.

+5

D , T/F. C, , , :

e: Boolean var, false, true, .

: , , ? .

. , , ,... , " t - .

D ? , , T, F.

+2

Source: https://habr.com/ru/post/1531216/


All Articles