thread pool java

Solutions on MaxInterview for thread pool java by the best coders in the world

showing results for - "thread pool java"
Daniela
02 Aug 2020
1import java.util.concurrent.Executors;
2import java.util.concurrent.ThreadPoolExecutor;
3import java.util.concurrent.TimeUnit;
4
5public class TestThread {
6	
7   public static void main(final String[] arguments) throws InterruptedException {
8      ThreadPoolExecutor executor = (ThreadPoolExecutor)Executors.newCachedThreadPool();
9
10      //Stats before tasks execution
11      System.out.println("Largest executions: "
12         + executor.getLargestPoolSize());
13      System.out.println("Maximum allowed threads: "
14         + executor.getMaximumPoolSize());
15      System.out.println("Current threads in pool: "
16         + executor.getPoolSize());
17      System.out.println("Currently executing threads: "
18         + executor.getActiveCount());
19      System.out.println("Total number of threads(ever scheduled): "
20         + executor.getTaskCount());
21
22      executor.submit(new Task());
23      executor.submit(new Task());
24
25      //Stats after tasks execution
26      System.out.println("Core threads: " + executor.getCorePoolSize());
27      System.out.println("Largest executions: "
28         + executor.getLargestPoolSize());
29      System.out.println("Maximum allowed threads: "
30         + executor.getMaximumPoolSize());
31      System.out.println("Current threads in pool: "
32         + executor.getPoolSize());
33      System.out.println("Currently executing threads: "
34         + executor.getActiveCount());
35      System.out.println("Total number of threads(ever scheduled): "
36         + executor.getTaskCount());
37
38      executor.shutdown();
39   }  
40
41   static class Task implements Runnable {
42
43      public void run() {
44
45         try {
46            Long duration = (long) (Math.random() * 5);
47            System.out.println("Running Task! Thread Name: " +
48               Thread.currentThread().getName());
49            TimeUnit.SECONDS.sleep(duration);
50            System.out.println("Task Completed! Thread Name: " +
51               Thread.currentThread().getName());
52         } catch (InterruptedException e) {
53            e.printStackTrace();
54         }
55      }
56   }
57}
Thea
28 Aug 2020
1
2package com.journaldev.threadpool;
3
4public class WorkerThread implements Runnable {
5  
6    private String command;
7    
8    public WorkerThread(String s){
9        this.command=s;
10    }
11
12    @Override
13    public void run() {
14        System.out.println(Thread.currentThread().getName()+" Start. Command = "+command);
15        processCommand();
16        System.out.println(Thread.currentThread().getName()+" End.");
17    }
18
19    private void processCommand() {
20        try {
21            Thread.sleep(5000);
22        } catch (InterruptedException e) {
23            e.printStackTrace();
24        }
25    }
26
27    @Override
28    public String toString(){
29        return this.command;
30    }
31}
32
queries leading to this page
when to create thread poolwhat is a thread poolthread pool exampleexecute thread pool javathread pool in java mkyonghow to write thread pool in javajava pool de threadsexecutor thread pool examplejava thread pool easy examplehow does thread pool executor worksbest way to use thread pool executorjava thread pool task executorwhat is a java thread pool executorjava unlimited thread executorjava pool executortypes of thread pool in javathread pool pythonhow to create a thread pool in java 15what is thread pool in java with examplepool threadhow to create a thread pool in javause thread poolthread pool executorthe thread poolwhat is pool threadmultithreading classes java thread poolsuse of threadpoolexecutor in javathread pool executor usage codecreating a thread pool in javajava thread pool classdesigning a thread pool in javajava thread pool programizthread pool which can create executor classhow to create a thread pool for the whole project javathread pool executor submitthreadpool join javajava pool of threadsthreadpoolexecutor java 6 examplewhat is thread pool in javathread pool manager javaa thread pool that creates as many threads it needs to execute the task in paralleljava thread pool executor examplethreading poolpool thread javathreading pool executorcreate a thread pool in javaamount of threads for thread pool javathreadpool javathread pool in java 8thread pool in java mediumcreate thread pool in javahow to create a thread pooljava thread pool executorpooling threadjoin in thread pool javathread pooling in java programizthreadpool executorjava execute code in thread pooljava update class properties in threadpoolthreadpools in javacustom thread pool in javathreadpoolexecutor 2 threads examplesjava how to make your own thread poolexecutor poolthread pool in javaexecutorservice 3d executors newfixedthreadpool 285 29 3bdiscuss the benefits of using a thread pool 3f in javamake thread pool javawhat is thread poolhow to create thread pool in java without executor servicethreadpoolexecutor java c3 b9 thread pool java threadpoolexecutorthread pool java tutorialdifferent types of thread pool in javaexecutor thread pool javajava executorservice thread poolexecutorservice fixed thread poolwhat is thread pooling in javanew thread pool executorspring executor thread poolthread pool executor in javathread pool service 5cjava thread poolhow to use thread pool in javaglobal thread pool javacraeting threads and thfeadpool in javathread pool c 23java thread pool nedirthread pool crashingexecutorservice java thread pool 3ethread pool javahow to runnable thread to executor poolthread add to pool executorthread pool executor trong javajava create thread pooluse a thread pool javausing a thread pool javathread executor pool in javajava thread pooljava threads poolthread poolingthreadpoolexecutor javarunnable thread poolsjava spring boot thread poolthread pools in javajava 8 create thread pool for code executionthread pool java practical 3b examplejava thread pool executor waitthread pool and executor in javathread pool java dev tojava use thread pool executorjava thread pool e2 80 93 threadpoolexecutor examplehow to add task to threadpool javajava httpserver thread pool executorthread pool java examplejava threadpoolexecutor examplehow many thread pools are there in javaexecutors thread pool differentpool executor javathread pool tutorialdifferent ways to create thread pool in javajava queue with thread poolthread pool type in javajava how to make a thread poolthread pool executor android exampleget thread number of thread pool javajava create a thread poolthreadpoolexecutor in javawhat is the thread poolidentify threads in executor pool javajava thread pool examplethreadpool in javajava util concurrent threadpoolexecutorjava thread pool examplesthread poolinf in javahow does a thread pool workthread pooling in javawhat is a thread pool in javahow to create thread pool in javajava worker pooljava ee thread pooljava pool threadthread pools javathreadpoolexecutor use in javahow to make a threadpool in javahow does thread pool work in javathreading threadpool javajava fixed thread pooljava threadpoolthreadpoolexecutor source javapool of threads javaexecutor thread pooljava thread managerthread pool executor in java 8 examplewait thread pool javathread pool scheduler javasingle thread executor javathread pool java projectsjava thread pool statethread poolthread pool javaimplement thread pool in javahow many threads should be used thread pool java executorsthread pool in javathread pool types in javahow to get thread poolthread pool executor examplehow thread pool executor works internallythreadpoolexecutorjava executor managerhow many ways to create thread pool in javausing thread pool in javajava thread pool executor service definitionthread pool projectbest way to create a thread pool with multiple threadsthread pool executor awaittermination example in javawhen is a thread pool usedthread pool implementationthreadpoolexecutor exampleswhat is process pool thread pool and defalut executorthreadpoolexecutor examplethread pooling javaexecutor java thread pooljava threading pooljava executpoolhow thread pool executor works in javathread pool executor javathreadpoolexecutor java examplethread pool method in javathread pool what is itwhat is threadpoolexecutor in javathread pool implementation in javathread pool java