Java synchronized list between threads. Best practice

I am making a database registration mechanism when certain changes have occurred. These changes are queued in the thread that processes 25 LogObjects in the queue every 50 ms.

I was thinking about using Collections.synchronizedList () to store objects that I still need to process in the stream.

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

However, I feel that there may be better ways to do this, or is this a perfectly acceptable approach? Or should I use ArrayBlockingQueue for his situation? Or another synchronized list object ... there are so many options.

This is my first experience with threads, so I'm trying to figure out what is the best approach for the job queue and which objects to use to maintain it. Can I just add things directly to the lists with the list? Or do I need to use a synchronized method to do this in a thread?

The questions are mainly:

  • Where can I store a synchronized list of objects to be processed between two threads (in a processing thread or in the main thread?)
  • What is the best practice for adding / removing items from a list? via a synchronized function or directly to a List object?
  • What are my options for List objects when creating a job queue?
+6
source share
3 answers

Your choices basically revolve around what you want, when for some reason a backup of the queue and how much memory you want to use.

If itโ€™s convenient for you to stop the main process until the log stream clears part of the queue, then ArrayBlockingQueue will be fine, because it (and fixed in size) will not consume your memory under heavy load.

If you are comfortable ignoring memory issues when backing up logs (you may be sure that the logging flow will always be supported), then LinkedBlockingQueue might be better, as it is slightly more optimal and unlimited. It can also be given a size limit, but this is optional.

If you use any of them, you do not need to add synchronization logic, as they do everything themselves.

Where can I store a synchronized list of objects to be processed between two threads (in a processing thread or in the main thread?)

Or, you usually create it in your main thread and pass it to the logging stream and the processing stream, because both of them will share it.

What is the best practice for adding / removing items from a list? via a synchronized function or directly to a List object?

BlockingQueue provides a rich API for thread safe adding and removing elements.

What are my options for List objects when creating a job queue?

See above.

+3
source
  • Wherever it is available to all those who use it.
  • BlockingQueue made for this, and you do not need to do any synchronization yourself.
  • Numerous, but BlockingQueue is the easiest in this situation (producer-consumer).
+4
source
  • In this case, the queue seems to belong to the processing object, that is, the logging thread. But this is just a conceptual solution, and in the end they both should have a link to some common object.
  • It's definitely better to expose the only method of adding items (you really don't want to delete) than to expose the whole list, free of abuse. Again, this has nothing to do with synchronization, it's just the basic principle of encapsulation, or Demeterโ€™s law, if you prefer: just let customers do whatever you need. There is no reason why the main thread should know anything about how you implement the queue (or even where there is a queue), other than "I can post an event here."
  • The queue is probably best represented by LinkedList , but since the queue will still be small and the operation is not on a critical path, the choice is unlikely to be important. Use what is easiest to read and understand. (I would go with @Kayaman's suggestion about BlockingQueue , since it takes a lot of work from you, and anyone reading the code can easily understand what's going on.)
+1
source

Source: https://habr.com/ru/post/985859/


All Articles