What Is Multi-threading?

What Is Multi-threading?

Hello, tech family welcome to my blog post for this week, it's been quite a while since I dropped my last post, been super busy with lots of stuff, well over the week I learned about threads and concurrency to implement sales and purchases in the store sales and management application I have been working on. So in light of that, I will be writing about multithreading which is a very important concept in Java as regards having a better user experience with respect to application responsiveness, fairness, safety, and utilization of single and multiple CPUs.

In this post, I will be basically writing about two concepts:

  1. Threads and Concurrency.

  2. Scheduling Task.

After going through this post you should be able to have an idea of:

  • What is multithreading and when to use it.

  • Multithreading Vs Multitasking

  • Thread safety

  • How Thread and Process work.

  • Concurrency Vs Parallelism

  • How to schedule tasks.

So let's begin...

WHAT IS MULTITHREADING?

In computer architecture, multithreading is a computer processing unit (CPU) having multiple threads of execution inside the same application, where threads are like the separate tiny CPUs used for application performance.

WHY YOU SHOULD USE MULTITHREADING

Multithreading helps in the better usage of a single CPU:-

One of the major reasons why multi-threading is used is to be able to utilize resources more efficiently in the computer. What this basically means is that if one thread is waiting for the response to a request sent over a network, another thread within the application will be able to make use of the available CPU to respond to something else.

Multithreading helps in the better usage of multiple CPU:-

If your CPU contained multiple execution cores then it is important for you to use multiple threads in your application in order for you to execute all CPU cores as to the use of a single thread that can only implement just one CPU.

Multithreading gives better flexibility:-

Another reason to use multithreading in your application is that it gives you a better user experience due to it being responsive. for instance, picture yourself for a moment trying to purchase an iPhone from an e-commerce store and then clicking the add to cart button while waiting patiently for the request to be sent and bring back a response over the network, here it matters which thread is carrying out the request process if in the process of using the add to cart thread and that same thread is also updating the user interface then there is a possibility that this will lead to the user experiencing the GUI lagging behind, instead of that the add to cart request can be performed by another thread thereby allowing the user interface thread to respond to other requests.

Multithreading stops thread monopolization:-

Multithreading can share resources fairly amongst users that way servers could receive requests from different clients at the same time and be able to send back responses to all clients.

Multithreading vs Multitasking

Have you ever been confused about the concept of multithreading and multitasking, seeing both terms as the same thing, well you might be right to some extent, but both terminologies have major differences between them, in general, they are just two different approaches used to increase the response time and increase the efficiency of the overall program.

Multitasking:- involves the execution of multiple processes at the same time, multitasking can switch between various processes concurrently, here the processes share separate resources and memory. For instance, moving through your browser while creating a to-do list in your excel file and listening to music in your media player at the same time, multitasking is slower than multithreading.

multitask image.webp

Multithreading:- is basically having multiple threads of execution inside the same application, in multithreading the CPUs are able to execute multiple threads from a process at the same time while providing the same memory and resources to the process.

multithread image.jpg

Thread Safety

Why should you bother about thread safety? thread safety is a way for multiple threads to access the same resources without producing unexpected errors.

  1. One of the ways of achieving thread safety is to make class instances immutable, this can be achieved by using the private and final keyword and not providing setters.

I am going to illustrate this using a simple code:

public class ThreadSafety {
  private final String makeThreadSafe;

public class ThreadSafety(String makeThreadSafe) {
  this.makeThreadSafe = makeThreadSafe;
}

}

By declaring the threadSafety object private and final it becomes immutable since its state can't change.

  1. Thread safety can also be achieved by the implementation of synchronized methods where one thread has access to a method while blocking its access to this method from other threads.

  2. The use of Atomic Integer, Atomic Long, and Atomic Boolean which are sets of the Atomic Object class can also be used to make thread-safe as this allows us to perform Atomic Operations which are threads without using synchronization.

Thread and Process

Thread:- is a lightweight process that has its own call stack, and can access shared data and resources from another thread in the same process.

Process:- in-process access to shared data are not possible as they run independently of each other.

Concurrency Vs Parallelism

Concurrency in Java means that an application is making progress in more than one task at the same time, this can enable the program to have high efficiency by making use of the machine operating system and hardware. The thing about concurrency is that it mostly depended on threads which are lightweight processes having their own call stack. The concept behind Java concurrency is not as simple as it seems, as the concurrent application has complex design than a single-threaded application, as this makes codes executed by multiple threads need more attention as it becomes difficult to detect bugs from it, and moreover, concurrency requires more resources to operate the application.

concurrency.jpeg

Parallelism means that an application can split up large tasks into smaller sub-tasks which are processed in parallel on different CPUs at the same time to solve problems.

parralele.png

Task Scheduling

In Java, the scheduling of tasks is done by a scheduler, which schedules a thread or task that runs at a certain period of time at a fixed interval. Some of the ways to schedule a task are:

TimeTask which is executed by a daemon thread, a daemon thread in Java is a service provider thread that runs in the background to perform a task.

TaskScheduler this basically helps you schedule a task at a particular time, it has almost the same working principle as the ScheduleExecutorService, but more preferable if the application is going to be in spring.

So techies, here you have it, we really can not exhaust the topic of multithreading, concurrency, etc in just one post, but this should give us an idea of why we should be using multithreading in our program.

Until my next post comes your way, keep grinding, keep learning, keep thinking, keep doing and never stop believing.