Concurrency, Everything you need to know!

So concurrency, today we'll be going through all the ins and out and all the components that make up the concurrency system starting with:

Processes and Threads.

So processes and threads, what are they and how do they work? Processes and threads are the two components that make up concurrency.

A process is simply a program that has already been loaded into memory with all the resources it needs to run, those resources being: the register, program counter and stack. Let's break down each of these resources' function. 

The register is part of the central processing unit (CPU) and is used to store the data used for the program.

The program counter keeps track of where the computer is in running its programs' sequence.

The stack stores information about the active subroutines of a program. It provides a temporary memory location for data to be stored.

So how do these all tie in together? Each iteration of a running program are processes that run independently of each other.  This is particularly efficient because if one process is malfunctioning, it does not affect the program as a whole and can be debugged individually.

A thread is the executing unit of the process. It is important to note that the process can have several threads but a thread cannot contain a process. What does that mean? 

A single-threaded process (a process that only contains one thread) is when the process and the thread are the essentially the same thing, meaning it can only do one thing at a time.

A multi-threaded process is when the process is accomplishing multiple things at virtually the same time. 

Threads are known as "lightweight" processors because they access shared data in a heap even though they have their own stacks. The downside to this being if there is a program with a single thread it will affect the program as a whole.


So let's get into the nitty gritty, the different types of threads and how to use them.

How to create a thread in Java and run it:

1) Extend the main class to the Thread Class 

2) Override the run() method

2) Create an instance of the thread class and call its start() method

3) Run

What happens if you don’t override the thread class run() method?

In a nutshell, there will be no output for the compiler to run. The danger is in the fact that the compiler will not display an error message so you will not know what is wrong with your code.

What is a daemon thread and what are its use cases:

Daemon threads run in the background to perform lower-level services for the application such as releasing unused memory. 

How to create a daemon thread in Java:

1) call the setDaemon() method and set its boolean variable to true


What is the Java Memory Model (JMM):

JMM describes how memory interacts with threads and therefore dictates the behaviour of said multithreaded programs through execution-time restraints. 


What are deadlock, livelock, and starvation and what causes these conditions?

Deadlock is when processes block each other because they're waiting for resources held by the other process, thus halting progress. It is caused by these four conditions being met, simultaneously:

1) Mutual Exclusion

2) Hold and Wait

3) No Preemption

4) Circular Wait

Livelock is similar to deadlock in that the processes are still dependant on each other thus halting progress, however, the status of each progress is constantly changing. This occurrences when both processes are either active or inactive at the same time.

Starvation when a process has restricted access to a shared resource, thus halting progress. It can be caused by deadlock, livelock or any other processing bug.


What is atomic operation and what are atomic classes in the Java Concurrency API:

Atomic operations handle multi-threaded environments and account for their potential data inconsistencies. Some atomic classes include:

1) AtomicInteger

2) AtomicLong

3) AtomicBoolean

4) AtomicReference


What are Executor and ExecutorService and the differences between them:

See table below:


https://www.linkedin.com/pulse/difference-between-executor-executorservice-executors-omar-ismail


Finally, What are Concurrent Collection Classes:

Concurrent Collection Classes are java classes that are specifically designed to accommodate concurrent operations.


Comments

Popular posts from this blog

Interfaces in Java, Everything You Need to Know!

LEARN TO CODE with HYPERIONDEV!