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

前端 未结 4 1345
借酒劲吻你
借酒劲吻你 2021-02-15 13:00

Reading a few threads (common concurrency problems, volatile keyword, memory model) I\'m confused about concurrency issues in Java.

I have a lot of fields that are acces

相关标签:
4条回答
  • 2021-02-15 13:32

    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 讨论(0)
  • 2021-02-15 13:39

    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.

    0 讨论(0)
  • 2021-02-15 13:48

    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.

    0 讨论(0)
  • 2021-02-15 13:51

    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.

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