java线程池RejectedExecutionException出现的三种典型场景:Terminated,Shutting down,Running

本文深入探讨了Java中线程池的各种状态,包括TERMINATED、SHUTDOWN和RUNNING,解析了每种状态下线程池的行为特征及可能出现的问题,如RejectedExecutionException异常的抛出。通过具体代码示例,展示了不同状态下的线程池如何处理任务提交。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TERMINATED

java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@2cd3e0cd rejected from java.util.concurrent.ThreadPoolExecutor@77da21eb[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]

package com.company;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ExecutorDemo {
    public static void main(String[] args) {
    ExecutorService pool =
        new ThreadPoolExecutor(2, 2, 10000, TimeUnit.MILLISECONDS, new LinkedTransferQueue<>());

        pool.shutdown();

        for(int i=0;i<10;i++) {
            pool.submit(new Task());
        }
    }

    private static class Task implements Callable<Integer> {
        public Integer call() {
            try {
                System.out.println(Thread.currentThread().getName() + " running...");
                Thread.sleep(10000l);
            } catch(InterruptedException e) {
                e.printStackTrace();
            }
            return 1;
        }
    }
}

SHUTDOWN

java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@2cd3e0cd rejected from java.util.concurrent.ThreadPoolExecutor@77da21eb[Shutting down, pool size = 2, active threads = 2, queued tasks = 8, completed tasks = 0]

package com.company;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ExecutorDemo {
    public static void main(String[] args) {
    ExecutorService pool =
        new ThreadPoolExecutor(2, 2, 10000, TimeUnit.MILLISECONDS, new LinkedTransferQueue<>());

        for (int i = 0; i < 10; i++) {
            pool.submit(new Task());
        }

        pool.shutdown();

        for(int i=0;i<10;i++) {
            pool.submit(new Task());
        }
    }

    private static class Task implements Callable<Integer> {
        public Integer call() {
            try {
                System.out.println(Thread.currentThread().getName() + " running...");
                Thread.sleep(10000l);
            } catch(InterruptedException e) {
                e.printStackTrace();
            }
            return 1;
        }
    }
}


RUNNING

java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@2cd3e0cd rejected from java.util.concurrent.ThreadPoolExecutor@77da21eb[Running, pool size = 2, active threads = 2, queued tasks = 0, completed tasks = 0]

package com.company;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ExecutorDemo {
    public static void main(String[] args) {
    ExecutorService pool =
        new ThreadPoolExecutor(2, 2, 10000, TimeUnit.MILLISECONDS, new SynchronousQueue<>());

        for (int i = 0; i < 10; i++) {
            pool.submit(new Task());
        }
    }

    private static class Task implements Callable<Integer> {
        public Integer call() {
            try {
                System.out.println(Thread.currentThread().getName() + " running...");
                Thread.sleep(10000l);
            } catch(InterruptedException e) {
                e.printStackTrace();
            }
            return 1;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值