Java multithreading guide for beginners (6): exercise caution when using the volatile keyword [transferred from www.bitscn.com]

Source: Internet
Author: User

The volatile keyword is believed to be clearly understood by readers who understand Java multithreading. The volatile keyword is used to declare simple type variables, such as int, float, and boolean data types. If these simple data types are declared as volatile, their operations will change to atomic level. But there are some restrictions. For example, N in the following example is not atomic: Network Management Network bitscn_com

Package mythread;

Public class jointhread extends thread
{
Public static volatile int n = 0;
54ne.com

Public void run ()
{
For (INT I = 0; I <10; I ++)
China Network Management Alliance WWW, bitscn, com

Try
{
N = n + 1;
Sleep (3); // delay of 3 ms for the running result to be more random

}
Catch (exception E)
{
}
}

Public static void main (string [] ARGs) throws exception www.bitscn.com
{

Thread threads [] = new thread [100];
For (INT I = 0; I <threads. length; I ++)
Feedom.net

// Create 100 threads
Threads [I] = new jointhread ();
For (INT I = 0; I <threads. length; I ++) 54com.cn
// Run the 100 threads just created
Threads [I]. Start ();
For (INT I = 0; I <threads. length; I ++) www.bitscn.com
// Continue after all the 100 threads are executed
Threads [I]. Join ();
System. Out. println ("n =" + jointhread. N );
}
}

China Network Management Alliance WWW, bitscn, com

If the operation on N is atomic, the final output result should be n = 1000. When the area code is executed, n is usually less than 1000, this indicates that n = n + 1 is not an atomic operation. The reason is that the simple variable declared as volatile does not work if the current value is related to the previous value of the variable, that is, the following expressions are not atomic operations:

China Network Management Forum bbs.bitscn.com

N = n + 1;
N ++;

China Network Management Forum bbs.bitscn.com

To change this situation to an atomic operation, you need to use the synchronized keyword. The above code can be changed to the following form: feedom.net

Package mythread;

Public class jointhread extends thread
{
Public static int n = 0; feedom.net

Public static synchronized void Inc ()
{
N ++;
}
Public void run ()
{
For (INT I = 0; I <10; I ++)
Network Management Network bitscn_com

Try
{
INC (); // n = n + 1 is changed to Inc ();
Sleep (3); // delay of 3 ms for the running result to be more random

}
Catch (exception E)
{
}
}

Public static void main (string [] ARGs) throws exception 54ne.com
{

Thread threads [] = new thread [100];
For (INT I = 0; I <threads. length; I ++)
China Network Management Forum bbs.bitscn.com

// Create 100 threads
Threads [I] = new jointhread ();
For (INT I = 0; I <threads. length; I ++)
China Network Management Forum bbs.bitscn.com

// Run the 100 threads just created
Threads [I]. Start ();
For (INT I = 0; I <threads. length; I ++)
Feedom.net

// Continue after all the 100 threads are executed
Threads [I]. Join ();
System. Out. println ("n =" + jointhread. N );
}
}
Www.bitscn.com

The code above changes n = n + 1 to Inc (), where the INC method uses the synchronized keyword for method synchronization. Therefore, exercise caution when using the volatile keyword. Instead of modifying a simple type variable with volatile, all operations on this variable are original operations, when the value of a variable is determined by the previous one, such as N = n + 1 and N + + +, the volatile keyword is invalid, the operation on the variable is atomic level only when the value of the variable is irrelevant to the previous value, for example, n = m + 1. This is the original level. Therefore, be cautious when using volatile. If you are not sure about it, you can use synchronized instead of volatile. 54ne.com.

[Transferred from www.bitscn.com]

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.