♾️ The Beginning of The End

🥕 The Program, The Process & The Thread

Multi-threading is when the process uses multiple threads. Java provides us with the Thread class declared in the java.lang package to deal with threads.

🪡 The Need for Multi-threading

The 2 main reasons for using multiple threads are Responsiveness & Performance.

<aside> 👉

Multi-tasking at the process-level vs thread-level

Use Multi-threaded approach when you prefer to share the data between threads. Threads are much faster to create and destroy. Also it has faster context switches.

Use Multi-processes approach when security and stability is your concern or if the tasks are unrelated to each other.

</aside>

Responsiveness

Responsiveness is when the users are not made to wait after they have performed any action. We can use multi-threading to achieve responsiveness. Responsiveness is extremely important in UI based applications. For example — If a user submits a request to a web server then other user should not have to wait until the request of first user is completed. This behavior can be achieved using concurrency.

<aside> ✅

Concurrency is when the CPU switches between multiple threads to create an illusion of parallelism, when in reality the threads are not running in parallel. This is called as concurrency at the Thread level.

</aside>

Performance

If we are provided with multiple cores then we can actually run multiple tasks in parallel on different cores. This will help us to complete complex tasks in fraction of time than the single threaded application.

Performance Optimization

Generally there are 2 main units to measure performance

  1. Latency

    Latency is the time required to complete a task. We can reduce latency by breaking a single long tasks in multiple shorter tasks and scheduling them in different threads in parallel. This will improve the performance of the application by the factor of N, where N is the number of threads. There are few things that we need to consider before dividing the tasks:

    1. Can we break any task into subtasks? — No

      No, we cannot break all the tasks into sub-tasks.

    2. What should be the value of N?

      In an optimal case, the value of N should be the number of cores present on the machine. So that all the threads can run in parallel on different threads. This is theoretical optimization, In practice there will be other tasks running on the system.

    3. Does breaking the tasks and aggregating the subtasks comes free?

      It certainly has some cost associated with it. Breaking the trivial and simple tasks would be of no use as it would slow the application.

  2. Throughput

    Throughput is the number of tasks completed in a given period. It is measured in tasks / time unit. If we complete N tasks in T time then the throughput would be T / N.