What is the volatile keyword useful for?

前端 未结 23 2594
闹比i
闹比i 2020-11-21 05:32

At work today, I came across the volatile keyword in Java. Not being very familiar with it, I found this explanation.

Given the detail in which that arti

23条回答
  •  有刺的猬
    2020-11-21 05:59

    volatile only guarantees that all threads, even themselves, are incrementing. For example: a counter sees the same face of the variable at the same time. It is not used instead of synchronized or atomic or other stuff, it completely makes the reads synchronized. Please do not compare it with other java keywords. As the example shows below volatile variable operations are also atomic they fail or succeed at once.

    package io.netty.example.telnet;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Main {
    
        public static volatile  int a = 0;
        public static void main(String args[]) throws InterruptedException{
    
            List list = new  ArrayList();
            for(int i = 0 ; i<11 ;i++){
                list.add(new Pojo());
            }
    
            for (Thread thread : list) {
                thread.start();
            }
    
            Thread.sleep(20000);
            System.out.println(a);
        }
    }
    class Pojo extends Thread{
        int a = 10001;
        public void run() {
            while(a-->0){
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Main.a++;
                System.out.println("a = "+Main.a);
            }
        }
    }
    

    Even you put volatile or not results will always differ. But if you use AtomicInteger as below results will be always same. This is same with synchronized also.

        package io.netty.example.telnet;
    
        import java.util.ArrayList;
        import java.util.List;
        import java.util.concurrent.atomic.AtomicInteger;
    
        public class Main {
    
            public static volatile  AtomicInteger a = new AtomicInteger(0);
            public static void main(String args[]) throws InterruptedException{
    
                List list = new  ArrayList();
                for(int i = 0 ; i<11 ;i++){
                    list.add(new Pojo());
                }
    
                for (Thread thread : list) {
                    thread.start();
                }
    
                Thread.sleep(20000);
                System.out.println(a.get());
    
            }
        }
        class Pojo extends Thread{
            int a = 10001;
            public void run() {
                while(a-->0){
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Main.a.incrementAndGet();
                    System.out.println("a = "+Main.a);
                }
            }
        }
    

提交回复
热议问题