Can volatile variable be defined as static in java?

后端 未结 5 1456
無奈伤痛
無奈伤痛 2021-02-01 04:36

Can I declare something like this??

static volatile boolean first=false;
相关标签:
5条回答
  • 2021-02-01 05:05

    Yes, you can.

    A static variable in Java is stored once per class (not once per object, such as non-static variables are). This means all your objects (and static methods) share the same variable.

    Declaring a variable as volatile (be it static or not) states that the variable will be accessed frequently by multiple threads. In Java, this boils down to instructing threads that they can not cache the variable's value, but will have to write back immediately after mutating so that other threads see the change. (Threads in Java are free to cache variables by default).

    0 讨论(0)
  • 2021-02-01 05:15

    Sure. The effects of the two modifiers are completely orthogonal.

    0 讨论(0)
  • 2021-02-01 05:23

    To expand on Michael's comment.

    static simply means not associated with an instance of the containing class.

    volatile simply means that the value may be changed by other threads without warning.

    So your question boils down to "can a field not associated with an instance of the containing class be changed by another thread without warning?"

    As Michael pointed out, the answer to that question is yes. Instance association is orthogonal to concurrent modification.

    0 讨论(0)
  • 2021-02-01 05:25

    yes, you can.

    static boolean first=false;
    

    When you are declaring a static variable, there will be only one copy for all the objects, no matter how many objects of the class are created. Each thread would have its own cached copy.

    If it is volatile, Thread should go to memory each time and gets the updated value of the variable.

    static volatile boolean first=false;
    

    It will

    force the thread to read each time the memory directly

    to find the value of the variable first.

    0 讨论(0)
  • 2021-02-01 05:28

    As per the question, yes we can but in that case we need to handle all the scenario.

    Access a static value through multiple threads, each thread can have it’s local cached copy! To avoid this you can declare the variable as static volatile and this will force the thread to read each time the global value. However, volatile is not a substitute for proper synchronisation

    private static volatile int cnt= 0;

    private void checkCnt() {
         cnt+= 1;
         // some conditions
         // some code
         cnt -= 1;
    }
    

    Executing checkCnt concurrently many times the final value of cnt different from what we expect! To solve the problem, we’ve to implement a lock to keep track of increment/decrement

    private static final Object lock = new Object();
    private static volatile int cnt= 0;
    
    private void checkCnt() {
         synchronized (lock) {
             cnt += 1;
         }
         //some conditions
         //some code
         synchronized (lock) {
             cnt -= 1;
         }
    }
    

    Not sure is this the right way but by this we can manage the things and use static with volatile. Please provide inputs, if there is better way.

    0 讨论(0)
提交回复
热议问题