What Every Java Developer Should Know About Threads, Runnables, and Thread Pools

Multithreading is the most complex and powerful part of Java

The Multithreading chapter is the most difficult chapter in Java to understand and use. Unfortunately, there are not many resources that will give you all the answers. At the same time, concurrency knowledge is crucial. In this article, I explain the core aspects of multithreading that every Java developer must know. In this part, we start with the topic of Thread and Runnable.

Why is concurrency knowledge so important?

You can't get advanced Java work without good knowledge of multithreading

Knowledge of multithreading is almost certainly a topic of interviews for senior Java positions. Without a clear understanding of multithreading, with or without practical experience, you are likely to fail.

Almost every production application uses the multithreading paradigm

In practice on a real project, you would use an application server or its replacement. They are all based on multi-threaded solutions like thread pools. Any proper implementation on top of it requires concurrency consistency.

Thread and Runnable Definitions

Multithreading is based on Thread and Runnable. Thread is a class that starts a new independent activity and executes the instructions provided by the Runnable.

A thread is an entity attached to the operating system, so that's why it's a heavy class. Also Runnable is just a set of instructions - so that's why it's lightweight.

How to execute a new thread

A thread can execute instructions inside the currently running thread by using the Run() method. To run instructions in a new activity, Thread provides the Start() method.

How to reuse threads

A thread can perform many runnable tasks in it. Here is a more detailed article. Here you can see a very brief example with many runnables (tasks) running in one thread:

How to stop a thread

You can't just stop() or suspend() the thread. These methods are deprecated. You have to take care of interrupt design using isAlive() or isInterrupted()

thread daemon

A thread can be a daemon. Even if the last part won't execute, the daemon thread will be interrupted immediately. So such threads can be attached to resources. Otherwise, they may be the cause of resource or/and memory leaks.

How to use thread pools

As long as the Thread instance is heavy, it makes sense to use the ThreadPool class to reuse the same Thread. You can use different ThreadPool implementations depending on the thread.

Fixed thread pool

FixedThreadPool is a simple pool with a predefined number of threads. The number of threads does not change during this period. It makes sense to use:

cache thread pool

Contrary to Fixed Thread Pool, this one can dynamically increase the number of threads as more tasks are added. Each newly created thread will be active while in use, otherwise it will be deleted after 60 seconds of idleness.

How to define the number of threads in the thread pool

In order to use the best thread pool for your application, you need to know the following:

  • If your thread does a lot of computation, like video rendering, encryption, etc., then it eats up the process running that thread.
  • If your thread runs independently of CPU activity (such as network calls, memory calls, etc.), it will not consume the CPU on which its thread is running.

With this knowledge, you might come to the following conclusions:

  • Tasks with high CPU consumption tasks will not be allocated more threads than CPU cores.

  • For tasks with low CPU consumption, you can have more threads than CPU cores (but the ratio depends on the situation).

in conclusion:

This article only focuses on the main things about threads, runnables, and thread pool concepts, but doesn't cover them all. There are still many aspects that could play an important role. I hope you enjoy the infographics I use. If you think I've missed something important related to this topic, please leave your feedback. thanks for reading!

Guess you like

Origin blog.csdn.net/Trouvailless/article/details/124449260
Recommended