0

I need to write an arraylist to a file.
It gets filled all the time and when it gets too big I need to start writing it.

So I thought to check when the arraylist sise is greater then 100 and then append the file and write the current rows .
But the problem is sometimes it doesnt get filled for a few minuettes and I will want to dump the data to a file.
So my second thought was to have another thread that will check if there are rows every few sec and dump it to the file.
But than I would need to manage locks between threads.

My questions are :
1. Is the multithread design ok ?
2. Is there an arraylist that supports multithread in any form ?

Bick
  • 17,833
  • 52
  • 146
  • 251

8 Answers8

2

Check out this thread.

You should use

 List synchronizedList = Collections.synchronizedList(list);
Community
  • 1
  • 1
Ludevik
  • 6,954
  • 1
  • 41
  • 59
2

You can make synchronized lists using Collections.synchronizedList.

khachik
  • 28,112
  • 9
  • 59
  • 94
2

Instead of ArrayList I would recommend you to use proper implementation of Queue<E>. If you are only appending data to the list and then dumping it to the file removing saved items from the list, queue is a much better choice.

Some implementations are threads safe and will even allow the caller thread to block until something actually appears in the queue - which is much better approach than having a polling thread. BlockingQueue looks very promising for your case.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
2

From your question it appears most of the time you are going to perform write operation and that too from a single thread & will be intermittently checking for the size of list. other wise you can not use plain old ArrayList.

synchronizing the list and then using locks to access the list looks like a overkill. rather have a if check that will check the size.

If you are going to access list in multiple threads then to avoid ConcurrentModificationException use the method suggested by @Ludevik

There are other approaches as well but for the sake of simplicity @Ludevik approach fits the bill.

Anupam Saini
  • 2,431
  • 2
  • 23
  • 30
1

Instead of arrays you can use vectors. Since vectors are thread-safe.

Ankit
  • 2,753
  • 1
  • 19
  • 26
1

ArrayList is not thread-safe, but you can get a thread-safe list with Collections.synchronizedList()

Maurice Perry
  • 32,610
  • 9
  • 70
  • 97
1

You need to use Collections.synchronizedList(List) to create a thread safe list. However, you still need to synchronize operations such as add or remove and synchronize the updates to the objects held in the list.

aseychell
  • 1,794
  • 17
  • 35
0

The simple solutions are to use Collections.synchronizedList(list) or Vector. However, there is a gotcha.

The iterator() method for a synchronized list / Vector created as above is NOT synchronized. So there's nothing to stop a thread from trying to add new element to the list if you copy it using an iterator explicitly, by using for (type var : list) {...} or by using a copy constructor that relies on the iterator.

This is liable to result in concurrent modification exceptions. To avoid that problem, you will need to do your own locking.


It may be better idea to use a concurrent Queue class so that the thread that writes stuff to the file doesn't need to iterate a list.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216