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.
Performance: ArrayList is faster. Since it is non-synchronized, while vector operations give slower performance since they are synchronized (thread-safe), if one thread works on a vector, it has acquired a lock on it, which forces any other thread wanting to work on it to have to wait until the lock is released.
ArrayLists are resizable arrays and can store elements of type wrapper class objects. Java provides the flexibility of converting ArrayLists to Array and vice versa. 3 ways of conversion - manual conversion using get() method, using Object[] toArray() method, using T[] toArray(T[] arr) method.
They are obsolete, but they are not deprecated.
For those who have to fight with legacy code do just the following:
new Vector<Obj>(anyThingWhichImplemntsCollection);
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With