Memory Consistency Errors!
!Occur when different threads have inconsistent views of what should be the same data.
!Causes of it:complex and beyond the scope of this tutorial.and fortunately,the programmer does not need a detail understanding of these causes.
!All that is needed is a strategy for avoiding them
!happens-before relationship(the key to avoiding memory consistency errors): a guarantee that memory writes by one specific statement are visible to another specific statement.
!there are several actions that create happens-before relationships(a guarantee):one of them is synchronization
!two actions that create happens-before relationships:
!Thread.start:When a statement invokes Thread.start, every statement that has a happens-before relationship with that statement also has a happens-before relationship with every statement executed by the new thread. The effects of the code that led up to the creation of the new thread are visible to the new thread.
!When a thread terminates and causes a Thread.join in another thread to return, then all the statements executed by the terminated thread have a happens-before relationship with all the statements following the successful join. The effects of the code in the thread are now visible to the thread that performed the join.
!Synchronized Methods:simply add the synchronized keyword to its declaration:
!two effects:
!first,it is not possible for two invocations of synchronized methods on the same object to interleave.
!second,when a synchronized method exits,it automatically establishes a happens-before relationshi with any subsequent invocation of a synchronized method for the same object.this gurantees that changes to the state of the object are visible to all threads.
!Intrinsic Locks and Synchronization(around which synchronization is built)
!when a thread release an intrinsic lock, happens-before relationship is established between that action and any subsequent acquistion of the same lock.
!Synchronized Statements
!the object that provides the intrinsic lock must be specified.
public void addName(String name) {
synchronized(this) {
lastName = name;
nameCount++;
}
nameList.add(name);
}
!needs to avoid synchronizing invocations of other objects' methods. (Invoking other objects' methods from synchronized code can create problems that are described in the section on Liveness.)
! are also useful for improving concurrency with fine-grained synchronization.(????????????)
!Reentrant Synchronization(Allowing a thread to acquire the same lock more than once enables reentrant synchronization.)
!this describes a situation where synchronized code,directly or indirectly,invokes a method that also contains synchronized code,and both sets of code use the same lock.
!Occur when different threads have inconsistent views of what should be the same data.
!Causes of it:complex and beyond the scope of this tutorial.and fortunately,the programmer does not need a detail understanding of these causes.
!All that is needed is a strategy for avoiding them
!happens-before relationship(the key to avoiding memory consistency errors): a guarantee that memory writes by one specific statement are visible to another specific statement.
!there are several actions that create happens-before relationships(a guarantee):one of them is synchronization
!two actions that create happens-before relationships:
!Thread.start:When a statement invokes Thread.start, every statement that has a happens-before relationship with that statement also has a happens-before relationship with every statement executed by the new thread. The effects of the code that led up to the creation of the new thread are visible to the new thread.
!When a thread terminates and causes a Thread.join in another thread to return, then all the statements executed by the terminated thread have a happens-before relationship with all the statements following the successful join. The effects of the code in the thread are now visible to the thread that performed the join.
!Synchronized Methods:simply add the synchronized keyword to its declaration:
!two effects:
!first,it is not possible for two invocations of synchronized methods on the same object to interleave.
!second,when a synchronized method exits,it automatically establishes a happens-before relationshi with any subsequent invocation of a synchronized method for the same object.this gurantees that changes to the state of the object are visible to all threads.
!Intrinsic Locks and Synchronization(around which synchronization is built)
!when a thread release an intrinsic lock, happens-before relationship is established between that action and any subsequent acquistion of the same lock.
!Synchronized Statements
!the object that provides the intrinsic lock must be specified.
public void addName(String name) {
synchronized(this) {
lastName = name;
nameCount++;
}
nameList.add(name);
}
!needs to avoid synchronizing invocations of other objects' methods. (Invoking other objects' methods from synchronized code can create problems that are described in the section on Liveness.)
! are also useful for improving concurrency with fine-grained synchronization.(????????????)
!Reentrant Synchronization(Allowing a thread to acquire the same lock more than once enables reentrant synchronization.)
!this describes a situation where synchronized code,directly or indirectly,invokes a method that also contains synchronized code,and both sets of code use the same lock.