Memory Consistency Errors

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.
     
 
        
           
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值