Volatile and synchronized usage analysis in Java Multi-threading _java

Source: Internet
Author: User
Tags volatile

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.

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.