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.
erickson
source share