C
Curious
I have an arraylist, OrderList, in which each item in the arraylist is
an Order object.
At the beginning of my program, I read from an input file to create
the OrderList. Then I launch several threads for different operations
related to OrderList.
Thread #1: "OnOrderFilled" method is REMOVING an order from the
arraylist once the order is filled.
Thread #1: "OnOrderCancelled" method is changing the property of an
order once the order is cancelled.
Thread #2: "ActivateOrder" method loops through each order in
OrderList and sends out the orders.
Here's the problem - When "ActivatOrder" sends out orders (please note
that it takes time to send out orders), some orders may be filled at
the same time; so the "OnOrderFilled" method on Thread #1 is fired
which REMOVES the orders from OrderList that are filled, and this
affects "ActivateOrder" method that loops through OrderList. I get
"Collection was modified" exception.
To get this fixed, I was thinking of instead of REMOVING order from
OrderList that causes "Collection was modified" error, I should add a
property to each Order called "IsFilled" so that I CHANGE "IsFilled"
to true when an order is filled.
My question is -- Will changing the property in "OnOrderFilled" method
cause any thread-safety issue for "ActivateOrder" method? Thanks!
an Order object.
At the beginning of my program, I read from an input file to create
the OrderList. Then I launch several threads for different operations
related to OrderList.
Thread #1: "OnOrderFilled" method is REMOVING an order from the
arraylist once the order is filled.
Thread #1: "OnOrderCancelled" method is changing the property of an
order once the order is cancelled.
Thread #2: "ActivateOrder" method loops through each order in
OrderList and sends out the orders.
Here's the problem - When "ActivatOrder" sends out orders (please note
that it takes time to send out orders), some orders may be filled at
the same time; so the "OnOrderFilled" method on Thread #1 is fired
which REMOVES the orders from OrderList that are filled, and this
affects "ActivateOrder" method that loops through OrderList. I get
"Collection was modified" exception.
To get this fixed, I was thinking of instead of REMOVING order from
OrderList that causes "Collection was modified" error, I should add a
property to each Order called "IsFilled" so that I CHANGE "IsFilled"
to true when an order is filled.
My question is -- Will changing the property in "OnOrderFilled" method
cause any thread-safety issue for "ActivateOrder" method? Thanks!