vjk
vjk

Reputation: 2283

synchronization and visibility

// In Thread1  
x=5;  
synchronization(obj)  
{  
    // do something (no operations involving x)  
}

// In thread 2  
synchronization(obj)  
{  
    // do something(operations involving x)  
}

Is there any requirement that JVM should first execute all the statements before a synchronized block before entering that block. In Thread-1 since synchronized block doesn't have any operations to do with x can it first execute synchronized block and then the assignment operation x=5.

And what would Thread-2 see for the value of x in its synchronized block. Suppose Thread-1 first executes and then Thread-2 executes and both of them are running on the same object, x = 0 when object was created.

We can say that synchronized block in Thread-1 happens before synchronized block in Thread-2. So what should be the value of x in Thread-2 within its synchronized block?

Upvotes: 2

Views: 601

Answers (4)

XuegeGeGe
XuegeGeGe

Reputation: 1

How do you make sure the change on x is visible to Thread 2?

x is not in Thread 1 sync block and I think you didn't make it volatile, so even x assignment happens before Thread 2, Thread 2 may still read the stale value of x.

Upvotes: 0

yshavit
yshavit

Reputation: 43391

In Thread-2, x could be a few things:

  • 0, if Thread-2's synchronized block ran before x = 5 (it could also have actually ran after it, but have a previously cached value of 0 which it's not obligated to update, since there's no happens-before edge between x = 5 and Thread-2)
  • 5, if Thread-2's synchronized block ran after x = 5 (and happened to have had its value flushed) but before Thread-1's synchronized block
  • whatever value Thread-1's synchronized block assigns to x (which in your case doesn't happen, so this option is out)
  • some other value altogether, if x is a long or double, since the words within those types don't have to be atomically updated. (see jls 17.7). Note that in this specific case, that "other value altogether" would be 0 or 5 (if you only saw the top or bottom halves of the long, respectively). But if you had a value with non-0 values for both words, you could see any combination of those words' potential values.

In Thread-1, as JB Nizet points out, within-thread semantics mean that you won't see x as an uninitialized 0 -- it'll either be 5 or whatever Thread-2 sets it to (which of course could be 0).

Upvotes: 0

user845279
user845279

Reputation: 2804

  1. All statements in a single thread are executed sequentially.

  2. What you describe in your second question is a race condition. Thread #1 is assigning the a value to x outside of a synchronized block. Depending on the order of execution Thread #2 could see x = 0 or x = 5;

  3. For your third assumption, x would equal 5.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691943

Is there any requirement that jvm should first execute all the statements before a synchronized bloc before entering that bloc

Yes. http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/package-summary.html says:

Each action in a thread happens-before every action in that thread that comes later in the program's order.

Given that the assignment to x happens-before the first synchronized block execution, and that the first synchronized block execution happens-before the second synchronized block execution, the value assigned to x in the first thread will be visible to the second one. (happens-before is transitive).

Upvotes: 3

Related Questions