Posts

Showing posts from September, 2022

Concurrent Collection Classes

  Concurrent Collection Classes A collection of Application Programming Interfaces that deal particularly with concurrent operations. These classes are alternative to the Java Collection Framework and provides similar functionality although with additional support towards Concurrency. The ultimate goal of this collection is to allow many threads access to resources in a synchronised manor. Concurrent Collections in Java According to Oracle.com  and as quoted below:  "The  java.util.concurrent  package includes a number of additions to the Java Collections Framework. These are most easily categorized by the collection interfaces provided: BlockingQueue  defines a first-in-first-out data structure that blocks or times out when you attempt to add to a full queue, or retrieve from an empty queue. ConcurrentMap  is a subinterface of  java.util.Map  that defines useful atomic operations. These operations remove or replace a key-value pair only if t...

Executor and ExecutorService

Image
  Executor Simply put, an Executor just actions threads and instructions provided to it ExecutorService Extends from Executor and includes startup / shutdown threads and is able to wait and determine the status of any instructions a user may have added above the Executor. In the illustration above, we se a simple example that illustrates the main differences between the Executor and ExecutorService.  Both Executor and ExecutorService are interfaces, however ExecutorService extends Executor.  Executor runs execution methods, ExecutorService runs submission methods. Executor returns void type, ExecutorService returns future type.  In summary and according to Oracle.com : "An ExecutorService that provides methods to manage termination and methods to that can produce a Future for tracking progresso one or more asynchronous task" You can read up more about this class by clicking the Oracle link above. 

Atomic Operation and Atomic Classes

  Atomic Operation An Atomic Operation is widely known in Concurrency as an operation that performs a single unit of work without any possibility of interference from another operation. A classic example to illustrate Atomic Operation would be two users making use of the the same application to print a statement - Atomic Operation will allow both users to print the statement by synchronising the requests and running them individually. First user clicks print, then second user clicks print - Printer Driver will synchronise the requests and run each request as its own process. Atomic Variables Atomic Variables belong to the java.util.concurrent.atomic package and  considered  primitive variables that are designed to be  safely used in multi-threaded processes. Atomic Variables provide operation that cannot be interfered by any multiple-threads and are guaranteed to execute using machine-level operation and modern processors. They are helpful in avoiding ...

Deadlock, Livelock and Starvation

Image
  When a process waiting fails. In multiprocessing, we find that there may be a process, or a number of processes that will need to make use of a particular resource available to the application, and all the time, we find that a process is waiting for a resource that is currently in use by another processes, making it unavailable. If we ignore some very vital signs that a process is waiting for too long, the process will fail. We are likely to find our application in one of the three known scenarios. Deadlock When a process acquires or is assigned a resource, often once the process is complete or is done with the resource, it releases it so that any other processes that will require the resource has access to it.  For any reason, if one process (Process A) is waiting on a resource (Resource 2) being used by another process (Process B), and by chance resource 2 is not being released by Process B as it is also waiting on the release of a resource (Resource 1) being used by Proce...

Java Memory Model

Image
  Java Memory Model (JMM) A Java Memory Model describes how threads will interact through memory.  A JMM defines how multithreaded programs must behave, and when reordering can happen by placing execution-time restrictions on how threads behave between each other and the main memory, and this is meant to result in a reliable, consistent working java application.  It determines how the java virtual memory behaves with the computers Random Access Memory (RAM). This plays vital in designing java concurrent applications. The JMM used within a Java virtual model (JVM) works by dividing memory between each thread stack and the heap. Thread-Stack Every thread that is running within the Java virtual Machine will have its own thread stack and this is the only thread stack the thread will have access to. The thread stack holds unique thread-information about the history of the methods that were called to reach the current point in the execution of that thre...

Daemon Threads

Daemon Threads What is a daemon thread? Every thread in a process, aside from its state, is prioritised where threads with higher priority are executed in preference to lower priority threads. Daemon threads run in the background and are low priority operations. The setDaemon()  method of the Thread class is used to mark a thread as either a daemon thread, or a user thread. Some of the common uses for daemon threads are:  Garbage collectors (Collecting statistics and status monitoring tasks) Performing asynchronous input, output tasks.  Listening for incoming connections Creating a Daemon Thread Creating a daemon thread is pretty simple, we make use of the setDaemon() method (line 18). When the parameter is true, we declare that the thread is a daemon thread.  By default, the parameter is set to false, which means the thread is a normal / user thread. In the example below, the main thread creates a daemon thread that displays "I'm a Daemon." every 0.5 seconds. O...

Creating threads, running them and their states

Image
 Creating a Thread A thread consists of two aspects, the main thread ( which is the thread that is run when the program starts) and the slave thread  which is a 'product' or result of the main thread and is considered the final thread that completes the execution of the thread. We are able to create threads making use of two approaches:  • Implementing a Runnable interface • Extending the Thread class The difference between implementing Runnable interface and extending the Thread is that when we extend the Thread class, we are unable to extend any other class, even if we need to, where as implementing Runnable saves space for our class to extend any other class at any time.  Implementing java.lang.Thread class Output Implementing java.lang.Runnable interface                                                    ...

Processes and Threads

Processes A process is defined as an instance of a program running in an operating system  If we were to think of an activity in normal life, like washing the dishes for example. The actions we that need to be made to clean the dishes (We can imagine all the steps collectively, like picking up the dish, using the cloth to clean the dish appropriately to its design, rinsing the dish off and placing it in the dry rack) is what we can compare a process to in the programming world. It is the collection of actions that take place in order for a program to deliver the expected result or outcome. This plays a big role in concurrency(multitasking) Distinctions of a process • A process is compiled of multiple threads • Makes use of a counter to identify where in the process (which thread) is currently running • Does not share memory Threads A thread is a component of a process running in an operating system. In the example above, the process of cleaning a dirty dish has a desired result - ...