Stack or LinkedList for ObjectPool?

I am sure that the correct answer to this depends on the type of objects combined and the workload, so I will talk in detail about my implementation:

I have an ObjectPool used to combine lengthy command line processes. These processes interact via stdin / stdout and perform file / network operations. Many tasks run much faster than others, and can quickly return processes to the pool. All access to the pool must be thread safe.

My question is: is it better for me to manage the pool using LIFO / Stack or FIFO / ConcurrentLinkedQueue? My reasoning on both sides:

  • Hot objects are stored on the stack where resources can remain cached / etc. longer.
  • FIFO balances a more even distribution between processes, with each doing less work. Thanks!
+4
source share
2 answers

My first thought was: check it out ! Both of your arguments seem reasonable. You must implement both strategies and run tests from which they lead to greater bandwidth.

The problem that you describe in your question is similar to the process that operating systems face. Therefore, it might be useful to use priorities. Use PriorityQueue and assign different priorities to your tasks, which can dynamically change over time, such as Aging .

Last but not least, as mentioned in the comments, you can try as ConcurrentLinkDeque , and let some get front objects and some back objects. Here's also my advice - to try and measure which balancing works best.

+3
source

This may not affect performance. Do whatever is easiest to encode and makes code easier to understand. The lack of a cache here will not make any difference when you deal with command line processes that perform file and network I / O ... they will dominate by orders of magnitude.

+1
source

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


All Articles