Volatile and Atomics in Java

Source: Internet
Author: User

Volatile

Volatile ensure visibility of variables in multithreading. Used to modify variables.

The code is as follows:

public class VolatileDemo1 {volatile int a;public int Geta () {return A;} public void SetA (int a) {this.a = A;} public void testA1 (final VolatileDemo1 VD) {thread thread = new Thread () {public void run () {for (int i=1; i<5; i++) {VD. SetA (i); System.out.println ("I was modified to:" + Vd.geta ());}}; Thread.Start ();} public void testA2 (final VolatileDemo1 VD) {thread thread = new Thread () {public void run () {System.out.println ("I Get the value:" + Vd.geta ());}; Thread.Start ();} public static void Main (string[] args) {VolatileDemo1 vd1 = new VolatileDemo1 (); vd1.testa1 (VD1); Vd1.testa2 (VD1);}}

Atomics
Import Java.util.concurrent.timeunit;import Java.util.concurrent.atomic.atomicinteger;public class AtomicsDemo { Private Atomicinteger Atomiccount = new Atomicinteger ();p rivate int count = 0;public int increase () {synchronized (this) { Count = Count + 1;return count;}} public int decrease () {synchronized (this) {count = Count-1;return count;}} public int atomicincrease () {return atomiccount.incrementandget ();} public int atomicdecrease () {return atomiccount.decrementandget ();} public static void Main (string[] args) throws Interruptedexception {Long startTime = System.currenttimemillis (); final Ato Micsdemo ad = new Atomicsdemo (), for (int i=0, i<1000; i++) {thread thread = new Thread () {public void run () {Ad.increase ();//ad.atomicincrease ();}; Thread.Start ();} TimeUnit.SECONDS.sleep (3); System.out.println ("Time:" + (System.currenttimemillis ()-startTime)); System.out.println ("Count size:" + ad.count); System.out.println ("Atomiccount size:" + ad.atomiccount); System.out.println ("========="); Long StarttiMe2 = System.currenttimemillis ();//final atomicsdemo ad = new Atomicsdemo (); for (int i=0; i<1000; i++) {Thread thread = New Thread () {public void run () {//ad.increase (); Ad.atomicincrease ();}}; Thread.Start ();} TimeUnit.SECONDS.sleep (3); System.out.println ("Time:" + (System.currenttimemillis ()-startTime2)); System.out.println ("Count size:" + ad.count); System.out.println ("Atomiccount size:" + Ad.atomiccount);}}



Volatile and Atomics in Java

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.