Java transient detailed __java those things

Source: Internet
Author: User
Tags class definition serialization volatile

When looking at Java source, suddenly to this piece of unfamiliar, thanks to this article resolving answer confused. Click on the Open link http://www.blogjava.net/fhtdy2004/

Volatile decorated member variables are forced to reread the value of the member variable from main memory each time it is accessed by the thread. Also, when a member variable changes, the thread is forced to write the change value back to the main memory. So at any given moment, two different threads always see the same value for a member variable.

The Java language specification states that in order to achieve optimal speed, the thread is allowed to save private copies of shared member variables, and only when a thread enters or leaves a synchronized code block, it is compared to the original value of the shared member variable.

This way, when multiple threads interact with an object at the same time, it is important to notice that the thread gets the change in the shared member variable in a timely manner.

The volatile keyword is the hint VM: You cannot save its private copy for this member variable, but you should interact directly with the shared member variable.

Recommendation: Use volatile on member variables accessed by two or more threads. You do not have to use when the variable you want to access is already in a synchronized code block, or it is a constant.

Because using volatile masks the necessary code optimizations in the VM, it is less efficient, so use this keyword when necessary.

java key word transient

Transferred from http://horst.sun.blog.163.com/blog/static/348849612007614494492/

Translated from http://www.devx.com/tips/Tip/13726.

The Java serialization provides a mechanism for persisting object instances. When you persist an object, you may have 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 one domain of a particular object, you can precede this 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 the object. When an object is serialized, the value of the transient type variable is not included in the serialized representation, whereas the transient variable is included.
Note that static variables can be serialized as well.

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 () +
"\ Password:" + password;
}
}

Now we create an instance of this class, serialize (serialize) it, and then write this serialized object as 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 is read back from disk (Read--back (de-serializing) is printed password to "not SET". This is the correct result that we expect 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 the transient domain. Suppose we modify the class definition to provide a default value for the Transient field.
The code is as follows:

public class Guestlogginginfo implements

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.