Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple threads modifying a collection in Java?

The project I am working on requires a whole bunch of queries towards a database. In principle there are two types of queries I am using:

  1. read from excel file, check for a couple of parameters and do a query for hits in the database. These hits are then registered as a series of custom classes. Any hit may (and most likely will) occur more than once so this part of the code checks and updates the occurrence in a custom list implementation that extends ArrayList.

  2. for each hit found, do a detail query and parse the output, so that the classes created in (I) get detailed info.

I figured I would use multiple threads to optimize time-wise. However I can't really come up with a good way to solve the problem that occurs with the collection these items are stored in. To elaborate a little bit; throughout the execution objects are supposed to be modified by both (I) and (II).

I deliberately didn't c/p any code, as it would be big chunks of code to make any sense.. I hope it make some sense with the description above.

Thanks,

like image 677
posdef Avatar asked Apr 29 '10 10:04

posdef


People also ask

What will happen if multiple threads accessing the same resource?

Multiple threads accessing shared data simultaneously may lead to a timing dependent error known as data race condition. Data races may be hidden in the code without interfering or harming the program execution until the moment when threads are scheduled in a scenario (the condition) that break the program execution.

Is multi threading possible in Java?

Java supports multithreading through Thread class. Java Thread allows us to create a lightweight process that executes some tasks. We can create multiple threads in our program and start them.

Can multiple threads access the same variable?

Only one thread can read and write a shared variable at a time. When one thread is accessing a shared variable, other threads should wait until the first thread is done. This guarantees that the access to a shared variable is Atomic, and multiple threads do not interfere.

Can multiple threads write to the same file Java?

You should NOT have multiple threads trying to write directly to the same file.


2 Answers

In Java 5 and above, you may either use CopyOnWriteArrayList or a synchronized wrapper around your list. In earlier Java versions, only the latter choice is available. The same is true if you absolutely want to stick to the custom ArrayList implementation you mention.

CopyOnWriteArrayList is feasible if the container is read much more often than written (changed), which seems to be true based on your explanation. Its atomic addIfAbsent() method may even help simplify your code.

[Update] On second thought, a map sounds more fitting to the use case you describe. So if changing from a list to e.g. a map is an option, you should consider ConcurrentHashMap. [/Update]

Changing the objects within the container does not affect the container itself, however you need to ensure that the objects themselves are thread-safe.

like image 157
Péter Török Avatar answered Oct 26 '22 13:10

Péter Török


Just use the new java.util.concurrent packages.

Classes like ConcurrentLinkedQueue and ConcurrentHashMap are already there for you to use and are all thread-safe.

like image 29
Gandalf Avatar answered Oct 26 '22 13:10

Gandalf