Java Concurrency (1)
Memory that can be shared betweenthreads is called shared memory or heap memory. The term variable as used inthis technical report refers to both fields and array elements. Variables thatare shared between threads are referred to as shared variables. All instance fields,static fields, and array elements are shared variables allocated in heapmemory. Local variables, formal method parameters, and exception-handlerparameters are never shared between threads and are not affected by the memorymodel.
The visibility of writes toshared variables can be problematic because the value of a shared variable maybe cached and not written to main memory immediately. Consequently, anotherthread may read a stale value of the variable.
A further concern is thatconcurrent executions of code are typically interleaved, and statements may bereordered b the compiler or runtime system to optimize performance. Thisresults in execution orders that are not immediately obvious when the sourcecode is examined. Failure to account for possible reordering is a common sourceof data races.
Volatile accesses do notguarantee the atomicity of composite operations such incrementing a variable.Consequently, volatile is not applicable in cases where the atomicity ofcomposite operations must be guaranteed.
A correctly synchronized programis one whose sequentially consistent executions do not have any data races.
Correct visibility guaranteesthat multiple threads accessing shared data can view each others’ results, butdoes not establish the order of when each thread reads or writes the data.Correct synchronization guarantees that threads access data in a proper order.