Volatile in Java does not guarantee thread safety (example explained)

Source: Internet
Author: User

Volatile in Java does not guarantee thread safety (example explained)

Reprinted 2017-09-04 Think-007 I want to comment

The following small series to bring you a piece of Java in the volatile can not guarantee thread safety (the example explained). Small series feel very good, now share to everyone, also for everyone to make a reference. Follow the small part together to look at it today hit the code to see if the Java volatile keyword can guarantee thread safety, after practice, volatile is not guaranteed to be thread-safe, it just guarantees the visibility of the data, no more cache, each thread is read from main memory data, Instead of reading the data from the cache, attach the code as follows, and when the synchronized is removed, the result of each thread is messy, plus the result is correct.
1234567891011121314151617181920212223242526272829303132333435 /** *  * 类简要描述 *  * <p> * 类详细描述 * </p> *  * @author think *  */public class VolatileThread implements Runnable { private volatile int a = 0; @Override public void run() {  // TODO Auto-generated method stub//  synchronized (this) {   a = a + 1;   System.out.println(Thread.currentThread().getName() + ":----" + a);   try {    Thread.sleep(100);    a = a + 2;   } catch (InterruptedException e) {    // TODO Auto-generated catch block    e.printStackTrace();   }   System.out.println(Thread.currentThread().getName() + ":----" + a);//  } }}
?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 /** *  * 类简要描述 *  * <p> * 类详细描述 * </p> *  * @author think *  */ public class VolatileMain { public static void main(String[] args) {  VolatileThread s = new VolatileThread();  Thread t1 = new Thread(s);  Thread t2 = new Thread(s);  Thread t3 = new Thread(s);  Thread t4 = new Thread(s);  t1.start();  t2.start();  t3.start();  t4.start();    /*  同步的结果  Thread-2:----1  Thread-2:----3  Thread-0:----4  Thread-0:----6  Thread-3:----7  Thread-3:----9  Thread-1:----10  Thread-1:----12*/  /*    去掉同步的结果  Thread-0:----1  Thread-1:----2  Thread-2:----3  Thread-3:----4  Thread-0:----8  Thread-3:----10  Thread-1:----10  Thread-2:----12*/   }}

The above this article in Java volatile does not guarantee thread safety (example) is the small part of the whole content to share to everyone, I hope to give you a reference, but also hope that we support the script house a lot.

Volatile in Java does not guarantee thread safety (example explained)

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.