Change from ArrayList to Vector
I'm working on an android game and I just noticed that since onTouchEvent
runs on the UI thread, and the update/render methods are ran from a separate threads, both of them update an ArrayList
which contains the entities. So obviously they conflict if they happen to modify the list at the same time.
I read that Vector
class is used exactly the same as ArrayList
with the only difference that Vector
is synchronized, ergo they wont conflict. Is that true? if so, does it have any performance issue or something that I should be concerned about? I have never used Vector
class before.
EDIT: what I actually meant was change from
ArrayList<Obj> list = new ArrayList<Obj>();
to
Vector<Obj> list = new Vector<Obj>()
But as the answers say, Vector
is not recommended to use. The selected answer solved my issue.
It's oldie Vector
try to not use Vector instead use
synchronizedList
Example :
list = Collections.synchronizedList(list);
Vector is considered obsolete and deprecated read Why vector is considerer obsolete?
对于那些不得不与传统代码进行斗争的人,只需执行以下操作:
new Vector<Obj>(anyThingWhichImplemntsCollection);
List<Foo> list = new Vector<Foo>(new ArrayList<Foo>());
should work. Both of those structures implements List interface.
But like other people suggested, this is not recomended.
链接地址: http://www.djcxy.com/p/76100.html上一篇: Vector与ArrayList的性能
下一篇: 从ArrayList更改为Vector