If more than one thread can access a field, if it is marked as mutable?

Reading multiple threads ( common concurrency issues , volatile keyword , memory model ) I got confused about concurrency issues in Java.

I have many fields that are accessed by more than one thread. Should I go through them and mark them as unstable?

When creating a class, I don’t know if there will be access to it for several threads, therefore it is unsafe that any field is not unstable, therefore, in my opinion, there are very few cases, Do not use it. Is it correct?

For me, this is typical for JVM versions 1.5 and later, but is not limited to the answer to my specific setup.

+6
java memory atomic volatile
source share
4 answers

If a field is accessed by multiple threads, it must be volatile or final or accessible only with synchronized blocks. Otherwise, the assigned values ​​may not be available for other threads.

The class must be specially designed for simultaneous access by multiple threads. Simple labeling of fields, volatile or terminal, is not sufficient to ensure thread safety. There are problems of consistency (atomic changes in several fields), problems with signaling between threads (for example, using wait and notify ), etc.

Thus, it is safer to assume that an object should be visible to only one thread, unless otherwise documented. Creating all your objects in streaming mode is not necessary and expensive, in terms of software speed, but more importantly, in terms of development costs.

Instead, the software should be designed so that parallel threads interact with each other as little as possible, preferably not at all. The points at which they interact must be clearly identified so that proper concurrency controls can be created.

+3
source share

Ok, you read these other questions, and I suppose you already read the answers, so I just highlighted some key points:

  • are they going to change? if not, you do not need volatility.
  • if so, is that the value of a field associated with another? if yes, go to step 4
  • how many threads will change it? if only 1, then volatility is all you need.
  • if the answer to number 2 is "no" or more than one thread writes to it, then volatility alone is not enough , you probably need to synchronize access li>

Added:
If a field refers to an object, then it will have its own fields, and all these considerations also apply to these fields.

+4
source share

If you need to ask, use locks. volatile may be useful in some cases, but it is 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 is possible that the result will be counter = 1 . This is because the computer first loads counter , adds one, and then saves it. Volatility simply forces you to save and load to be executed in a certain order relative to other operators.

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

Using volatile to create blocking algorithms is very, very difficult; stick to synchronized unless you have convincing evidence that it is too slow, and have carried out a detailed analysis of the algorithm that you plan to implement.

+2
source share

The short answer is no. Threading issues require more thought and planning than this. See this for some limitations when volatile helps for streaming and when not. Modification of values ​​must be correctly synchronized, but a very typical modification requires the state of more than one variable at a time. Say, for example, you have a variable, and you want to change it if it meets the criteria. Reading from an array and writing to an array are different instructions, and they need to be synchronized. Not enough power.

We also consider the case when a variable refers to a variable object (for example, an array or a collection), and then interaction with this object will not be thread safe just because the link is unstable.

+1
source share

All Articles