java use thread pool executor

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TestThread {
	
   public static void main(final String[] arguments) throws InterruptedException {
      ThreadPoolExecutor executor = (ThreadPoolExecutor)Executors.newCachedThreadPool();

      //Stats before tasks execution
      System.out.println("Largest executions: "
         + executor.getLargestPoolSize());
      System.out.println("Maximum allowed threads: "
         + executor.getMaximumPoolSize());
      System.out.println("Current threads in pool: "
         + executor.getPoolSize());
      System.out.println("Currently executing threads: "
         + executor.getActiveCount());
      System.out.println("Total number of threads(ever scheduled): "
         + executor.getTaskCount());

      executor.submit(new Task());
      executor.submit(new Task());

      //Stats after tasks execution
      System.out.println("Core threads: " + executor.getCorePoolSize());
      System.out.println("Largest executions: "
         + executor.getLargestPoolSize());
      System.out.println("Maximum allowed threads: "
         + executor.getMaximumPoolSize());
      System.out.println("Current threads in pool: "
         + executor.getPoolSize());
      System.out.println("Currently executing threads: "
         + executor.getActiveCount());
      System.out.println("Total number of threads(ever scheduled): "
         + executor.getTaskCount());

      executor.shutdown();
   }  

   static class Task implements Runnable {

      public void run() {

         try {
            Long duration = (long) (Math.random() * 5);
            System.out.println("Running Task! Thread Name: " +
               Thread.currentThread().getName());
            TimeUnit.SECONDS.sleep(duration);
            System.out.println("Task Completed! Thread Name: " +
               Thread.currentThread().getName());
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
   }
}

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
threadpoolexecutor java 6 example Java Thread Pool – ThreadPoolExecutor Example threadpoolexecutor java example java threadpoolexecutor example thread-pool-executor trong java executorservice java thread pool thread executor pool in java how thread pool executor works in java java thread pool executor service definition use of threadpoolexecutor in java thread pool executor awaittermination example in java thread pool executor example thread pool executor android example executor thread pool java best way to use thread pool executor what is threadpoolexecutor in java Executors thread pool different how many threads should be used thread pool java executors thread pool executor submit java pool executor executor java thread pool java executorservice thread pool how to runnable thread to executor pool java thread pool executor threadpoolexecutor source java How thread pool executor works internally how does thread pool executor works executor thread pool example threadpoolexecutor java Thread pool executor usage code what is a java thread pool executor executor thread pool identify threads in executor pool java what is process pool thread pool and defalut executor java threadpool java thread pool example ExecutorService fixed thread pool java worker pool java threadpoolexecutor java pool of threads creating a thread pool in java thread pool in java 8 single thread executor java how to create thread pool in java without executor service java queue with thread pool create a thread pool in java thread pool in java threadpool in java thread pool crashing executor pool java thread pool task executor threadpoolexecutor example threadpool join java how to add task to threadpool java java update class properties in threadpool java.util.concurrent.ThreadPoolExecutor java executor manager java httpserver thread pool executor thread pool example java unlimited thread executor when to create thread pool thread pool scheduler java java thread pool threadpool executor executorService = Executors.newFixedThreadPool(5); thread pool method in java thread pool executor java threadpoolexecutor javaù threadpoolexecutor thread pool executor in java java thread pool executor example java thread manager threadpoolexecutor 2 threads examples threadpoolexecutor examples java executpool new thread pool executor thread pool java java execute code in thread pool java 8 create thread pool for code execution thread pool executor thread pool which can create executor class a thread pool that creates as many threads it needs to execute the task in parallel threadpoolexecutor in java java create thread pool Runnable thread pools ThreadPool java java use thread pool executor
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source