Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Synchronised List between Threads. Best practice

I'm making a database logging engine when certain changes have happened. These changes get pushed to a queue in a thread that processes 25 LogObjects in the queue every 50ms.

I was thinking of using a Collections.synchronizedList() to hold the objects I still need to process in the thread.

The main application thread pushesses LogObjects into the list via ThreadObjInstance.LogList.add(new LogObject("Something to log"); and in the thread I do LogObject x = LogList.shift(); to process it.

However I feel like there might be better ways to do it or is this a perfectly acceptable approach? Or should I use ArrayBlockingQueue for his situation? Or another synchronised List Object... there are so many choices.

This is my first time working with threads so i'm trying to figure out what the best approach is for a job queue and which objects to use to maintain it. Can I just add things directly to the threaded lists? Or do I need to use a synchronised method for that in the thread?

The questions are basically:

  1. Where do I store a Synchronised List of Objects to process between two threads (In the processing thread or the main thread?)
  2. What is the best practise to add/remove items from the list? via synchronised function or directly on the List object?
  3. What are my choices for List objects when building a Job queue?
like image 884
Tschallacka Avatar asked Apr 24 '15 09:04

Tschallacka


2 Answers

Your choice mostly revolves around what you want to happen when the queue gets backed up for some reason and how much memory you want to use.

If you are comfortable halting your main process until the logging thread has cleared some of the queue then an ArrayBlockingQueue would be fine because it is bounded (and fixed in size) and will not eat your memory under high load.

If you are comfortable with ignoring memory issues when logging gets backed up (perhaps you are certain the logging thread will always keep up) then a LinkedBlockingQueue may be a better fit as it is slightly more optimal and is unbounded. It can also be given a size limit but this is optional.

If you use either of these you do not need to add any synchronization logic as they do it all themselves.

Where do I store a Synchronised List of Objects to process between two threads (In the processing thread or the main thread?)

Either - you would normally create it in your main thread and pass it to your logging thread and processing thread because they will both be sharing it.

What is the best practice to add/remove items from the list? via synchronised function or directly on the List object?

BlockingQueue provides a rich API of thread-safe adding and removal of items.

What are my choices for List objects when building a Job queue?

See above.

like image 159
OldCurmudgeon Avatar answered Oct 13 '22 12:10

OldCurmudgeon


  1. Anywhere you want, where it's accessible by all the threads that use it.
  2. BlockingQueue is made for this, and you don't need to do any synchronization yourself.
  3. Numerous, but BlockingQueue is the most straightforward in this situation (producer-consumer).
like image 38
Kayaman Avatar answered Oct 13 '22 11:10

Kayaman