Essays-204 comments-134 Articles -0 trackbacks-0 A volatile-modified member variable forces the value of the member variable to be reread from main memory each time it is accessed by the thread. Also, when a member variable changes, the forcing thread writes the change value back to the main memory. So at any moment, two different threads always see the same value for a member variable.
The Java language specification states that for optimal speed, a thread is allowed to save a private copy of a shared member variable, and is compared to the original value of a shared member variable only if the thread enters or leaves the synchronized code block.
this way, when multiple threads interact with an object at the same time, it is important to notice that the thread gets the changes to the shared member variables in a timely manner.
The volatile keyword is a hint to the VM: You cannot save its private copy for this member variable, but you should interact directly with the shared member variable.
Usage Recommendation: Use volatile on member variables accessed by two or more threads. You do not have to use the variable you want to access when it is already in a synchronized code block, or is a constant.
It is inefficient to use volatile to mask the necessary code optimizations in the VM, so use this keyword when necessary.
java keyword transient
Transfer from http://horst.sun.blog.163.com/blog/static/348849612007614494492/
translated from http://www.devx.com/tips/Tip/13726.
Java's serialization provides a mechanism for persisting object instances. When persisting an object, there may be a special object data member that we do not want to
use the serialization mechanism to save it. In order to turn off serialization on a domain of a particular object, you can precede the field with the keyword transient.
transient is a keyword in the Java language that is used to indicate that a domain is not part of the serialization of that object. When an object is serialized, the value of the transient variable is not included in the serialized representation, whereas the non-transient variable is included.
Note that the static variable can also be serialized
first, let's look at some Java serialization code:
Public class LoggingInfo implements Java.io.Serializable
{
Private date Loggingdate = new Date ();
Private String uid;
private transient String pwd;
LoggingInfo (string user, string password)
{
UID = user;
PWD = password;
}
Public String toString ()
{
String Password=null;
if (pwd = = null)
{
Password = "Not SET";
}
Else
{
Password = pwd;
}
Return "Logon info: \ n" + "User:" + UID +
"\ Logging Date:" + loggingdate.tostring () +
"\ n Password:" + password;
}
}
Now we create an instance of this class and serialize (serialize) it, and then write the serialized object like a disk.
LoggingInfo loginfo = new LoggingInfo ("MIKE", "mechanics");
System.out.println (Loginfo.tostring ());
Try
{
ObjectOutputStream o = new ObjectOutputStream (
New FileOutputStream ("Loginfo.out"));
O.writeobject (Loginfo);
O.close ();
}
catch (Exception e){//deal with exception}
To read the object back, we can write
Try
{
ObjectInputStream in =new ObjectInputStream (
New FileInputStream ("Loginfo.out"));
LoggingInfo Loginfo = (logginginfo) in.readobject ();
System.out.println (Loginfo.tostring ());
}
catch (Exception e){//deal with exception}
If we run this code, we will notice that the object that reads back from disk (Read--back (de-serializing)) prints password to "not SET". This is the correct result when we define the PWD domain as transient.
Now, let's take a look at the potential problems that may arise from careless treatment of transient domains. Suppose we modify the class definition to provide a default value to the transient domain,
The code is as follows:
public class Guestlogginginfo implements Java.io.Serializable
{
Private date Loggingdate = new Date ();
Private String uid;
private transient String pwd;
Guestlogginginfo ()
{
UID = "Guest";
PWD = "Guest";
}
Public String toString ()
{
Same as above
}
}
now, if we walk through an instance of Guestlogginginfo, write it to disk, and then read it out of disk, we still see that the object being read back is printed password
"Not SET"
. When an instance of a class is read from disk, the constructor for that class is not actually executed.
Instead, it loads a persisted state of the class object and assigns that state to another object of the class.
Java Volatile transient keyword