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 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 only 1, then volatile is all you need 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.
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.
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.
For this particular use case, see AtomicInteger, rather than using synchronized yourself. – erickson Oct 25 '09 at 18:36 Indeed. I just wanted to demonstrate the pitfalls of blindly using synchronized without understanding exactly what it does and does not provide – bdonlan Oct 25 '09 at 18:46.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.