• Java线程--ThreadPoolExecutor使用


    原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11871132.html

    Java线程--ThreadPoolExecutor使用

    public static void main(String[] args) {
            ExecutorService service = new ThreadPoolExecutor(1, 2, 1000, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(),Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
            for(int i=0 ; i<3 ; i++) {
                service.execute(new ThreadPoolExecutorTask());
            }
        }
    /**
     * 线程池执行器任务
     */
    class ThreadPoolExecutorTask implements Runnable{
    
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName());
        }
    }

    打印结果如下:

    Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task concurrent.ThreadPoolExecutorTask@6d6f6e28 rejected from java.util.concurrent.ThreadPoolExecutor@135fbaa4[Running, pool size = 2, active threads = 2, queued tasks = 0, completed tasks = 0]
        at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
        at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
        at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)
        at concurrent.MainConcurrent.main(MainConcurrent.java:37)
    pool-1-thread-1
    pool-1-thread-2

    可以看出, 超出最大的线程池设置大小之后, 就会报异常!

    其他参数不过多做介绍.

  • 相关阅读:
    我知道点redis-数据结构与对象
    白帽子-第十四章 PHP安全
    白帽子-第二篇 客户端脚本安全
    网络编程
    inline的作用
    Windows静态库和动态库区别
    简单实现图片上传预览
    Java 通用正则表达式
    C#+Mysql 图片数据存储
    FileUpload转换为字节
  • 原文地址:https://www.cnblogs.com/fanerwei222/p/11871132.html
Copyright © 2020-2023  润新知