• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Iterating over ArrayList while still growing

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to populate an ArrayList with SQL update statements to pass to an update object while still adding statements to the ArrayList. Is this possible?

A little background for this program. It will have five different versions all doing nearly the same thing. I have decided threading each variation would speed things up a bit. Each variation will get their data, manipulate it, and send the data to the ArrayList to be updated to the database. I want to be able to send these updates to the database while the threads are still populating the ArrayList.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using an ArrayList probably isn't the best solution, because you will get ConcurrentModificationExceptions when iterating over it and the producer adds more items. You should look at the collections found in the java.util.concurrent package. Perhaps some implementation of the java.util.BlockingQueue would help ensure thread safety.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't iterate using an Iterator, but you can still use the indexes:

This will be very inefficient if used with a LinkedList, but with ArrayList it's pretty fast.

You're still wise to synchronize access to the list, for instance using Collections.synchronizedList.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob - I assume that index and i were intended to be the same variable.

The problem with that approach is that, if the objects are processed faster than they are added to the list, index could become equal to the size of the loop, the while loop will finish and then more objects could be added to the list.

A blocking queue of some sort is probably a better solution.
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want to investigate an Object Relational Mapping - unless you are dealing with a legacy database, you shouldn't need to directly access an SQL interface to a database.

I don't envy your task.

ORM forum
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joanne Neal:
Rob - I assume that index and i were intended to be the same variable.

The problem with that approach is that, if the objects are processed faster than they are added to the list, index could become equal to the size of the loop, the while loop will finish and then more objects could be added to the list.

A blocking queue of some sort is probably a better solution.


Yes, index and i should be one and the same. Guess I missed one when replacing.

You're right that BlockingQueue would be better. To be honest, I'm not that familiar with that entire package - I never had to use any of it, or if I did I probably implemented it using Java 1.4 mechanisms.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Joanne]: The problem with that approach is that, if the objects are processed faster than they are added to the list, index could become equal to the size of the loop, the while loop will finish and then more objects could be added to the list.

But why is that a problem? If you're looping through while new updates are arriving, then it's a given that there may be new updates after you finish the loop. That should be expected, right? And if a new entry arrives just as you're otherwise finishing the loop, then maybe you'll see it just before you finish, and maybe you won't. The latter situation is pretty much like what happens if an update arrives after you're done. Either way, you probably need to repeat the loop periodically if you want to handle all arrivals.

[Joanne]: A blocking queue of some sort is probably a better solution.

Agreed.
 
Nickolas Case
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, BlockingQueue...

Perfect!

I had hovered over ORM but it didn't seem to fit as well as a queue

Thanks for all the input!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Simmons:
[Joanne]: The problem with that approach is that, if the objects are processed faster than they are added to the list, index could become equal to the size of the loop, the while loop will finish and then more objects could be added to the list.

But why is that a problem? If you're looping through while new updates are arriving, then it's a given that there may be new updates after you finish the loop. That should be expected, right? And if a new entry arrives just as you're otherwise finishing the loop, then maybe you'll see it just before you finish, and maybe you won't. The latter situation is pretty much like what happens if an update arrives after you're done. Either way, you probably need to repeat the loop periodically if you want to handle all arrivals.



It's only a problem in as much as you have to code to allow for such things (by running the loop multiple times as you suggest). Using a blocking queue means you don't have to worry about it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic