The volatile keyword believes that the reader who understands Java multithreading knows its role. The volatile keyword is used to declare simple type variables, such as int, float, Boolean, and so on. If these simple data types are declared as volatile, the operations on them become atomic levels. But there is a certain limit to this. For example, the n in the following example is not an atomic level:
Copy Code code as follows:
Package mythread;
public class Jointhread extends Thread
{
public static volatile int n = 0;
public void Run ()
{
for (int i = 0; i < i++)
Try
{
n = n + 1;
Sleep (3); To make the results more random, delay 3 milliseconds
}
catch (Exception e)
{
}
}
public static void Main (string[] args) throws Exception
{
Thread threads[] = new THREAD[100];
for (int i = 0; i < threads.length; i++)
Create 100 threads
Threads[i] = new Jointhread ();
for (int i = 0; i < threads.length; i++)
Run the 100 threads that you just created
Threads[i].start ();
for (int i = 0; i < threads.length; i++)
100 threads continue after execution
Threads[i].join ();
System.out.println ("n=" + JOINTHREAD.N);
}
}
If the operation of N is atomic, the result of the final output should be n=1000, and when the area code is executed, many times the output of n is less than 1000, indicating that N=n+1 is not an atomic-level operation. The reason is that a simple variable declared as volatile if the current value is related to the previous value of the variable, then the volatile keyword does not work, that is, the following expression is not an atomic operation:
Copy Code code as follows:
If you want to turn this into an atomic operation, you need to use the Synchronized keyword, as the code above can be changed to the following form:
Copy Code code as follows:
Package mythread;
public class Jointhread extends Thread
{
public static int n = 0;
public static synchronized Void Inc ()
{
n++;
}
public void Run ()
{
for (int i = 0; i < i++)
Try
{
Inc. (); n = n + 1 changed to Inc ();
Sleep (3); To make the results more random, delay 3 milliseconds
}
catch (Exception e)
{
}
}
public static void Main (string[] args) throws Exception
{
Thread threads[] = new THREAD[100];
for (int i = 0; i < threads.length; i++)
Create 100 threads
Threads[i] = new Jointhread ();
for (int i = 0; i < threads.length; i++)
Run the 100 threads that you just created
Threads[i].start ();
for (int i = 0; i < threads.length; i++)
100 threads continue after execution
Threads[i].join ();
System.out.println ("n=" + JOINTHREAD.N);
}
}
The
Above code changes n=n+1 to Inc. (), where the INC method uses the Synchronized keyword for method synchronization. Therefore, it is prudent to use the volatile keyword, not as long as the simple type variable using the volatile modifier, all operations on this variable are the original operation, when the value of the variable by its own last decision, such as n=n+1, n++, volatile keyword will be invalidated, The operation of the variable is only atomic if it is independent of its previous value, such as n = m + 1, which is the original level. So in the use of volatile key must be cautious, if they are not sure, you can use synchronized to replace volatile.