What are core threads in a ThreadPoolExecutor?

An SO User

I was looking at the ThreadPoolExecutor class and I found that it allows to specify the maximum pool size and the core pool size.

I understand, a little, about when to change the core and maximum pool sizes based on the answer here: When is specifying separate core and maximum pool sizes in ThreadPoolExecutor a good idea?

However, I would like to know what are these 'core threads'. I always get 0 when I use the getCorePoolSize() method of a ThreadPoolExecutor

SSCCE here:

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

public class PoolSize {
    public static void main(String[] args) {
        // Create a cached thread pool
        ExecutorService cachedPool = Executors.newCachedThreadPool();
        // Cast the object to its class type
        ThreadPoolExecutor pool = (ThreadPoolExecutor) cachedPool;

        // Create a Callable object of anonymous class
        Callable<String> aCallable = new Callable<String>(){
            String result = "Callable done !";
            @Override
            public String call() throws Exception {
                // Print a value
                System.out.println("Callable at work !");
                // Sleep for 5 sec
                Thread.sleep(0);
                return result;
            }
        };

        // Create a Runnable object of anonymous class
        Runnable aRunnable = new Runnable(){
            @Override
            public void run() {
                try {
                    // Print a value
                    System.out.println("Runnable at work !");
                    // Sleep for 5 sec
                    Thread.sleep(0);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        // Submit the two tasks for execution
        Future<String> callableFuture = cachedPool.submit(aCallable);
        Future<?> runnableFuture = cachedPool.submit(aRunnable);

        System.out.println("Core threads: " + pool.getCorePoolSize());
        System.out.println("Largest number of simultaneous executions: " 
                                            + pool.getLargestPoolSize());
        System.out.println("Maximum number of  allowed threads: " 
                                            + pool.getMaximumPoolSize());
        System.out.println("Current threads in the pool: " 
                                            + pool.getPoolSize());
        System.out.println("Currently executing threads: " 
                                            + pool.getTaskCount());

        pool.shutdown(); // shut down

    }
}
Peter Lawrey

core threads is the minimum which is always running just in case you want to pass it a task. The cached pool by default has a core of 0 as you might expect.

For the fixed thread pool, the core and the maximum are the same i.e. whatever you set the fixed size to.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

With ThreadPoolExecutor, what is the difference between allowCoreThreadTimeOut and zero core threads?

From Dev

ThreadPoolExecutor: how does it reuse threads

From Dev

ThreadPoolExecutor more threads=slower android

From Dev

ThreadPoolExecutor: how does it reuse threads

From Dev

Killing all workers/threads ThreadPoolExecutor

From Dev

Interrupt ThreadPoolExecutor current threads and empty its queue

From Dev

ThreadPoolExecutor not fully executing threads before shutting down

From Dev

How to access running threads inside ThreadPoolExecutor?

From Dev

What will be used for data exchange between threads are executing on one Core with HT?

From Dev

what does 2 core 4 threads mean in cpu?

From Dev

What are the ways to pass threadpoolexecutor to CompletableFuture?

From Dev

what is the function of ThreadPoolExecutor maximumPoolSize in java

From Dev

JAVA ThreadPoolExecutor is can be except main thread core?

From Java

How to get the ThreadPoolExecutor to increase threads to max before queueing?

From Dev

ThreadPoolExecutor similar to Executors.cachedThreadPool but with max threads and a queue

From Dev

How to get number of current running and queued threads from ThreadPoolExecutor?

From Dev

ThreadPoolExecutor similar to Executors.cachedThreadPool but with max threads and a queue

From Dev

How to wait until a ThreadPoolExecutor's threads have exited?

From Dev

Two threads one core

From Dev

multithreading - threads per core

From Dev

What is `threads` in the source panel

From Dev

What are uwsgi threads used for?

From Dev

Run threads in each core in Delphi

From Dev

Segmentation fault (core dumped) with Threads

From Dev

Two or more threads on single core

From Dev

With Hyper Threading, threads of one physical core are exchanging via what level of cache L1/L2/L3?

From Dev

What is a core?

From Dev

What are the alternatives to Perl interpreter threads?

From Dev

Java: Why/what are these threads monitoring?

Related Related

  1. 1

    With ThreadPoolExecutor, what is the difference between allowCoreThreadTimeOut and zero core threads?

  2. 2

    ThreadPoolExecutor: how does it reuse threads

  3. 3

    ThreadPoolExecutor more threads=slower android

  4. 4

    ThreadPoolExecutor: how does it reuse threads

  5. 5

    Killing all workers/threads ThreadPoolExecutor

  6. 6

    Interrupt ThreadPoolExecutor current threads and empty its queue

  7. 7

    ThreadPoolExecutor not fully executing threads before shutting down

  8. 8

    How to access running threads inside ThreadPoolExecutor?

  9. 9

    What will be used for data exchange between threads are executing on one Core with HT?

  10. 10

    what does 2 core 4 threads mean in cpu?

  11. 11

    What are the ways to pass threadpoolexecutor to CompletableFuture?

  12. 12

    what is the function of ThreadPoolExecutor maximumPoolSize in java

  13. 13

    JAVA ThreadPoolExecutor is can be except main thread core?

  14. 14

    How to get the ThreadPoolExecutor to increase threads to max before queueing?

  15. 15

    ThreadPoolExecutor similar to Executors.cachedThreadPool but with max threads and a queue

  16. 16

    How to get number of current running and queued threads from ThreadPoolExecutor?

  17. 17

    ThreadPoolExecutor similar to Executors.cachedThreadPool but with max threads and a queue

  18. 18

    How to wait until a ThreadPoolExecutor's threads have exited?

  19. 19

    Two threads one core

  20. 20

    multithreading - threads per core

  21. 21

    What is `threads` in the source panel

  22. 22

    What are uwsgi threads used for?

  23. 23

    Run threads in each core in Delphi

  24. 24

    Segmentation fault (core dumped) with Threads

  25. 25

    Two or more threads on single core

  26. 26

    With Hyper Threading, threads of one physical core are exchanging via what level of cache L1/L2/L3?

  27. 27

    What is a core?

  28. 28

    What are the alternatives to Perl interpreter threads?

  29. 29

    Java: Why/what are these threads monitoring?

HotTag

Archive