转载

CopyOnWriteArrayList再续

相关文章

http://wangxinchun.iteye.com/blog/1872212

CopyOnWriteArrayList 适合读频繁,写不频繁的情况,最重要的是如果list中的数据量非常大,请不要使用这个,那在写的时候复制到代价将会非常大。

这时可以考虑使用Vector 或者collections.synchronizedlist 。

但是这样读和读之间依然是互斥的,性能肯定差。

下面这个实现通过读写锁分离的思想,很简单的可以实现读和读是并行的,读和写是互斥的,写和写是互斥的。

如果有问题,请指正~

源码如下:

package cn.javaframe.validator;  import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;  /**  * @author xinchun.wang  */ public class ReadWriteLockArrayList<T> {  private List<T> list = new ArrayList<T>();  private ReadWriteLock lock = new ReentrantReadWriteLock();  private Lock writeLock = lock.writeLock();  private Lock readLock = lock.readLock();   public int size() {   readLock.lock();   try {    return list.size();   } finally {    readLock.unlock();   }  }   public boolean isEmpty() {   readLock.lock();   try {    return list.isEmpty();   } finally {    readLock.unlock();   }  }   public boolean contains(Object o) {   readLock.lock();   try {    return list.contains(o);   } finally {    readLock.unlock();   }  }   public Iterator<T> iterator() {   throw new UnsupportedOperationException();  }   public Object[] toArray() {   readLock.lock();   try {    return list.toArray();   } finally {    readLock.unlock();   }  }   public boolean add(T e) {   writeLock.lock();   try {    return list.add(e);   } finally {    writeLock.unlock();   }  }   public boolean remove(T o) {   writeLock.lock();   try {    return list.remove(o);   } finally {    writeLock.unlock();   }  }   public void clear() {   writeLock.lock();   try {    list.clear();   } finally {    writeLock.unlock();   }  }   public T get(int index) {   readLock.lock();   try {    return list.get(index);   } finally {    readLock.unlock();   }   }   public T remove(int index) {   writeLock.lock();   try {    return list.remove(index);   } finally {    writeLock.unlock();   }  }   public int indexOf(Object o) {   readLock.lock();   try {    return list.indexOf(o);   } finally {    readLock.unlock();   }   }   public int lastIndexOf(Object o) {   readLock.lock();   try {    return list.lastIndexOf(o);   } finally {    readLock.unlock();   }  }   public boolean addAll(Collection<? extends T> c) {   writeLock.lock();   try {    return list.addAll(c);   } finally {    writeLock.unlock();   }  }   public boolean addAll(int index, Collection<? extends T> c) {   writeLock.lock();   try {    return list.addAll(index, c);   } finally {    writeLock.unlock();   }  }   public boolean removeAll(Collection<?> c) {   writeLock.lock();   try {    return list.removeAll(c);   } finally {    writeLock.unlock();   }  }   public T set(int index, T element) {   writeLock.lock();   try {    return list.set(index, element);   } finally {    writeLock.unlock();   }  }   public void add(int index, T element) {   writeLock.lock();   try {    list.add(index, element);   } finally {    writeLock.unlock();   }  }  }
正文到此结束
Loading...