Tuesday 7 April 2020

How many threads supported by Java?

Number of threads supported by Java is depend on many factors like CPU you are using, Operating System, RAM, Java release that you are using etc.,

For example, below application can able to check 'how many threads can be created in your system approximately?''

App.java
package com.sample.app;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class App {

 private static AtomicInteger atomicInteger = new AtomicInteger(0);

 public static void main(String args[]) {

  while (true) {
   Thread t1 = new Thread() {
    public void run() {
     try {
      System.out.println("Started thread " + atomicInteger.incrementAndGet());
      TimeUnit.MINUTES.sleep(10);
     } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
   };

   t1.start();
  }
 }

}



You may like

No comments:

Post a Comment