2. 线程安全性
2.1 什么是线程安全性
线程安全类:当一个类被多个线程访问时,不管运行环境中如何调度,这些线程如何交替执行,并且在调用的代码部分不需要额为的同步或者协同。这个类为线程安全类
Thread-safe classes encapsulate any needed synchronization so that clients need not provide their own.
2.1.1. Example: A Stateless Servlet
Stateless objects are always thread-safe.
2.2 原子性
在有状态的servlet中,对状态变量count在多线程条件下,++count 这个应为原子性操作
The possibility of incorrect results in the presence of unlucky timing is so important in concurrent programming that it has a name: a race condition.
2.2.1 竞态条件
如何产生:当某个正确的结果取决于多个线程的交替执行的时序时,就会发生竞态条件
本质:通过基于一种可能失效的观察结果来做出判断或者执行某个计算
最常见类型:先检测后执行(check-then-act) (星巴克AB会见朋友例子)
2.2.2 示例:延迟初始化的竞态条件
LazyInitRace 如果应用在应用程序的注册表,可能丢失注册信息,或者对同一组注册对象表现不一致视图
@NotThreadSafe
public class LazyInitRace {
private ExpensiveObject instance = null;
public ExpensiveObject getInstance() {
if (instance == null)
instance = new ExpensiveObject();
return instance;
}
}
UnsafeSequence 如果应用在持久化的数据中,会产生不同的对象有相同的id,违反了标识的完整性约束
@NotThreadSafe
public class UnsafeSequence {
private int value;
/** Returns a unique value. */
public int getNext() {
return value++;
}
}
2.2.3 复合操作
像++count,这种“读取-修改-写入”的操作统称为复合操作,复合操作应该是原子性的。
1、通过2.3的加锁机制
2、使用现有线程安全类AtomicLong
Listing 2.4. Servlet that Counts Requests Using AtomicLong.
@ThreadSafe
public class CountingFactorizer implements Servlet {
private final AtomicLong count = new AtomicLong(0);
public long getCount() { return count.get(); }
public void service(ServletRequest req, ServletResponse resp) {
BigInteger i = extractFromRequest(req);
BigInteger[] factors = factor(i);
count.incrementAndGet();
encodeIntoResponse(resp, factors);
}
}
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.
实际中,尽可能使用现有的线程安全类来管理类的状态
2.3 加锁机制
例子:UnsafeCachingFactorizer
To preserve state consistency, update related state variables in a single atomic operation
为了保持状态的一致性,则需在一个原子操作中更新所有的相关的状态变量
2.3.1 内置锁
java提供内置锁机制支持原子性:the synchronized block
these built-in locks are called intrinsic locks or monitor locks
2.3.2 重入
某个线程可以获得已经持有的锁
2.4 用锁来保护状态
对于所有可变的状态,都需要使用同一个锁来保护
如果所有方法都同步,会造成活跃性问题和性能问题
2.5 活跃性和性能
network or console I/O. 不要持有锁
3.对象的共享