1

I've just read a question here, and read the most rated answer by @JB Nizet, and I got confused... According to the answer, in the following code,

private int a=0;

public void foo(){
  int temp=35;
  a=28;
  a=temp;
}

a=28; is an atomic operation.

In some other questions and answers that I've read in Stackoverflow, the information was different, saying that a=28; is not an atomic operation, because first a read operation of the right operand should take place, then the write operation takes place, and each of these 2 operations is atomic, but the entire assignment is not (To be honest, this is how I thought it works).

And what about a=temp; ? Is it any different than a=28; in terms of atomicity?

By the way, I know about the need of volatile for double and long to make read/write to them atomic, just confused about what I wrote above.

Can someone plz elaborate on this?

Thanks..

Community
  • 1
  • 1
JamesJenkins
  • 131
  • 4
  • 4
    You're confusing the *scope* of the word "operation". The assignment to `a` is atomic, because `a` is an `int`. Had it been a `long`, it wouldn't have been an atomic operation. That is all regardless of how the value to assign is obtained. You're right, the *statement* is not atomic, but the *assignment operation* is. – Andreas Mar 20 '17 at 21:32
  • As an aside, in addition to atomicity it's good to be aware of *visibility* between threads. Fun examples here: http://stackoverflow.com/questions/2787094/how-to-demonstrate-java-multithreading-visibility-problems – dnault Mar 20 '17 at 22:19
  • @Andreas, Thanks. I understood what you say, that the actual write to a variable is atomic. It’s just that I see in so many documentations and books that something like a=temp; is atomic, so it confused me. Can I say that a=temp; is not atomic in the sense that if threadA executes a=temp; , between the read of temp and write to a, another threadB could interleave on a, and read the value of a, before it was written by threadA, hence making a=temp; not atomic by definition of atomicity? – JamesJenkins Mar 21 '17 at 15:23
  • 1
    @JamesJenkins You are correct, another thread could execute between read operation and write operation, so assignment *statement* is not atomic. Since "atomic" is usually about *operations*, you should make sure to clarify that you're talking about *statement* when saying it's not atomic, or people will misunderstand you, the way you've been misunderstanding what you've read. – Andreas Mar 21 '17 at 16:05

1 Answers1

6

According to the official documentation:

Reads and writes are atomic for reference variables and for most primitive variables (all types except long and double).

Since a=28; is a write into a primitive which isn't long or double, it's atomic.

However a=temp isn't atomic since it consists of two separate operations - the read from temp and the write to int. Each of these is atomic, but not their composition.

Malt
  • 28,965
  • 9
  • 65
  • 105
  • Thanks. I’ve read the documentation, still got confused. So you say that if I use a literal, like 28 in the example, no other thread could interleave on a=28; ? However, another thread could interleave on a=temp where between the read of temp and write to a, another thread could come in and read the value of a… ? – JamesJenkins Mar 20 '17 at 21:47