aspose file tools*
The moose likes Threads and Synchronization and the fly likes volatile example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "volatile example" Watch "volatile example" New topic
Author

volatile example

kavan desai
Ranch Hand

Joined: Jan 16, 2005
Posts: 32
Hi,

I am trying to understand the usage of volatile variable. I was trying following code snippet.

<-- code snippet start --->


<-- code snippet end --->


Out put is -->
run method called
Thread 1 : 0
run method called
Test Value : 10
Thread 2 : 0
Test Value : 10
Thread 2 : 1
Test Value : 20
Thread 1 : 1
Test Value : 20
Thread 2 : 2
Test Value : 30
Thread 1 : 2
Test Value : 30
Thread 2 : 3
Thread 1 : 3
Test Value : 40
Test Value : 40
Thread 1 : 4
Test Value : 50
Thread 2 : 4
Test Value : 50
Thread 2 : 5
Test Value : 60
Thread 1 : 5
Test Value : 60
Thread 2 : 6
Thread 1 : 6
Test Value : 70
Test Value : 70
Thread 1 : 7
Test Value : 80
Thread 2 : 7
Test Value : 80
Thread 1 : 8
Test Value : 90
Thread 2 : 8
Test Value : 90
Thread 1 : 9
Test Value : 100
Thread 2 : 9
Test Value : 100



So here each thread is having its own copy of 'testValue' variable, even though I have declared it as volatile. May be because there are two different object are created for class VolatileExample? So how can I make the common between two threads? i.e. One thread to could see updated the variable value updated by other thread ? One more thing I am using java 5.
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Declare testValue as static - static var is shared by objects of the same class.
Or creae only one object of VolatileExample class, and use it in two threads,
for example in this way:
Prashant R Kataria
Greenhorn

Joined: Jul 23, 2007
Posts: 20
Kevan, the code you have written doesn't demonstrate use of volatile.

I am just pasting the explanation over volatile written in Java language specification.

The Java programming language allows threads to access shared variables. As a rule, to ensure that shared variables are consistently and reliably updated, a thread should ensure that it has exclusive use of such variables by obtaining a lock that, conventionally, enforces mutual exclusion for those shared variables.
The Java programming language provides a second mechanism, volatile fields, that is more convenient than locking for some purposes.

A field may be declared volatile, in which case the Java memory model ensures that all threads see a consistent value for the variable. If, in the following example, one thread repeatedly calls the method one (but no more than Integer.MAX_VALUE times in all), and another thread repeatedly calls the method two:


then method two could occasionally print a value for j that is greater than the value of i, because the example includes no synchronization and the shared values of i and j might be updated out of order. One way to prevent this out-or-order behavior would be to declare methods one and two to be synchronized:

This prevents method one and method two from being executed concurrently, and furthermore guarantees that the shared values of i and j are both updated before method one returns. Therefore method two never observes a value for j greater than that for i; indeed, it always observes the same value for i and j.
Another approach would be to declare i and j to be volatile:

This allows method one and method two to be executed concurrently, but guarantees that accesses to the shared values for i and j occur exactly as many times, and in exactly the same order, as they appear to occur during execution of the program text by each thread. Therefore, the shared value for j is never greater than that for i, because each update to i must be reflected in the shared value for i before the update to j occurs. It is possible, however, that any given invocation of method two might observe a value for j that is much greater than the value observed for i, because method one might be executed many times between the moment when method two fetches the value of i and the moment when method
two fetches the value of j.


A compile-time error occurs if a final variable is also declared volatile.


-
Prashant Kataria
SCJP 5.0

"If you are going through hell, keep going!!!"
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

I'm afraid there is a small problem though.

The only thing volatile does is make it so that threads don't maintain their own copies of variables. In a multi-threaded environment, you should *always* synchronize operations that are not atomic.

In the example
i is still able to appear to be larger than j. What if thread one executes i++, it gets preempted, and thread two executes two() before one() resumes again? Volatile does NOT prevent this. The only thing volatile does is that it makes sure that thread two sees the correct value of i after i++ has been executed. Without volatile, or synchronization, thread two may display any old value of i and j, even if thread one has already executed one() a hundred times.
 
jQuery in Action, 2nd edition
 
subject: volatile example