Usage of volatile in Java

Source: Internet
Author: User

Usage of volatile in Java

Today, it is very useful to write a thread process and accidentally see the usage of volatile. Generally, if multiple threads collaboratively store and fetch a variable, you generally need to use the synchronized keyword for synchronization, such:
Public class
Mytestthread extends mytest implements runnable {
Private Boolean _ DONE =
False; Public synchronized Boolean getdone ()
{
Return
_ Done;
}
Public synchronized void setdone (Boolean B)

{
_ DONE = B;
}

Public void run (){

Boolean done;
Done = getdone ();
While (! Done)
{
Repaint ();
Try {

Thread. Sleep (100 );
} Catch (interruptedexception IE)
{
Return;
}
}
}
} Or:
Public class mytestthread extends mytest implements runnable
{
Private Boolean _ DONE = false; Public void setdone (Boolean B)
{

Synchronized (this)
{
_ DONE = B;
}

}

Public void run (){
Boolean done;

Synchronized (this)
{
Done = _ done;

}

While (! Done ){
Repaint (
);
Try {
Thread. Sleep (100 );
}
Catch (interruptedexception IE ){
Return;

}
}
}
} However, with the volatile keyword, We can greatly simplify:
Public class mytestthread extends mytest
Implements runnable {
Private volatile Boolean done = false; Public void run (){
While (! Done ){
Repaint (
);
Try {
Thread. Sleep (100 );
}
Catch (interruptedexception IE ){
Return;

}
}
} Public void setdone (Boolean B ){
Done = B;

}
} Related introductions are as follows:
Java specifies that basic loading and storing of Variables
(Random t for long and double variables) is atomic. That means the value of
Variable can't be found in an interim State during the store, nor can it be
Changed in the middle of loading the variable to a register. The setdone ()
Method has only one store operation; therefore, it is atomic. Unfortunately, Java's memory model is a bit more complex. threads are
Allowed to hold the values of variables in local memory (e.g., in a machine
Register). In that case, when one thread changes the value of the variable,
Another thread may not see the changed variable. This is Special true in
Loops that are controlled by a variable (like the done flag that we are using
Terminate the thread): the looping thread may have already loaded the value
The variable into a register and does not necessarily notice when another thread
Changes the variable. One way to solve this problem is to provide setter and getter methods
The variable. We can then simply synchronize access by using the synchronized
Keyword on these methods. However, Java provides a more elegant solution: the volatile keyword. If
Variable is marked as volatile, every time the variable is used it must be read
From Main Memory. Similarly, every time the variable is written, the value must
Be stored in main memory.

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.