Source reading of synchronizedList of Collections in JDK1.6

ArrayList and LinkedList are both thread-unsafe classes. You can return a thread-safe class through the synchronizedList method of Collections.

Use Cases:

List list = Collections.synchronizedList(new ArrayList()); 

Implementation principle:

        Using the proxy mode, the synchronizedList method of Collections returns a new SynchronizedList instance, which proxy all methods in the List, and all methods are synchronized.

         SynchronizedList类:

static class SynchronizedList<E>
	extends SynchronizedCollection<E>
	implements List<E> {
        static final long serialVersionUID = -7754090372962971524L;

	final List<E> list;

	SynchronizedList(List<E> list) {
	    super(list);
	    this.list = list;
	}
	SynchronizedList(List<E> list, Object mutex) {
            super(list, mutex);
	    this.list = list;
        }

	public boolean equals(Object o) {
	    synchronized(mutex) {return list.equals(o);}
        }
	public int hashCode() {
	    synchronized(mutex) {return list.hashCode();}
        }

	public E get(int index) {
	    synchronized(mutex) {return list.get(index);}
        }
	public E set(int index, E element) {
	    synchronized(mutex) {return list.set(index, element);}
        }
	public void add(int index, E element) {
	    synchronized(mutex) {list.add(index, element);}
        }
	public E remove(int index) {
	    synchronized(mutex) {return list.remove(index);}
        }

	public int indexOf(Object o) {
	    synchronized(mutex) {return list.indexOf(o);}
        }
	public int lastIndexOf(Object o) {
	    synchronized(mutex) {return list.lastIndexOf(o);}
        }

	public boolean addAll(int index, Collection<? extends E> c) {
	    synchronized(mutex) {return list.addAll(index, c);}
        }

	public ListIterator <E> listIterator () {
	    return list.listIterator(); // Must be manually synched by user
        }

	public ListIterator<E> listIterator(int index) {
	    return list.listIterator(index); // Must be manually synched by user
        }

	public List<E> subList(int fromIndex, int toIndex) {
	    synchronized(mutex) {
                return new SynchronizedList<E>(list.subList(fromIndex, toIndex),
                                            mutex);
            }
        }

        Collections inner class SynchronizedCollection:

static class SynchronizedCollection<E> implements Collection<E>, Serializable {
	// use serialVersionUID from JDK 1.2.2 for interoperability
	private static final long serialVersionUID = 3053995032091335093L;

	final Collection<E> c;  // Backing Collection
	final Object mutex; // Object on which to synchronize, synchronization lock, note that it is not locked on this

	SynchronizedCollection(Collection<E> c) {
            if (c==null)
                throw new NullPointerException();
	    this.c = c;
            mutex = this;
        }
	SynchronizedCollection(Collection<E> c, Object mutex) {
	    this.c = c;
            this.mutex = mutex;
        }

	public int size() {
	    synchronized(mutex) {return c.size();}
        }
	public boolean isEmpty() {
	    synchronized(mutex) {return c.isEmpty();}
        }
	public boolean contains(Object o) {
	    synchronized(mutex) {return c.contains(o);}
        }
	public Object[] toArray() {
	    synchronized(mutex) {return c.toArray();}
        }
	public <T> T[] toArray(T[] a) {
	    synchronized(mutex) {return c.toArray(a);}
        }

	public Iterator<E> iterator() {
            return c.iterator(); // Must be manually synched by user!
        }

	public boolean add(E e) {
	    synchronized(mutex) {return c.add(e);}
        }
	public boolean remove(Object o) {
	    synchronized(mutex) {return c.remove(o);}
        }

	public boolean containsAll(Collection<?> coll) {
	    synchronized(mutex) {return c.containsAll(coll);}
        }
	public boolean addAll(Collection<? extends E> coll) {
	    synchronized(mutex) {return c.addAll(coll);}
        }
	public boolean removeAll(Collection<?> coll) {
	    synchronized(mutex) {return c.removeAll(coll);}
        }
	public boolean retainAll(Collection<?> coll) {
	    synchronized(mutex) {return c.retainAll(coll);}
        }
	public void clear() {
	    synchronized(mutex) {c.clear();}
        }
	public String toString() {
	    synchronized(mutex) {return c.toString();}
        }
        private void writeObject(ObjectOutputStream s) throws IOException {
	    synchronized(mutex) {s.defaultWriteObject();}
        }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326073590&siteId=291194637