The problem test of whether the thread pool needs to be closed

IDEA configures -xmx and -xms

1. Not closing the thread pool will cause OOM

    static AtomicInteger integer = new AtomicInteger(0);
    static ExecutorService es;
    private static void execute(){
        es = Executors.newFixedThreadPool(3);
        es.execute(() -> {
            integer.getAndAdd(1);
            System.out.println("hello" + integer.get());
        });
    }

    public static void main(String[] args) {

        while (true) {
            execute();
        }
    }
hello388
hello388
hello376
hello376Exception in thread "main" 
hello376
hello376
hello376
hello376
hello376
hello376
java.lang.OutOfMemoryError: Java heap space
    at java.util.concurrent.Executors$DefaultThreadFactory.newThread(Executors.java:612)
    at java.util.concurrent.ThreadPoolExecutor$Worker.<init>(ThreadPoolExecutor.java:612)
hello402

It means that the reference es of the thread pool points to the newly created thread pool, and the threads in the old thread pool have not been recycled.

2. The shutdown thread pool is not recycled

    static AtomicInteger integer = new AtomicInteger(0);
    private static void execute(){
        ExecutorService es = Executors.newFixedThreadPool(3);
        es.execute(() -> {
            integer.getAndAdd(1);
            System.out.println("hello" + integer.get());
        });
    }

    public static void main(String[] args) {

        while (true) {
            execute();
        }
    }

output:

hello402
hello400
hello400
hello398
hello396
Exception in thread "pool-396-thread-1" java.lang.OutOfMemoryError: GC overhead limit exceeded

The thread pool is not closed, the thread is not recycled, so OOM

3. Close the thread after using the thread pool

    static AtomicInteger integer = new AtomicInteger(0);
    private static void execute(){
        ExecutorService es = Executors.newFixedThreadPool(3);
        es.execute(() -> {
            integer.getAndAdd(1);
            System.out.println("hello" + integer.get());
        });
        es.shutdown();
    }

    public static void main(String[] args) {

        while (true) {
            execute();
        }
    }

The output is OK:

hello7757
hello7758
hello7759
hello7760
hello7761
hello7762

4. Thread pool variables as global variables

static AtomicInteger integer = new AtomicInteger(0);
    static ExecutorService es = Executors.newFixedThreadPool(3);
    private static void execute(){
        es.execute(() -> {
            integer.getAndAdd(1);
            System.out.println("hello" + integer.get());
        });
    }

    public static void main(String[] args) {

        while (true) {
            execute();
        }
    }
hello86
hello119hello121
hello124
hello124
hello124
Exception in thread "pool-1-thread-1" hello126
Exception in thread "pool-1-thread-4" hello127Exception in thread "pool-1-thread-3" java.lang.OutOfMemoryError: Java heap space

Because of the newFixedThreadPool used, the infinite blocking queue used will cause OOM

5. Increase the rejection policy and set the thread pool related parameters:

    static AtomicInteger integer = new AtomicInteger(0);
    static ExecutorService es = new ThreadPoolExecutor(3, 4, 1, TimeUnit.MILLISECONDS,
            new ArrayBlockingQueue<>(5), new ThreadPoolExecutor.CallerRunsPolicy());
    private static void execute(){
        es.execute(() -> {
            integer.getAndAdd(1);
            System.out.println("hello" + integer.get());
        });
    }

    public static void main(String[] args) {

        while (true) {
            execute();
        }
    }

output:

hello264551
hello264552
hello264553
hello264554
hello264555
hello264556

The queue size is set small, and the rejection policy is added, which can be executed well.

summary:

1) After the thread pool is executed, remember to execute shutdown to recycle the thread pool resources. Simply set the thread pool reference to empty, and the GC cannot recycle the thread pool resources well.

2) The thread pool should set reasonable parameters (queue size, type, number of core threads, rejection policy), and pay attention to the OOM problem when using an infinite size queue.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324142306&siteId=291194637