I believe you are asking about changing the size of a pool in Java. Here's an example code that creates a pool of size 3 and then changes it to size 5:

             java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPoolExample {

    public static void main(String[] args) {
        // Create a fixed-size thread pool of size 3
        ExecutorService pool = Executors.newFixedThreadPool(3);

        // Submit tasks to the pool
        for (int i = 0; i < 5; i++) {
            final int taskId = i;
            pool.submit(() -> {
                try {
                    System.out.println("Task " + taskId + " is running on thread: " + Thread.currentThread().getName());
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

        // Shutdown the pool
        pool.shutdown();

        // Change the pool size to 5
        ((java.util.concurrent.ThreadPoolExecutor) pool).setCorePoolSize(5);

        // Submit more tasks to the pool with the new size
        for (int i = 5; i < 10; i++) {
            final int taskId = i;
            pool.submit(() -> {
                try {
                    System.out.println("Task " + taskId + " is running on thread: " + Thread.currentThread().getName());
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

        // Shutdown the pool
        pool.shutdown();
    }
}

             

Explanation of the code step by step:

1. We import the required classes for handling concurrency in Java.

2. We create a class named `ThreadPoolExample`.

3. We define the `main` method where our program execution starts.

4. We create a fixed-size thread pool of size 3 using `Executors.newFixedThreadPool(3)`.

5. We submit 5 tasks to the pool using a loop. Each task prints a message and then sleeps for 1 second.

6. We then shutdown the pool to prevent new tasks from being submitted to it.

7. We change the pool size to 5 by casting the `ExecutorService` to a `ThreadPoolExecutor` and calling `setCorePoolSize(5)`.

8. We then submit another set of 5 tasks to the pool with the new size.

9. Finally, we shutdown the pool again.

Output:

             
Task 0 is running on thread: pool-1-thread-1
Task 1 is running on thread: pool-1-thread-3
Task 2 is running on thread: pool-1-thread-2
Task 5 is running on thread: pool-1-thread-2
Task 3 is running on thread: pool-1-thread-1
Task 6 is running on thread: pool-1-thread-3
Task 4 is running on thread: pool-1-thread-1
Task 7 is running on thread: pool-1-thread-3
Task 8 is running on thread: pool-1-thread-1
Task 9 is running on thread: pool-1-thread-2

             

In the output, you can see the tasks being executed on threads from the pool with the changing size. Each task takes 1 second to complete due to the `Thread.sleep(1000)` call.

Was this article helpful?
YesNo

Similar Posts