1. The role of transient and how to use it
We all know that an object can be serialized as long as the Serilizable interface is implemented, and theJava serialization pattern provides developers with a lot of convenience, and we don't have to relate to the process of specific serialization, as long as this class implements the Serilizable interface, All properties and methods of this class are automatically serialized.
However, in the actual development process, we often encounter such problems, some properties of this class need to serialize, and other attributes do not need to be serialized, for example, if a user has some sensitive information (such as password, bank card number, etc.), for the sake of security, You do not want to be transferred in a network operation (primarily involving serialization operations, where the local serialization cache is also applicable), and the variables corresponding to these information can be added to the transient keyword. In other words, the life cycle of this field is only stored in the caller's memory and is not persisted to disk.
In short, the Java transient keyword is convenient for us, you only need to implement the Serilizable interface, will not need to serialize the property before adding the keyword transient, when serializing the object, this property will not be serialized to the specified destination.
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 from 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 transient keywords use small notes