Collections.SynchronizedList<E>

本文深入解析了SynchronizedList类的设计原理及其实现细节,重点介绍了如何通过加锁机制确保线程安全,包括get方法在内的核心操作的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、类图

2、源码

    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) {
            if (this == o)
                return true;
	    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);
            }
        }

        /**
         * SynchronizedRandomAccessList instances are serialized as
         * SynchronizedList instances to allow them to be deserialized
         * in pre-1.4 JREs (which do not have SynchronizedRandomAccessList).
         * This method inverts the transformation.  As a beneficial
         * side-effect, it also grafts the RandomAccess marker onto
         * SynchronizedList instances that were serialized in pre-1.4 JREs.
         *
         * Note: Unfortunately, SynchronizedRandomAccessList instances
         * serialized in 1.4.1 and deserialized in 1.4 will become
         * SynchronizedList instances, as this method was missing in 1.4.
         */
        private Object readResolve() {
            return (list instanceof RandomAccess
		    ? new SynchronizedRandomAccessList<E>(list)
		    : this);
        }
    }


3、线程安全

	public E get(int index) {
	    synchronized(mutex) {return list.get(index);}
  
以get方法为例,通过在成员变量mutex上设置锁来保证方法的线程安全性。
mutex的来源分为两种:
1、从构造方法中传入:
	SynchronizedList(List<E> list, Object mutex) {
            super(list, mutex);
	    this.list = list;
        }
2、不传入,使用父类SynchronizedCollection<E>中默认变量mutex.

SynchronizedCollection<E>默认变量为自身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;
        }

因此,无论是否传入mutex均能保证SynchronizedList<E>在同一个mutex上实现同步,保证线程安全。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值