What is the practical use of volatile / transient modifiers?

Questions by deepti_charolkar

Showing Answers 1 - 9 of 9 Answers

sbarik

  • May 12th, 2007
 

Volatile  comes into use specaiily in multithreaded applications.

And about transient ..In many applications we like to store the stte of the object persistently..but sometimes we want to skip storing of some of the fields of that object. Thats where transient comes in to use.

  Was this answer useful?  Yes

Volatile: In a multithreaded scenario when a shared variable value is modified by Thread1 might  not be immediately updated to main memory. When Thread2 reads, it will get the old value. You can avoid this by specifying the variable with volatile modifier. When we specify the variable as volatile, its value will be synchronized with main copy as and when it modified. This solution is not 100 percent accurate. To get accurate results in multi threaded scenario you can use atomic classes.

Transient: This mdifier is used when we don't want to serialize the value of the member variable when serializing the object.

naresh17201

  • Jul 21st, 2010
 

Transient variables are can not be serialized. So it is used in the serialization process.
In serilization object, some field is marked as transient,will not be transmitted in the byte stream.eg..file handles,database connections etc....such objects are only meaningful locally.

      eg.   class Table implements serializable
               {
                    String color=null;
                   transient File fh=null;
                }
          after serialization, it can be
               class Table implements serializable
              {
                  String color=null;
               }
        I think you understand...
            
In Java, volatile modifier is reserved means can't used as variable names and it is also rarely used.  It is used on instance variables that may be modified simultaneously by other threads. Actually synchronized modifier synchronizes all variables, but volatile modifier only synchronizes the variables marked as volatile.since other threads can't see local variables, there is no need to mark local variables as volatile.
       eg...
                public class myrunnable implemants Runnable
     {
                private volatile boolean active;
                public void run()   //called one thread
                    {
                          active = true;
                           while(active)
                          {
                             //code here
                          }
                     }
                pulic void stop()//called another thread
                        {
                            active=false;
                        }
            }
The loop may not stop when you set active to false

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions