Java多线程学习(二)——Thread Safety

Thread Safety

写多线程程序的核心不在于使用锁或者线程,他们都只是工具,真正核心的要点在于管理mutable state.


Whenever more than one thread accesses a given state variable, and one of them might write to it, they all must coordinate their access to it using synchronization.


synchronized: (primary mechanism) provides exclusive locking(互斥锁)

synchronization: exclusive locking, volatile variables, explicit locks, atomic variables


原则:

If multiple threads access the same mutable state variable without appropriate synchronization, your program is broken. There are three ways to fix it.

1. Don't share the state variable across threads.

2. Make the state variable immutable.

3. use synchronization whenever accessing the state variable.


When designing thread-safe classes, good object-oriented techniques--encapsulation, immutability, and clear specification of invariants -- are your best friends.


定义:

    A class is thread-safe if it behaves correctly when accessed from multiple threads, regardless of the scheduling or interleaving of the execution of those threads by the runtime environment, and with no additional synchronization or other coordination on the part of the calling code.


java.util.concurrent.atomic package contains atomic variable classes for effecting atomic state transitions on numbers and object references.


建议:

    Where practical, use existing thread-safe objects, like AtomicLong, to manage your class's state. It is simpler to reason about the possible states and state transitions for existing thread-safe objects than it is for arbitrary state variables, and this makes it easier to maintain and verify thread safety.


Locking

    To preserve state consistency, update related state variables in a single atomic operation.

Intrinsic Locks

    Java provides a built-in locking mechanism for enforcing atomicity: the synchronized block. A synchronized block has two parts: a reference to an object that will serve as the lock, and a block of code to be guarded by that lock. A synchronized method is a shorthand for a synchronized block that spans an entire method body, and whose lock is the object on which the method is being invoked. (Static synchronized methods use the Class object for the lock)


    Every Java object can implicitly act as a lock for purposses of synchronization.

    The lock is automatically acquired by the executing thread before entering a synchronized block and automatically released when control exits the synchronized block, whether by the normal control path or by throwing an exception out of the block.The only way to acquire an intrinsic lock is to enter a synchronized block or method guarded by that lock.

    Intrinsic locks in Java act as mutexes, which means that at most one thread may own the lock.

Reentrancy

Locks are acquired on a per-thread rather than per-invocation basis.( This differes from the default locking behavior for pthreads mutexes, which are granted on a per-invocation basis.)

    

When a thread requests a lock that is already held by another thread, the requesting thread block. But because intrinsic locks arereentrant, if a thread tries to acquire a lock that it already holds, the request succeeds. 

要点:

若不这样做,则当子类重写父类synchronized方法,并call父类方法时,则会产生死锁。


Pitfalls: 

synchronization只需要在写shared variables时使用,这是一个错误的想法,它必须在所有shared variable使用的时候调用。

For each mutable state variable that may be accessed by more than one thread, all accesses to that variable must be performed with the same lock held. In this case, we say that the variable is guarded by that lock.

Every shared mutable variable should be guarded by exactly one lock. Make it clear to maintainers which lock that is.


A common locking convention is to encapsulate all mutable state within an object and to protect it from concurrent access by synchronizing any code path that accesses mutable state using the object's intrinsic lock.(被诸如Vector之类的synchronized collection class使用)


Java中每个object都拥有内置(built-in)锁,你不需要显示创建lock object.(这也许是个坏的设计,因为这不仅有些让人迷惑,而且它迫使JVM实现商在object size和lock performance之间做权衡)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值