对象的组合
注: 在此章之后不会再赘述 原著的内容,只记录一些自己的理解。
设计线程安全的类
- 分析该类的所有域
- 明确这些域的有什么限制条件,即不变性条件。
- 设计访问这些类的线程安全的方法
收集同步需求
- 即确定那些 可能出现 if-else 的情况。
- 并使用 同步机制来确保这些情况的线程安全性。
依赖状态的操作
- 就是包裹在
if
代码块中的操作。 - 设计合理的内置加锁机制比较困难
- 不要重复制作轮子,使用jdk内置的阻塞类库来帮助我们实现。
状态的所有权
- 类封装的属性(状态或域),如果域是
private
的并且没有提供任何形式的public
的get
方法,那么这个类对该状态 享有觉得的所有权、控制权。 - 如果将该属性发布出去,则该类与其他类共享控制权。
实例封闭
- 如果一个对象不是线程安全的,那么就将对它的操作规范起来,并封装在一个类中。
- 可以更加容易的通过封装类的实例的内置锁来守护这个不是线程安全的对象。
package net.jcip.examples;
import java.util.*;
import net.jcip.annotations.*;
/**
* PersonSet
* <p/>
* Using confinement to ensure thread safety
*
* @author Brian Goetz and Tim Peierls
*/
@ThreadSafe
public class PersonSet {
@GuardedBy("this") private final Set<Person> mySet = new HashSet<Person>();
public synchronized void addPerson(Person p) {
mySet.add(p);
}
public synchronized boolean containsPerson(Person p) {
return mySet.contains(p);
}
interface Person {
}
}
java监视器模式
- 对象的所有域的操作都由它的内置锁守护
- 标准的监视器模式应用
package net.jcip.examples;
import net.jcip.annotations.*;
/**
* Counter
* <p/>
* Simple thread-safe counter using the Java monitor pattern
*
* @author Brian Goetz and Tim Peierls
*/
@ThreadSafe
public final class Counter {
@GuardedBy("this") private long value = 0;
public synchronized long getValue() {
return value;
}
public synchronized long increment() {
if (value == Long.MAX_VALUE)
throw new IllegalStateException("counter overflow");
return ++value;
}
}
监视器模式的应用
package net.jcip.examples;
import java.util.*;
import net.jcip.annotations.*;
/**
* MonitorVehicleTracker
* <p/>
* Monitor-based vehicle tracker implementation
*
* @author Brian Goetz and Tim Peierls
*/
@ThreadSafe
public class MonitorVehicleTracker {
@GuardedBy("this") private final Map<String, MutablePoint> locations;
public MonitorVehicleTracker(Map<String, MutablePoint> locations) {
this.locations = deepCopy(locations);
}
public synchronized Map<String, MutablePoint> getLocations() {
return deepCopy(locations);
}
public synchronized MutablePoint getLocation(String id) {
MutablePoint loc = locations.get(id);
return loc == null ? null : new MutablePoint(loc);
}
public synchronized void setLocation(String id, int x, int y) {
MutablePoint loc = locations.get(id);
if (loc == null)
throw new IllegalArgumentException("No such ID: " + id);
loc.x = x;
loc.y = y;
}
private static Map<String, MutablePoint> deepCopy(Map<String, MutablePoint> m) {
Map<String, MutablePoint> result = new HashMap<String, MutablePoint>();
for (String id : m.keySet())
result.put(id, new MutablePoint(m.get(id)));
return Collections.unmodifiableMap(result);
}
}
- 将对线程不安全的对象的操作,封装在
MonitorVehicleTracker
中,并且每个操作都由 this 守护。 - 当需要获取对象值的时候,返回了拷贝对象,保证了线程不安全对象不会逸出。即原始对象不会被非法修改,修改的只会是拷贝对象。
线程安全性的委托
- 将上例使用委托机制改造
package net.jcip.examples;
import net.jcip.annotations.*;
/**
* Point
* <p/>
* Immutable Point class used by DelegatingVehicleTracker
*
* @author Brian Goetz and Tim Peierls
*/
@Immutable
public class Point {
public final int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
package net.jcip.examples;
import java.util.*;
import java.util.concurrent.*;
import java.awt.*;
import java.awt.Point;
import net.jcip.annotations.*;
/**
* DelegatingVehicleTracker
* <p/>
* Delegating thread safety to a ConcurrentHashMap
*
* @author Brian Goetz and Tim Peierls
*/
@ThreadSafe
public class DelegatingVehicleTracker {
private final ConcurrentMap<String, Point> locations;
private final Map<String, Point> unmodifiableMap;
public DelegatingVehicleTracker(Map<String, Point> points) {
locations = new ConcurrentHashMap<String, Point>(points);
unmodifiableMap = Collections.unmodifiableMap(locations);
}
public Map<String, Point> getLocations() {
return unmodifiableMap;
}
public Point getLocation(String id) {
return locations.get(id);
}
public void setLocation(String id, int x, int y) {
if (locations.replace(id, new Point(x, y)) == null)
throw new IllegalArgumentException("invalid vehicle name: " + id);
}
// Alternate version of getLocations (Listing 4.8)
public Map<String, Point> getLocationsAsStatic() {
return Collections.unmodifiableMap(
new HashMap<String, Point>(locations));
}
}
Collections.unmodifiableMap(locations)
这个Map是会实时反映原始Map(locations)的变化的。
独立的状态变量
- 委托机制需要注意的是 各个状态变量互相独立时,把他们委托给线程安全的类的时候是可以保证线程安全性的,但是当状态变量之间存在对应关系,即改变一个,另一个也需要改变的时候,这时候单纯的依靠委托机制是不能保证安全性的。
package net.jcip.examples;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.util.*;
import java.util.concurrent.*;
/**
* VisualComponent
* <p/>
* Delegating thread safety to multiple underlying state variables
*
* @author Brian Goetz and Tim Peierls
*/
public class VisualComponent {
private final List<KeyListener> keyListeners
= new CopyOnWriteArrayList<KeyListener>();
private final List<MouseListener> mouseListeners
= new CopyOnWriteArrayList<MouseListener>();
public void addKeyListener(KeyListener listener) {
keyListeners.add(listener);
}
public void addMouseListener(MouseListener listener) {
mouseListeners.add(listener);
}
public void removeKeyListener(KeyListener listener) {
keyListeners.remove(listener);
}
public void removeMouseListener(MouseListener listener) {
mouseListeners.remove(listener);
}
}
CopyOnWriteArrayList
是一个线程安全的链表。
当委托失效时
package net.jcip.examples;
import java.util.concurrent.atomic.*;
/**
* NumberRange
* <p/>
* Number range class that does not sufficiently protect its invariants
*
* @author Brian Goetz and Tim Peierls
*/
public class NumberRange {
// INVARIANT: lower <= upper
private final AtomicInteger lower = new AtomicInteger(0);
private final AtomicInteger upper = new AtomicInteger(0);
public void setLower(int i) {
// Warning -- unsafe check-then-act
if (i > upper.get())
throw new IllegalArgumentException("can't set lower to " + i + " > upper");
lower.set(i);
}
public void setUpper(int i) {
// Warning -- unsafe check-then-act
if (i < lower.get())
throw new IllegalArgumentException("can't set upper to " + i + " < lower");
upper.set(i);
}
public boolean isInRange(int i) {
return (i >= lower.get() && i <= upper.get());
}
}
- 不变性条件
INVARIANT: lower <= upper
- 两个变量相互存在关系,虽然各自委托给了原子变量
AutomicInteger
管理,但是上述代码并不是线程安全的。
发布底层的状态变量
- 如果该状态变量是线程安全的,并且改变这些状态变量的操作也是线程安全的,那么就可以发布这些变量。
在现有的线程安全的类中添加功能
package net.jcip.examples;
import java.util.*;
import net.jcip.annotations.*;
/**
* BetterVector
* <p/>
* Extending Vector to have a put-if-absent method
*
* @author Brian Goetz and Tim Peierls
*/
@ThreadSafe
public class BetterVector <E> extends Vector<E> {
// When extending a serializable class, you should redefine serialVersionUID
static final long serialVersionUID = -3963416950630760754L;
public synchronized boolean putIfAbsent(E x) {
boolean absent = !contains(x);
if (absent)
add(x);
return absent;
}
}
客户端加锁机制
- 错误的扩展线程安全的类。
- 内置锁的错误理解。
package net.jcip.examples;
import java.util.*;
import net.jcip.annotations.*;
/**
* ListHelder
* <p/>
* Examples of thread-safe and non-thread-safe implementations of
* put-if-absent helper methods for List
*
* @author Brian Goetz and Tim Peierls
*/
@NotThreadSafe
class BadListHelper <E> {
public List<E> list = Collections.synchronizedList(new ArrayList<E>());
public synchronized boolean putIfAbsent(E x) {
boolean absent = !list.contains(x);
if (absent)
list.add(x);
return absent;
}
}
@ThreadSafe
class GoodListHelper <E> {
public List<E> list = Collections.synchronizedList(new ArrayList<E>());
public boolean putIfAbsent(E x) {
synchronized (list) {
boolean absent = !list.contains(x);
if (absent)
list.add(x);
return absent;
}
}
}