对象的共享

上一章介绍了如何通过同步来避免多个线程在同一时刻访问相同的数据,而本章将介绍如何共享和发布对象,从而使它们能够安全地由多个线程同时访问。

    • 列同步代码块和同步方法可以确保以原子的方式执行操作,但一种常见的误解是,认为关键字synchronized只能用于实现原子性或者确定“临界区”。同步还有另一重要的方面;内存可见性。
    • 我们不仅希望防止某个线程正在使用对象状态而另一个线程在同时修改该状态,而希望确保当一个线程修改了对象状态之后,其他线程能够看到发生的状态变化。
    • 如果没有同步,那么这种情况就无法实现。

    3.1 可见性

    通常,我们无法确保执行读操作的线程能适时地看到其他线程写入的值。有时甚至是不可能的事情。为了确保多个线程的之间对内存写入操作的可见性,必须使用同步机制。
    /** * NoVisibility * <p/> * Sharing variables without synchronization * * @author Brian Goetz and Tim Peierls */public class NoVisibility {    private static boolean ready;    private static int number;    private static class ReaderThread extends Thread {        public void run() {            while (!ready)                Thread.yield();            System.out.println(number);        }    }    public static void main(String[] args) {        new ReaderThread().start();        number = 42;        ready = true;    }}

    主线程启动读线程,然后将number设为42,并将ready设为true。读线程一直循环知道发现ready的值为true,然后输出number的值。虽然NoVisibility看起来会输出42,但事实上很可能输出0,或者根本无法终止。这是因为在代码中没有使用足够的同步机制,因此无法保证主线程写入的ready值和number值对于读线程来说是可见的。

    3.1.1 失效数据

    查看变脸时,可能会得到一个已经失效的值。
    下方代码中,如果某线程调用了set,那么另一个正在调用get的线程可能会看到更新后的value值,也可能看不到。

    /** * MutableInteger * <p/> * Non-thread-safe mutable integer holder * * @author Brian Goetz and Tim Peierls */@NotThreadSafepublic class MutableInteger {    private int value;    public int get() {        return value;    }    public void set(int value) {        this.value = value;    }}

    下面的SynchronizedInteger 中,通过对get和set等方法进行同步,可以使 MutableInteger 成为一个线程安全的类。

    /** * SynchronizedInteger * <p/> * Thread-safe mutable integer holder * * @author Brian Goetz and Tim Peierls */@ThreadSafepublic class SynchronizedInteger {    @GuardedBy("this") private int value;    public synchronized int get() {        return value;    }    public synchronized void set(int value) {        this.value = value;    }}

    3.1.2 非原子的64位操作

    - 最低安全性:当线程在没有同步的情况下读取变量时,可能会得到一个失效值,但至少这个值是由之前的某个线程设置的值,而不是一个随机值。这种安全性保证也被称之为最低安全性。- 最低安全性适用于绝大多数变量,但是存在一个例外:非volatile类型的64位数值变量(double和long),JVM允许将64位的度操作或写操分解为 两个32位操作。

    3.1.3 加锁和可见性

    加锁的含义不仅仅局限于互斥行为,还包括内存可见性。为了确保所有的线程都能看到共享变量的最新值,所有执行读操作或者写操作的线程都必须在同一个锁上同步。

    3.1.4 Volatile变量

    • Java语言提供了一种稍弱的同步机制,即volatile变量,用来确保将变量的更新操作通知到其它线程。
    • 当把变量声明为volatile类型后,编译器于运行时都会注意到这个变量是共享的,因此不会讲该变量上的操作与其他内存操作一起重排序。volatile变量不会被缓存在寄存器或者对其他处理器不可见的地方,因此在读取volatile类型的变量时总会返回最新写入的值。
    • volatile不会加锁,所以非常轻量级。
    • volatile 不能使用在 验证正确性时需要对可见性进行复杂的判断,那么就不需要使用volatile变量。
    • volatile 的正确使用方式 ,1,确保它们自身状态的可见性。2,确保它们所引用对象状态的可见性,3,以及标识一些重要的程序生命周期事件的发生。

    下面给出volatile 的典型用法。

    /** * CountingSheep * <p/> * Counting sheep * * @author Brian Goetz and Tim Peierls */public class CountingSheep {    volatile boolean asleep;    void tryToSleep() {        while (!asleep)            countSomeSheep();    }    void countSomeSheep() {        // One, two, three...    }}

    当且仅当满足以下所有条件是,才应该使用volatile变量:

    • 对变量的写入操作不依赖变量的当前值,或者你能确保只有单个线程更新变量的值。
    • 该变量不会与其他状态变量一起纳入不变性条件中。
    • 在访问变量时不需要加锁。

    3.2 发布与逸出

    • “发布”一个对象的意思是指,是对象能够在当前作用域之外的代码中使用。
    • “逸出”当某个不应该发布的对象被发布时,被称之为逸出。

    发布一个对象

    /** * Secrets * * Publishing an object * * @author Brian Goetz and Tim Peierls */class Secrets {    public static Set<Secret> knownSecrets;    public void initialize() {        knownSecrets = new HashSet<Secret>();    }}class Secret {}

    使内部的可变状态逸出(不要这么做 )

    /** * UnsafeStates * <p/> * Allowing internal mutable state to escape * * @author Brian Goetz and Tim Peierls */class UnsafeStates {    private String[] states = new String[]{        "AK", "AL" /*...*/    };    public String[] getStates() {        return states;    }}

    最后一种发布的方式, 就是发布一个内部的类实例。

    /**    隐式的使this引用逸出(不要这样做) * ThisEscape * <p/> * Implicitly allowing the this reference to escape * * @author Brian Goetz and Tim Peierls */public class ThisEscape {    public ThisEscape(EventSource source) {        source.registerListener(new EventListener() {            public void onEvent(Event e) {                doSomething(e);            }        });    }    void doSomething(Event e) {    }    interface EventSource {        void registerListener(EventListener e);    }    interface EventListener {        void onEvent(Event e);    }    interface Event {    }}

    使用工厂方法来防止this引用在构造函数中逸出

    /** * SafeListener * <p/> * Using a factory method to prevent the this reference from escaping during construction * * @author Brian Goetz and Tim Peierls */public class SafeListener {    private final EventListener listener;    private SafeListener() {        listener = new EventListener() {            public void onEvent(Event e) {                doSomething(e);            }        };    }    public static SafeListener newInstance(EventSource source) {        SafeListener safe = new SafeListener();        source.registerListener(safe.listener);        return safe;    }    void doSomething(Event e) {    }    interface EventSource {        void registerListener(EventListener e);    }    interface EventListener {        void onEvent(Event e);    }    interface Event {    }}