- public class Collections {
- ......
- static List synchronizedList(List list, Object mutex) {
- return(listinstanceofRandomAccessnewSynchronizedRandomAccessList<>(list, mutex) :
- newSynchronizedList<>(list, mutex));
- }
- static classSynchronizedList
- extendsSynchronizedCollection
- implementsList {
- private static final longserialVersionUID = -7754090372962971524L;
- finalList list;
- SynchronizedList(List list) {
- super(list);
- this.list = list;
- }
- SynchronizedList(List list, Object mutex) {
- super(list, mutex);
- this.list = list;
- }
- publicE get(int index) {
- synchronized(mutex) {return list.get(index);}
- }
- publicE set(int index, E element) {
- synchronized(mutex) {return list.set(index, element);}
- }
- public voidadd(int index, E element) {
- synchronized (mutex) {list.add(index, element);}
- }
- ......
- }
- ......
- }
来源: