ArrayList.add() in a for loop, will the order of the items always be the same?
Kevin Bear
Greenhorn
Joined: Mar 12, 2009
Posts: 4
posted
0
I am using an ArrayList to store some information, the code looks like this:
Two questions:
1. Will the order of the results in the list be guaranteed? (From what I read, it seems not. But the experiments I have done always produce nicely ordered results)
2. if I use the following code to examine the result after the for loops, will the get() method ensure that all the calculations in the for loops completed before the result is examined?
Thanks in advance for all the help.
Kevin Bear
Greenhorn
Joined: Mar 12, 2009
Posts: 4
posted
0
An addtional question: I have an ArrayList of Objects (I'll name it callableList) for use in the Callable module. callableList gets some new items (as well as get some items removed) every time the program enters the innnermost for loop. It seems that this would cause concurrency problem, but the test I ran seems to be fine. I am now totally confused with all the concurrency ideas
Alan Mehio
Ranch Hand
Joined: Apr 04, 2005
Posts: 70
posted
0
Two questions:
1. Will the order of the results in the list be guaranteed? (From what I read, it seems not. But the experiments I have done always produce nicely ordered results)
2. if I use the following code to examine the result after the for loops, will the get() method ensure that all the calculations in the for loops completed before the result is examined?
1- It says in the spec : " By default, even core threads are initially created and started only when new tasks arrive, but this can be overridden dynamically using method"
so the task are being executed in a concurrent way. If you have one CPU processor then it may give you the order. In general, there is no order since the default underlying working queue is BlockingQueue; you may define your own queue during the creation of the pool by implementing your own Queue or the one which exist such as PriorityQueue or PriorityBlockingQueue
2- get will wait until the computation result is returned;please refer to the Future.get() API docs for further details.
So when you mention "ensure all the calculations" I guess you mean for each callable task you submit it does process intensive things and it will return the future result which you can obtain via the "get" method; you will never go forward after the get if the result is not ready ( finish, exception happen etc..)
So it is based on per future and not all future.
Two questions:
1. Will the order of the results in the list be guaranteed? (From what I read, it seems not. But the experiments I have done always produce nicely ordered results)
2. if I use the following code to examine the result after the for loops, will the get() method ensure that all the calculations in the for loops completed before the result is examined?
1. In your example you are putting the Futures in the List. The Future is returned immediately when the exec.submit() is called. It does not wait for the callable to finish. This means that the Futures are ordered. When you read the code, you are waiting for the callable to finish using the get() method so if the Object you want isn't ready you wait for it to be ready - so they are retrieved in order as well. Because you are storing the Future in the List you are guaranteed they will be in order.
2. Yes, as per the API, get will wait for the callable to complete before returning.
Kevin Bear wrote:
An addtional question: I have an ArrayList of Objects (I'll name it callableList) for use in the Callable module. callableList gets some new items (as well as get some items removed) every time the program enters the innnermost for loop. It seems that this would cause concurrency problem,
Perhaps, perhaps not. You don't show what happens with callableList in the code you provide, and we have no clue what the Callable code is doing, so it is impossible to say with the code you provided.
Steve
Kevin Bear
Greenhorn
Joined: Mar 12, 2009
Posts: 4
posted
0
Many thanks to Alan and Steve for the replies.
Perhaps, perhaps not. You don't show what happens with callableList in the code you provide, and we have no clue what the Callable code is doing, so it is impossible to say with the code you provided.
Sorry I made some mistake in decribing how the Callable module works. The callableList is used in the callable module (for 1000 times in the innnermost for-loop) to generate a result. What happens in the Callable(callableList) module is like this:
I assume that the last 10 values you want to take out of the list are supposed to be the same 10 you put into the list? Or is that incorrect.
If the assumption is correct then:
Then if the same callableList is sent to each instance of Callable, yes, you will have concurrency problems. You will either need to
1) synchronize everything from where you begin to put things into the list to where you need to take them out, and if this is the entire work of your callable then you may want to get out of using a threaded application
2) make a snapshot copy of the callableList to send into the Callable so the different threads don't interfere with each other
Kevin Bear
Greenhorn
Joined: Mar 12, 2009
Posts: 4
posted
0
Thank you Steve for the clarification and help.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: ArrayList.add() in a for loop, will the order of the items always be the same?