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

  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • How to Introduce a New API Quickly Using Micronaut
  • Introducing Graph Concepts in Java With Eclipse JNoSQL

Trending

  • IoT and Cybersecurity: Addressing Data Privacy and Security Challenges
  • After 9 Years, Microsoft Fulfills This Windows Feature Request
  • Optimizing Software Performance for High-Impact Asset Management Systems
  • Observability Expenses: When ‘Pennies on the Dollar’ Add Up Quickly
  1. DZone
  2. Coding
  3. Java
  4. Java: What is the Limit to the Number of Threads You Can Create?

Java: What is the Limit to the Number of Threads You Can Create?

By 
Peter Lawrey user avatar
Peter Lawrey
·
Jul. 26, 11 · Interview
Likes (1)
Comment
Save
Tweet
Share
147.9K Views

Join the DZone community and get the full member experience.

Join For Free

I have seen a number of tests where a JVM has 10K threads. However, what happens if you go beyond this?

My recommendation is to consider having more servers once your total reaches 10K. You can get a decent server for $2K and a powerful one for $10K.

Creating threads gets slower

The time it takes to create a thread increases as you create more thread. For the 32-bit JVM, the stack size appears to limit the number of threads you can create. This may be due to the limited address space. In any case, the memory used by each thread's stack add up. If you have a stack of 128KB and you have 20K threads it will use 2.5 GB of virtual memory.


BitnessStack SizeMax threads
32-bit 64K32,073
32-bit128K20,549
32-bit256K11,216
64-bit 64Kstack too small
64-bit128K32,072
64-bit512K32,072

Note: in the last case, the thread stacks total 16 GB of virtual memory.


Java 6 update 26 32-bit,-XX:ThreadStackSize=64

4,000 threads: Time to create 4,000 threads was 0.522 seconds 
8,000 threads: Time to create 4,000 threads was 1.281 seconds 
12,000 threads: Time to create 4,000 threads was 1.874 seconds 
16,000 threads: Time to create 4,000 threads was 2.725 seconds 
20,000 threads: Time to create 4,000 threads was 3.333 seconds 
24,000 threads: Time to create 4,000 threads was 4.151 seconds 
28,000 threads: Time to create 4,000 threads was 5.293 seconds 
32,000 threads: Time to create 4,000 threads was 6.636 seconds 
After creating 32,073 threads, java.lang.OutOfMemoryError: unable to create new native thread
 at java.lang.Thread.start0(Native Method)
 at java.lang.Thread.start(Thread.java:640)
 at com.google.code.java.core.threads.MaxThreadsMain.addThread(MaxThreadsMain.java:46)
 at com.google.code.java.core.threads.MaxThreadsMain.main(MaxThreadsMain.java:16)


Java 6 update 26 32-bit,-XX:ThreadStackSize=128

4,000 threads: Time to create 4,000 threads was 0.525 seconds 
8,000 threads: Time to create 4,000 threads was 1.239 seconds 
12,000 threads: Time to create 4,000 threads was 1.902 seconds 
16,000 threads: Time to create 4,000 threads was 2.529 seconds 
20,000 threads: Time to create 4,000 threads was 3.165 seconds 
After creating 20,549 threads, java.lang.OutOfMemoryError: unable to create new native thread
 at java.lang.Thread.start0(Native Method)
 at java.lang.Thread.start(Thread.java:640)
 at com.google.code.java.core.threads.MaxThreadsMain.addThread(MaxThreadsMain.java:46)
 at com.google.code.java.core.threads.MaxThreadsMain.main(MaxThreadsMain.java:16)


Java 6 update 26 32-bit,-XX:ThreadStackSize=128

4,000 threads: Time to create 4,000 threads was 0.526 seconds 
8,000 threads: Time to create 4,000 threads was 1.212 seconds 
After creating 11,216 threads, java.lang.OutOfMemoryError: unable to create new native thread
 at java.lang.Thread.start0(Native Method)
 at java.lang.Thread.start(Thread.java:640)
 at com.google.code.java.core.threads.MaxThreadsMain.addThread(MaxThreadsMain.java:46)
 at com.google.code.java.core.threads.MaxThreadsMain.main(MaxThreadsMain.java:16)


Java 6 update 26 64-bit,-XX:ThreadStackSize=128

4,000 threads: Time to create 4,000 threads was 0.577 seconds 
8,000 threads: Time to create 4,000 threads was 1.292 seconds 
12,000 threads: Time to create 4,000 threads was 1.995 seconds 
16,000 threads: Time to create 4,000 threads was 2.653 seconds 
20,000 threads: Time to create 4,000 threads was 3.456 seconds 
24,000 threads: Time to create 4,000 threads was 4.663 seconds 
28,000 threads: Time to create 4,000 threads was 5.818 seconds 
32,000 threads: Time to create 4,000 threads was 6.792 seconds 
After creating 32,072 threads, java.lang.OutOfMemoryError: unable to create new native thread
 at java.lang.Thread.start0(Native Method)
 at java.lang.Thread.start(Thread.java:640)
 at com.google.code.java.core.threads.MaxThreadsMain.addThread(MaxThreadsMain.java:46)
 at com.google.code.java.core.threads.MaxThreadsMain.main(MaxThreadsMain.java:16)



Java 6 update 26 64-bit,-XX:ThreadStackSize=512

4,000 threads: Time to create 4,000 threads was 0.577 seconds 
8,000 threads: Time to create 4,000 threads was 1.292 seconds 
12,000 threads: Time to create 4,000 threads was 1.995 seconds 
16,000 threads: Time to create 4,000 threads was 2.653 seconds 
20,000 threads: Time to create 4,000 threads was 3.456 seconds 
24,000 threads: Time to create 4,000 threads was 4.663 seconds 
28,000 threads: Time to create 4,000 threads was 5.818 seconds 
32,000 threads: Time to create 4,000 threads was 6.792 seconds 
After creating 32,072 threads, java.lang.OutOfMemoryError: unable to create new native thread
 at java.lang.Thread.start0(Native Method)
 at java.lang.Thread.start(Thread.java:640)
 at com.google.code.java.core.threads.MaxThreadsMain.addThread(MaxThreadsMain.java:46)
 at com.google.code.java.core.threads.MaxThreadsMain.main(MaxThreadsMain.java:16)


The Code

MaxThreadsMain.java

 

From http://vanillajava.blogspot.com/2011/07/java-what-is-limit-to-number-of-threads.html

Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • How to Introduce a New API Quickly Using Micronaut
  • Introducing Graph Concepts in Java With Eclipse JNoSQL

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!