I need to iterate on FastMap.values.
My problem is that basic loop fails over NullPointerException
basic loop
Collection<Order> orders = myObject.getOpenOrders();
for (Order order : orders) {
}
problem is that another thread in my system edits the fastmap
It is adding and removing elements to it and I get the NullPointerException.
Rarely, but It should be solved.
So i added Null check
Collection<Order> orders = myObject.getOpenOrders();
for (Order order : orders) {
if (order != null )
}
and still I get the NullPointerExcetion
So I tried iterate it as follows
FastMap<String, Order> openOrders = myObject.getOpenOrdersMap();
for (FastMap.Entry<String, Order> e = openOrders.head(), end = openOrders.tail(); (e = e.getNext()) != end && e != null;) {
Order order = e.getValue();
}
But then the loop stops when it gets to null instead of throwing NullPointerExcetion.
And this is also a problem, since I need to iterate all of the elements.
I assume that the problem is that the for iteration uses values() and it is actually a pointer to the list.
I tried to copy the list but then I also get NullPointerExcetion in the copy process.
any sugggestions
BTW: I know that changing the whole design is the best solution and using locks in every insert and read. but is there some smaller change I can make in order to solve my problem?
Any idea?
This is not JDK classes? (Javolution?)
From what doc says :
So you may encounter some concurrency problem ?
Try that :
FastMap<String, Order> openOrders = myObject.getOpenOrdersMap().shared()