thread pool java

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();
         }
      }
   }
}

3.67
3
Bob432 80 points

                                    
package com.journaldev.threadpool;

public class WorkerThread implements Runnable {
  
    private String command;
    
    public WorkerThread(String s){
        this.command=s;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+" Start. Command = "+command);
        processCommand();
        System.out.println(Thread.currentThread().getName()+" End.");
    }

    private void processCommand() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String toString(){
        return this.command;
    }
}

3.67 (3 Votes)
0
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 how to write thread pool in java execute thread pool java threadpoolexecutor java example java thread pool class thread pool manager java java threadpoolexecutor example thread-pool-executor trong java java thread pool nedir thread pool java dev to how to create thread pool in java java threads pool thread pool service executorservice java thread pool using thread pool in java thread pool what is it thread executor pool in java how thread pool executor works in java java thread pool executor service definition java thread pool easy example Thread pool. use of threadpoolexecutor in java thread pool executor awaittermination example in java thread pool executor example thread pool executor android example thread pool implementation in java executor thread pool java best way to use thread pool executor what is threadpoolexecutor in java wait thread pool java java fixed thread pool what is thread pooling in java Executors thread pool different what is pool thread thread pool in java medium Java pool thread how many threads should be used thread pool java executors thread pool executor submit thread pool types in java Discuss the benefits of using a thread pool? in java java pool executor the thread pool when is a thread pool used executor java thread pool what is thread pool in java with example thread pool java practical; example pooling thread what is the thread pool using a thread pool java types of thread pool in java java executorservice thread pool implement thread pool in java amount of threads for thread pool java how to create a thread pool in java 15 how to create a thread pool in java how to runnable thread to executor pool java thread pool executor threadpoolexecutor source java thread pooling custom thread pool in java How thread pool executor works internally java thread Pool state thread pooling in java programiz thread pooling in java use a thread pool java thread pool in java how does thread pool executor works thread pool tutorial java EE thread pool how to use thread pool in java Java Thread Pool programiz \Java Thread Pool executor thread pool example java how to make your own thread pool THread pool java example >THread pool java java threading pool thread pool implementation what is thread pool in java threadpoolexecutor java java thread pool examples thread poolinf in java Thread pool executor usage code thread pool type in java java pool de threads what is a java thread pool executor what is a thread pool executor thread pool use 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 pools java thread pool in java 8 single thread executor java java spring boot thread pool get thread number of thread pool java how to create thread pool in java without executor service how to create a thread pool java queue with thread pool create a thread pool in java thread pool in java threadpool in java thread pool crashing make thread pool java executor pool java thread pool task executor threadpoolexecutor example threadpool join java how to add task to threadpool java how many thread pools are there in java java update class properties in threadpool thread pool c# thread pool python java.util.concurrent.ThreadPoolExecutor threading pool java executor manager java httpserver thread pool executor thread pool example java unlimited thread executor when to create thread pool best way to create a thread pool with multiple threads different types of thread pool in java thread pool scheduler java java thread pool join in thread pool java threadpool executor how to make a threadpool in java ThreadPools in java craeting threads and thfeadpool in java executorService = Executors.newFixedThreadPool(5); thread pool method in java thread pool java projects THREAD POOL PROJECT thread pool executor java what is thread pool threadpoolexecutor javaù threadpoolexecutor Thread pools in java thread pool executor in java java thread pool executor example java thread manager thread pooling java thread pool multithreading classes java thread pools pool of threads java 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