开发者

If more than one thread can access a field should it be marked as volatile?

开发者 https://www.devze.com 2022-12-09 15:36 出处:网络
Reading a few threads (common concurrency pro开发者_StackOverflow社区blems, volatile keyword, memory model) I\'m confused about concurrency issues in Java.

Reading a few threads (common concurrency pro开发者_StackOverflow社区blems, volatile keyword, memory model) I'm confused about concurrency issues in Java.

I have a lot of fields that are accessed by more than one thread. Should I go through them and mark them all as volatile?

When building a class I'm not aware of whether multiple threads will access it, so surely it is unsafe to let any field not be volatile, so by my understanding there's very few cases you wouldn't use it. Is this correct?

For me this is specific to version 1.5 JVMs and later, but don't feel limited to answering about my specific setup.


Well, you've read those other questions and I presume you've read the answers already, so I'll just highlight some key points:

  1. are they going to change? if not, you don't need volatile
  2. if yes, then is the value of a field related to another? if yes, go to point 4
  3. how many threads will change it? if only 1, then volatile is all you need
  4. if the answer to number 2 is "no" or more than one threads is going to write to it, then volatile alone is not enough, you'll probably need to synchronize the access

Added:
If the field reference an Object, then it will have fields of its own and all those consideration also applies to these fields.


If a field is accessed by multiple threads, it should be volatile or final, or accessed only with synchronized blocks. Otherwise, assigned values may not be visible to other threads.

A class has to be specifically designed for concurrent access by multiple threads. Simply marking fields volatile or final is not sufficient for thread-safety. There are consistency issues (atomicity of changes to multiple fields), concerns about inter-thread signaling (for example using wait and notify), etc.

So, it is safest to assume that an object should be visible to only a single thread unless it is documented otherwise. Making all of your objects thread-safe isn't necessary, and is costly—in terms of software speed, but more importantly, in terms of development expense.

Instead, software should be designed so that concurrent threads interact with each other as little as possible, preferably not at all. The points where they do interact need to be clearly identified so that the proper concurrency controls can be designed.


If you have to ask, use locks. volatile can be useful in some cases, but it's very, very difficult to get right. For example:

class Foo {
  private volatile int counter = 0;
  int Increment() {
    counter++;
    return counter;
  }
}

If two threads run Increment() at the same time, it's possible for the result to be counter = 1. This is because the computer will first retrieve counter, add one, then save it back. Volatile just forces the save and load to occur in a specific order relative to other statements.

Note that synchronized usually obviates the need for volatile - if all accesses to a given field are protected by the same monitor, volatile will never be needed.

Using volatile to make lockless algorithms is very, very difficult; stick to synchronized unless you have hard evidence that it's too slow already, and have done detailed analysis on the algorithm you plan to implement.


The short answer is no. Threading issues require more thought and planning than this. See this for some limitations on when volatile helps for threading and when it does not. The modification of the values has to be properly synchronized, but very typically modification requires the state of more than one variable at a time. Say for example you have variable and you want to change it if it meets a criteria. The read from the array and the write to the array are different instructions, and need to be synchronized together. Volatile is not enough.

Consider also the case where the variable references a mutable object (say an array or a Collection), then interacting with that object will not be thread safe just because the reference is volatile.

0

精彩评论

暂无评论...
验证码 换一张
取 消