Best practices for concurrency module

Here is a list of considerations that a programmer must look into when carrying out concurrency. Let's look at the following best practices to consider when you to get a chance to work with the concurrent application module.

  • Obtaining an executor: The Executor Framework for obtaining an executor supplies the executors utility class. Various types of executors offer specific thread executions policies. Here are three examples:
    • ExecutorService newCachedThreadPool(): This creates a thread pool using the previously constructed threads if available. The performance of the programs that make use of the short-lived asynchronous tasks is enhanced using this type of thread pool.
    • ExecutorService newSingleThreadExecutor(): A worker thread that is operating in an unbounded queue is used here to create an executor. In this type, the tasks are added to the queue that is then executed one by one. In case, this thread fails during the execution, a new thread will be created and replace the failed thread so that the tasks can be executed without interruption.
    • ExecutorService newFixedThreadPool(int nThreads): A fixed number of threads that are operating in a shared unbounded queue are reused in this case for the creation of a thread pool. At threads, the tasks are being actively processed. While all the threads in the pool are active and new tasks are submitted, the tasks will be added in the queue until a thread becomes available for the processing of the new task. If before the shutdown of the executor, the thread fails, a new thread will be created for carrying out the execution of the task. Note that these thread pools exist only when the executor is active or on.
  • Use of cooperative synchronized constructs: It is recommended to use cooperative synchronized constructs when possible.
  • No unnecessary lengthy tasks and oversubscription: Lengthy tasks are known to cause deadlock, starvation, and even prevent other tasks from functioning properly. Larger tasks can be broken down into smaller tasks for proper performance. Oversubscription is also a way to avoid the deadlock, starvation, and so on. Using this, more threads than the available number of threads can be created. This is highly efficient when a lengthy task contains a lot of latency.
  • Use of concurrent memory-management functions: If in a situation, ensuing concurrent memory management functions can be used, it is highly recommended to use it. These can be used when objects with a short lifetime are used. The functions such as Allot and Free are used to free memory and allocate, without memory barriers or using locks.
  • Use of RAII to manage the lifetime of concurrency objects: RAII is the abbreviation for Resource Acquisition Is Initialization. This is an efficient way to manage the lifetime of a concurrency object.

This was all about the concurrency and it's design patterns that can be used to handle and implement concurrency. These are the most common five design patterns for concurrency programs. Also, some of the best practices for carrying out concurrency modules were discussed. Hope this was an informative a piece and helped you understand how concurrency patterns work!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset