DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Java Virtual Threads and Scaling
  • Virtual Threads: A Game-Changer for Concurrency
  • Deep Dive Into Java Executor Framework
  • Java Thread Dump Analysis

Trending

  • Using Python Libraries in Java
  • Caching 101: Theory, Algorithms, Tools, and Best Practices
  • Designing Fault-Tolerant Messaging Workflows Using State Machine Architecture
  • ITBench, Part 1: Next-Gen Benchmarking for IT Automation Evaluation
  1. DZone
  2. Coding
  3. Java
  4. What Every Java Developer Should Know About Thread, Runnable, and Thread Pool

What Every Java Developer Should Know About Thread, Runnable, and Thread Pool

Most important information about Thread, Runnable, and Thread Pool notions in Java with clear examples, infographics, and explanations.

By 
Dmitry Egorov user avatar
Dmitry Egorov
DZone Core CORE ·
Mar. 21, 22 · Tutorial
Likes (22)
Comment
Save
Tweet
Share
25.0K Views

Join the DZone community and get the full member experience.

Join For Free

Multithreading Is Most Complex And Biggest Part Of Java

Multithreading chapters are the most difficult to understand and use in Java. Unfortunately, there are not that many sources where you can get all the answers. Meanwhile, concurrency knowledge is critically important. In this article, I explain the core aspects of multithreading that every Java developer has to know. In this part, we start with the Thread and Runnable subject.

Why Is Concurrency Knowledge So Critical?

You Can't Get Senior Java Job Without Good Knowledge Of Multithreading

Multithreading knowledge almost certainly is the subject of interviews for senior Java positions. Without a clear understanding of multithreading with and without hands-on experience, you most likely will fail.  

Benefits of Senior Java Jobs

Almost Every Production Application Uses Multithreading Paradigm

In practice in real projects, you will use application servers or their alternatives. All of them are based on multithreading solutions like thread pools, etc. Any proper implementation on top of it requires concurrency consistency. 

Production Applications Graphic: Apache Tomcat, Spring, and WildFly

Thread and Runnable Definitions

Multithreading is based on Thread and Runnable. Thread is a class that starts new independent activity and performs instructions supplied by Runnable.

Thread vs. Runnable Graphic

Thread is an entity that is attached to the OS so this is why it's a heavy class. Meanwhile Runnable is just a set of instructions — so this is why it's lightweight.

Thread vs. Runnable Weight Comparison

How to Execute New Thread

Thread can execute instructions inside the current running thread by using Run() method. In order to run instructions in a new activity, Thread provides Start() method.

Java
 
(new Thread()).run(); // run in current thread
(new Thread()).start(); // run in new thread


Flow Example: Executing New Thread

How to Reuse Thread

Thread can perform many runnables inside of it. Here is an article with more details. Here you can see a very short example with many runnables (tasks) run inside one single thread:

Java
 
List<Runnable> tasks = new ArrayList<Runnable>();
(new Thread(() -> {
  for (Runnable task : tasks) {
    task.run();
  }
})).start();


Multiple Runnables Running a Single Thread

How to Stop Thread

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

Java
 
(new Thread(() -> {
    while(true){
      // process a peace of logic
      if(Thread.currentThread().isInterrupted()){
        //handle exiting
        break;
      }
    }
})).start();


Do's and Don'ts of Thread Stopping


Thread Daemon

Thread can be a daemon. Daemon threads are interrupted instantly even the final part won't be executed. So such threads can be attached to resources. Otherwise, they can be the reason for resource or/and memory leaks.

Java
 
Thread thread = (new Thread(() -> {
    try {
      // working with resource
    } finally {
      // resource.close(); - might won't work because finally if
      // deamon been interrupted
    }
}));
thread.setDaemon(true);
thread.start();


Daemon Threads

How To Use Thread Pool

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

Fixed Thread Pool

FixedThreadPool is a simple pool with a predefined number of threads. The number of threads won't be changed during. It makes sense to use it:

Fixed Thread Pool Over Time

Cached Thread Pool

Opposite to Fixed Thread Pool, this one can increase the number of threads dynamically when more tasks are added. Each newly created thread will be alive while it's used otherwise it will be removed after 60 seconds of idle.

Cached Thread Pool Over Time

How To Define Number Of Threads In Thread Pool

In order to use the best Thread Pool for your application you need to understand next things:

  • If your thread does a lot of computation like video rendering, encryption, etc., then it eats processes that run that thread.
  • If your thread runs not related to CPU activity like networking calls, memory calls, etc., then it won't consume the CPU that runs its thread.

High Computation vs. Low Computation Activities

Relying on this knowledge you might make the next conclusions:

  • Having high CPU consumable tasks don't allocate more threads than CPU cores

High CPU Consumable Tasks vs. CPU Cores

  • Having low CPU consumable tasks you can have a number of threads more than CPU cores (but proportion depends on the specific situation).

Low CPU Consumable Tasks vs. CPU Cores

Conclusion

This article highlights just the main thing about Thread, Runnable, and Thread Pool notions but doesn't cover it completely. There are still many aspects that might play an important role. I hope you liked the infographics I used. Please leave your feedback if you think that I missed something critically important related to this subject. Thanks for reading!

Thread pool Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Java Virtual Threads and Scaling
  • Virtual Threads: A Game-Changer for Concurrency
  • Deep Dive Into Java Executor Framework
  • Java Thread Dump Analysis

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!