简体   繁体   中英

Can volatile variable be defined as static in java?

我可以声明这样的事吗?

static volatile boolean first=false;

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.

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).

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM