📄 collections.java
字号:
public void add(int index, Object o) { synchronized (mutex) { list.add(index, o); } } public boolean addAll(int index, Collection c) { synchronized (mutex) { return list.addAll(index, c); } } public boolean equals(Object o) { synchronized (mutex) { return list.equals(o); } } public Object get(int index) { synchronized (mutex) { return list.get(index); } } public int hashCode() { synchronized (mutex) { return list.hashCode(); } } public int indexOf(Object o) { synchronized (mutex) { return list.indexOf(o); } } public int lastIndexOf(Object o) { synchronized (mutex) { return list.lastIndexOf(o); } } public ListIterator listIterator() { synchronized (mutex) { return new SynchronizedListIterator(mutex, list.listIterator()); } } public ListIterator listIterator(int index) { synchronized (mutex) { return new SynchronizedListIterator(mutex, list.listIterator(index)); } } public Object remove(int index) { synchronized (mutex) { return list.remove(index); } } public Object set(int index, Object o) { synchronized (mutex) { return list.set(index, o); } } public List subList(int fromIndex, int toIndex) { synchronized (mutex) { return new SynchronizedList(mutex, list.subList(fromIndex, toIndex)); } } } // class SynchronizedList /** * The implementation of {@link #synchronizedList(List)} for random-access * lists. This class name is required for compatibility with Sun's JDK * serializability. * * @author Eric Blake <ebb9@email.byu.edu> */ private static final class SynchronizedRandomAccessList extends SynchronizedList implements RandomAccess { /** * Compatible with JDK 1.4. */ private static final long serialVersionUID = 1530674583602358482L; /** * Wrap a given list. * @param l the list to wrap * @throws NullPointerException if l is null */ SynchronizedRandomAccessList(List l) { super(l); } /** * Called only by trusted code to specify the mutex as well as the * collection. * @param sync the mutex * @param l the list */ SynchronizedRandomAccessList(Object sync, List l) { super(sync, l); } public List subList(int fromIndex, int toIndex) { synchronized (mutex) { return new SynchronizedRandomAccessList(mutex, list.subList(fromIndex, toIndex)); } } } // class SynchronizedRandomAccessList /** * The implementation of {@link SynchronizedList#listIterator()}. This * iterator must "sync" on the same object as the list it iterates over. * * @author Eric Blake <ebb9@email.byu.edu> */ private static final class SynchronizedListIterator extends SynchronizedIterator implements ListIterator { /** * The wrapped iterator, stored both here and in the superclass to * avoid excessive casting. */ private final ListIterator li; /** * Only trusted code creates a wrapper, with the specified sync. * @param sync the mutex * @param li the wrapped iterator */ SynchronizedListIterator(Object sync, ListIterator li) { super(sync, li); this.li = li; } public void add(Object o) { synchronized (mutex) { li.add(o); } } public boolean hasPrevious() { synchronized (mutex) { return li.hasPrevious(); } } public int nextIndex() { synchronized (mutex) { return li.nextIndex(); } } public Object previous() { synchronized (mutex) { return li.previous(); } } public int previousIndex() { synchronized (mutex) { return li.previousIndex(); } } public void set(Object o) { synchronized (mutex) { li.set(o); } } } // class SynchronizedListIterator /** * Returns a synchronized (thread-safe) map wrapper backed by the given * map. Notice that element access through the collection views and their * iterators are thread-safe, but if the map can be structurally modified * (adding or removing elements) then you should synchronize around the * iteration to avoid non-deterministic behavior:<br> * <pre> * Map m = Collections.synchronizedMap(new Map(...)); * ... * Set s = m.keySet(); // safe outside a synchronized block * synchronized (m) // synch on m, not s * { * Iterator i = s.iterator(); * while (i.hasNext()) * foo(i.next()); * } * </pre><p> * * The returned Map implements Serializable, but can only be serialized if * the map it wraps is likewise Serializable. * * @param m the map to wrap * @return a synchronized view of the map * @see Serializable */ public static Map synchronizedMap(Map m) { return new SynchronizedMap(m); } /** * The implementation of {@link #synchronizedMap(Map)}. This * class name is required for compatibility with Sun's JDK serializability. * * @author Eric Blake <ebb9@email.byu.edu> */ private static class SynchronizedMap implements Map, Serializable { /** * Compatible with JDK 1.4. */ private static final long serialVersionUID = 1978198479659022715L; /** * The wrapped map. * @serial the real map */ private final Map m; /** * The object to synchronize on. When an instance is created via public * methods, it will be this; but other uses like * SynchronizedSortedMap.subMap() must specify another mutex. Package * visible for use by subclass. * @serial the lock */ final Object mutex; /** * Cache the entry set. */ private transient Set entries; /** * Cache the key set. */ private transient Set keys; /** * Cache the value collection. */ private transient Collection values; /** * Wrap a given map. * @param m the map to wrap * @throws NullPointerException if m is null */ SynchronizedMap(Map m) { this.m = m; mutex = this; if (m == null) throw new NullPointerException(); } /** * Called only by trusted code to specify the mutex as well as the map. * @param sync the mutex * @param m the map */ SynchronizedMap(Object sync, Map m) { this.m = m; mutex = sync; } public void clear() { synchronized (mutex) { m.clear(); } } public boolean containsKey(Object key) { synchronized (mutex) { return m.containsKey(key); } } public boolean containsValue(Object value) { synchronized (mutex) { return m.containsValue(value); } } // This is one of the ickiest cases of nesting I've ever seen. It just // means "return a SynchronizedSet, except that the iterator() method // returns an SynchronizedIterator whose next() method returns a // synchronized wrapper around its normal return value". public Set entrySet() { // Define this here to spare some nesting. class SynchronizedMapEntry implements Map.Entry { final Map.Entry e; SynchronizedMapEntry(Object o) { e = (Map.Entry) o; } public boolean equals(Object o) { synchronized (mutex) { return e.equals(o); } } public Object getKey() { synchronized (mutex) { return e.getKey(); } } public Object getValue() { synchronized (mutex) { return e.getValue(); } } public int hashCode() { synchronized (mutex) { return e.hashCode(); } } public Object setValue(Object value) { synchronized (mutex) { return e.setValue(value); } } public String toString() { synchronized (mutex) { return e.toString(); } } } // class SynchronizedMapEntry // Now the actual code. if (entries == null) synchronized (mutex) { entries = new SynchronizedSet(mutex, m.entrySet()) { public Iterator iterator() { synchronized (super.mutex) { return new SynchronizedIterator(super.mutex, c.iterator()) { public Object next() { synchronized (super.mutex) { return new SynchronizedMapEntry(super.next()); } } }; } } }; } return entries; } public boolean equals(Object o) { synchronized (mutex) { return m.equals(o); } } public Object get(Object key) { synchronized (mutex) { return m.get(key); } } public int hashCode() { synchronized (mutex) { return m.hashCode(); } } public boolean isEmpty() { synchronized (mutex) { return m.isEmpty(); } } public Set keySet() { if (keys == null) synchronized (mutex) { keys = new SynchronizedSet(mutex, m.keySet()); } return keys; } public Object put(Object key, Object value) { synchronized (mutex) { return m.put(key, value); } } public void putAll(Map map) { synchronized (mutex) { m.putAll(map); } } public Object remove(Object o) { synchronized (mutex) { return m.remove(o); } } public int size() { synchronized (mutex) { return m.size(); } } public String toString() { synchronized (mutex) { return m.toString(); } } public Collection values() { if (values == null) synchronized (mutex) { values = new Synch
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -