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
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
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

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • All You Need To Know About Garbage Collection in Java
  • Performance Engineering Management: A Quick Guide
  • Java Memory Management
  • Counting Distinct Users in Real-Time With Redis Using Low Memory

Trending

  • How To Verify Database Connection From a Spring Boot Application
  • Best Plugins For JetBrains IDEs
  • Effective Tips for Debugging Complex Code in Java
  • Parallelism in ConcurrentHashMap
  1. DZone
  2. Data Engineering
  3. Data
  4. Why We Shouldn't Use More Threads Than We Need To

Why We Shouldn't Use More Threads Than We Need To

Peter Lawrey user avatar by
Peter Lawrey
·
Nov. 13, 13 · Interview
Like (0)
Save
Tweet
Share
7.00K Views

Join the DZone community and get the full member experience.

Join For Free

There is a common argument that because we have lots of cores, and will have even more in the future we have to use them.  We just we need to find the best ways to use them but just because we can doesn't mean we should.

What is our goal?

Good reasons to use multiple threads are
  • the performance of using one thread is not enough.
  • you have profiled your application to ensure there is no low hanging fruit.
  • multiple threads improve the throughput,  latency or consistency.
At this point you should add a thread when you know it gets you closer to your goal.

A bad reason to use multiple threads

Just because we can use more threads doesn't mean we should.  Multiple threads
  • Adds complexity to the code
  • There are other was to speed up an application.  You L1 cache is 10-20x faster than you L3 cache and if you can spend more time in you L1 cache by optimising your memory usage and access, you can gain more performance than using every CPU in your socket.
  • Multiple thread can introduce subtle, rarely seen bugs which just wouldn't be there with single threaded code.
  • Multiple threads adds synchronization, more use of immutable objects instead of recycling mutable one.
  • Multiple threads tend to lead to much worse jitter and worse case performance even if the typical performance is better.

In short, multi-threading more likely to slow down a program than speed it up unless some thought is put into it.  Two CPUs can be twice as fast at best but can easily be ten times slower if you are not careful. i.e. you have more to lose than you can gain.

A simple example of this is calculating Fibonacci numbers.  These are very easy to describe recursively and create lots of threads.  Thus calculation Fibonacci numbers are often used as a example of how to use lots of threads.  What they often don't mentions is that the number of threads you create is equal to the answer i.e. it grow exponentially.  This means that while iterating in one loop/thread take about 4 ms to compute fib(69), the multi-threaded version will create trillions of trillions of threads and will take longer than the age of the universe if it didn't crash.

But if I have CPUs idle I am wasting them.

If you want to use every CPU, just write a busy waiting thread for every CPU and you are done, every CPU is at 100%

Say you want to travel from A to B,  sometimes you can take one street and sometimes taking four streets is faster.  But there are 20 streets near A and B and you should go up and down all twenty street because otherwise there is no point them being there, right!?

Conclusion

If you are focused on engineering your system, for ease of development and maintainability, you want the simplest solution to which will solve your problem. If that means you don't use 100% of your network bandwidth, or 100% of your disk space or 100% of your memory or 100% of your CPUs, perhaps that is a good thing.

Busy waiting application Cache (computing) Memory (storage engine) Ease (programming language) Fruit (software) Maintainability Space (architecture)

Published at DZone with permission of Peter Lawrey, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • All You Need To Know About Garbage Collection in Java
  • Performance Engineering Management: A Quick Guide
  • Java Memory Management
  • Counting Distinct Users in Real-Time With Redis Using Low Memory

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • 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: