How to get a customized synchronized list in java?

I have one custom MyList list that extends ArrayList, for example:

class MyList extends ArrayList<SomeParticularItem>{
   [some methods...]
}

Since I have parallel reading and writing to the list, I want to synchronize it:

MyList mylist = (MyList) Collections.synchronizedList(new MyList());

Everything seems to be in order .jar is built. Then at runtime, I get:

java.util.Collections$SynchronizedRandomAccessList cannot be cast to MyList

Is there a better way (if at all) to get a synchronized list of a list that inherits from some java.util.List?

+1
source share
2 answers

The easiest way to do this is to expand Vectorinstead ArrayList. You can also synchronize methods MyListyourself if you want to save it as ArrayList.

+1
source

, MyList, List mylist = Collections.synchronizedList( MyList());

: , MyList Vector, Vectors , .

+2

All Articles