简体   繁体   中英

volatile boolean

If I have a volatile boolean (let's call it valid), is the following piece of code thread-safe in Java?

if (valid)
  return;
valid = true;

Or, do I need to synchronize since valid is set to true only if it's false (hence set of valid depends on its current value)?

这需要同步,因为如果一个线程评估有效为false,然后在赋值之前暂停执行,那么另一个线程出现并且还将有效检查为false,在将valid设置为true之前,您将有两个线程运行代码从这里(大概你不想要)。

Use AtomicBoolean. Instances can be simultaneously checked and set.

It isn't thread-safe. But if this is the whole code, it wouldn't matter.

Edit: An all-around superior alternative would be AtomicBoolean , which uses low-level operations to achieve conditional updates without synchronization.

There are two separate (ie non-atomic) accesses to the flag, thus synchronization is necessary unless this thread is the only one doing write operations on the flag. Even then it would probably be good to have synchronization just to be sure in case of future changes.

Your code is not threadsafe, but it really depends on your other code whether or not it is safe to do that.

Do you require that the code following valid = true only be executed once by a single thread? If so, then your code is not safe because any number of threads can read a false value of valid , and then end up setting it to true . For example:

if (valid)
  return;
// Imagine every single one of your threads stops and blocks here.
// They will all wake up again and set valid to true and then
// execute the code to follow.
valid = true;

But if you just want to guarantee that the code after valid = true is executed at most one time by any thread... then what you have is fine. But if that's the behavior you require I would achieve that by other means, because using volatile in that case will just look like you don't know what you're doing. For example, you could just not share the valid variable across threads, and allow each thread to just execute the code once.

Also, when in doubt while reasoning about synchronization and volatile... just use synchronization. It is usually more clear, and will get you everything you wanted from using volatile , except it will be easier to reason how the code works.

thread-safety is a system wide property. you cannot look at a piece of code in isolation and call it thread safe/unsafe. it depends on how other threads interact with it and what is the consistency requirement. that being said, most thread-safe design has while() loop instead of if() block, so, your design is most likely incorrect:)

According to great paper by Brian Goetz https://www.ibm.com/developerworks/java/library/j-jtp06197/ "You can use volatile variables instead of locks only under a restricted set of circumstances. Both of the following criteria must be met for volatile variables to provide the desired thread-safety:

  • Writes to the variable do not depend on its current value.
  • The variable does not participate in invariants with other variables.

Basically, these conditions state that the set of valid values that can be written to a volatile variable is independent of any other program state, including the variable's current state." So it must be synchronized with a lock. It is not thread-safe as is.

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