The volatile and synchronized usages in Java multithreading are analyzed in this paper. Share to everyone for your reference. The implementation methods are as follows:
Copy Code code as follows:
Package Com.chzhao;
public class Volatiletest extends Thread {
private static int count = 0;
public void Run () {
count++;
}
public static void Main (string[] args) {
Thread threads[] = new thread[10000];
for (int i = 0; i < threads.length; i++) {
Threads[i] = new Volatiletest ();
}
for (int i = 0; i < threads.length; i++) {
Threads[i].start ();
}
try {
Thread.Sleep (1000);
catch (Interruptedexception e) {
E.printstacktrace ();
}
System.out.println (count);
}
}
Code like, expect output is 10000, then, because count++ is not thread safe, so the output is often less than 10000.
In order to solve this problem, added the volatile keyword.
Copy Code code as follows:
Package Com.chzhao;
public class Volatiletest extends Thread {
private volatile static int count = 0;
public void Run () {
count++;
}
public static void Main (string[] args) {
Thread threads[] = new thread[10000];
for (int i = 0; i < threads.length; i++) {
Threads[i] = new Volatiletest ();
}
for (int i = 0; i < threads.length; i++) {
Threads[i].start ();
}
try {
Thread.Sleep (2000);
catch (Interruptedexception e) {
E.printstacktrace ();
}
System.out.println (count);
}
}
After you modify it, you often output a value that is not 10000.
Modified to synchronized form, the code is as follows:
Copy Code code as follows:
Package Com.chzhao;
public class Synchronizedtest extends Thread {
private static int count = 0;
public void Run () {
Synchronized (Lockclass.lock) {
count++;
}
}
public static void Main (string[] args) {
Thread threads[] = new thread[10000];
for (int i = 0; i < threads.length; i++) {
Threads[i] = new Synchronizedtest ();
}
for (int i = 0; i < threads.length; i++) {
Threads[i].start ();
}
try {
Thread.Sleep (2000);
catch (Interruptedexception e) {
E.printstacktrace ();
}
System.out.println (count);
}
}
Copy Code code as follows:
Package Com.chzhao;
public class Lockclass {
public static byte[] lock = new Byte[0];
}
After this modification, the output is 10000.
Does this mean that the volatile keyword is completely useless? Only synchronized can guarantee thread safety?
Description
The Java language contains two intrinsic synchronization mechanisms: synchronous blocks (or methods) and volatile variables. Both of these mechanisms are proposed to implement the Code thread security. The Volatile variables are less synchronized (but sometimes simpler and less expensive) and are more prone to error. The volatile variable in the Java language can be viewed as a "lighter synchronized", and the volatile variable requires less coding and less run-time overhead than the synchronized block, but the functionality it can achieve is only Part of the synchronized.
That is to say, in some cases, volitile is more convenient than synchronized, of course, the synchronization is even more nearly.
I hope this article will help you with your Java programming.