Does synchronization with access / write to a logical object

This may seem like a very stupid question. Consider this:

I have a simple Boolean object with a getter and setter. Now both methods are called from a large number of threads very often.

  • Do I need to have synchronization for this boolean?
  • Do logical operations with atoms also occur?

[UPDATE]: I already know about Atomic Boolean. I already have many diverse solutions, but I specifically looked for answers and justification for the answers to the above question.

+6
java synchronization boolean
source share
7 answers

No, logical access is NOT atomic (at the machine code level), although it accepts "only 1 operation in Java."

Therefore, yes, you need synchronization for Booleans.

Please see slides 4-6 of this presentation for code examples.

In the corresponding note, you should not synchronize the boolean value

+10
source share
  • Yes. But if it is a flag that is written from only one thread, and you want to ensure visibility for all threads, then the cheap alternative uses volatile .

  • Yes - although the internal representation of the object (that is, the actual boolean flag inside the boolean wrapper) was 64 bits, and therefore it could be "split up" in a parallel situation, the boolean can only have one of two values, right? Thus, simple assignments (get or set) are atomic, but if you do something else (for example, check-then-act), for example x = !x , then of course it is not an atom if it is not synchronized.

+3
source share
  • From a technical point of view, synchronization is not required in order for records in one stream to be perceived in another stream. What you need is what happened to the brim. It is likely that volatile or synchronized will be used to reach the edge “to the start”. Both of these methods lead to edge synchronization. Thus, in practice, you are likely to use synchronization to control the state of your boolean.
  • Yes. Note that you do not change the state of the Boolean object. You only change the reference to the Boolean object. Section 17.7 of the language specification states that "write and read links are always atomic."

Update: let me state the need for it to happen - to the brim. Without an event-to-edge, changes that one thread makes to a variable are not guaranteed to be ever perceived by other threads. It’s not just that change can be perceived at a bad time, for example, between reading and writing. This change can never be perceived.

Let's say that we have a boolean variable that we initialize to false. Then we start two threads. The first thread sets the variable to true and stop. The second thread continuously checks the variable until it returns, after which it stops. There is no guarantee that the second thread will ever see the variable as true.

+3
source share
+2
source share
  • No no. But declare a volatile variable so that the values ​​are reflected in all threads that access the boolean. If you look at the AtomicBoolean set(..) method, it also has no synchronization.

  • Yes, in practice, an assignment is atomic. Just setting the value does not require synchronization. However, if you want to do something like:

     if (!bool) { bool = false; } 

    then you need synchronization (or AtomicBoolean , which is more efficient than synchronization)

+1
source share

http://docs.oracle.com/javase/tutorial/essential/concurrency/atomic.html says that reading and writing primitive variables is atomic.

Therefore, it is possible to ensure strict alternation or occurs after relations using logical (or volatile boolean in case of cache effects)

+1
source share

even if it was atomic, synchronization still exists, since you will surely check the value sometime for example if (boolVar == true) -> another thread takes control do_something ();

0
source share

All Articles