java并发编程实践 笔记(1)

线程安全

什么是线程安全(thread-safe)?

一个类,如果能够在多线程并发访问的环境下(不管线程访问的顺序)正确表现自己的行为,并且无需外部调用添加任何同步之类的操作,这个类就是线程安全的。
这个正确性可以这么理解,就是说多线程访问的结果不会不同于单线程的访问。
线程安全的类不需要外部调用提供任何附加的同步。

无状态(stateless)的类永远是线程安全的。
什么是无状态的类?没有实例变量(field),也没有引用其它类的field。

原子性

A race condition occurs when the correctness of a computation depends on the relative timing or interleaving of multiple threads by the runtime; in other words, when getting the right answer relies on lucky timing.

最常见的race condition是check-and-act。要防止这种race condition,我们需要原子性的操作。什么是原子性呢,就是说,你这个操作在执行的过程中,对于你操作的状态上其它的操作(包括你自己)要么全都执行完了,要么还没开始。
想说句通顺的中国话怎么这么难啊,还是给出原文吧:

Operations A and B are atomic with respect to each other if, from the perspective of a thread executing A, when another thread executes B, either all of B has executed or none of it has. An atomic operation is one that is atomic with respect to all operations, including itself, that operate on the same state.

来个例子

@NotThreadSafe
public class UnsafeCountingFactorizer implements Servlet {
    private long count = 0;

    public long getCount() { return count; }

    public void service(ServletRequest req, ServletResponse resp) {
        BigInteger i = extractFromRequest(req);
        BigInteger[] factors = factor(i);
        ++count;
        encodeIntoResponse(resp, factors);
    }

}
@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);
    }
}

Intrinsic Locks

就是synchronized啦,每个java对象都可以作为一个锁,这个叫做intrinsic locks或者是monitor locks

synchronized (lock) {
    // Access or modify shared state guarded by lock
}
Reentancy

synchroinzed获得的锁是可重入的,也就是一个线程获得了锁之后可以再次进入这个锁。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值