volatile in Java

The volatile modifier applies only to instance variables.

The volatile modifier tells the JVM that a thread accessing the variable must alwasy reconcile its own private copy of the variable with the master copy in memory. i.e it guarantees that any thread that reads the field will see the most recently written value.
Keep in mind : In java if multiple threads using the same variable, each thread will have its own copy of the local cache for that variable.

In multi-treaded programming,  if the variable value updated by one thread, it will update in the local cache of the thread not in the main variable memory. The other thread which is using the same variable doen't know anything about the values changed by the another thread. To overcome this problem, if a variable decalre as volatile, then it will not be stored in the local cache. whenever thread are updating the values, it is updated to the main memory. So other threads can access the updated value.
Figure 1
Figure 2

Figure 1 : Thread 1 and Thread 2 will use same memory of volatile variable, the value will update on same memory.  
Figure 2 : For non volatile variable - each thread will maintain copy of the local cache and other thread won't know the updated value.


  1. can u explain how to implement captcha in java webapplication in
    struts or any

    1. Karthik, check below url for captcha...


Related Posts Plugin for WordPress, Blogger...