Java并发编程 - ThreadPoolExecutor 应用

简介: Java并发编程 - ThreadPoolExecutor 应用

newCachedThreadPool:       可缓存线程池,可灵活回收空闲线程

newFixedThreadPool:          定长线程池,可控制线程的最大并发数,超出的线程会在队列中等待

newScheduledThreadPool:  定时线程池,支持定时和周期性任务的执行

newSingleThreadExecutor:  单线程化的线程池,保证所有任务按照指定顺序(先入先出,优先级等)执行


newCachedThreadPool

packagecom.mmall.concurrency.example.threadPool;
importlombok.extern.slf4j.Slf4j;
importjava.util.concurrent.ExecutorService;
importjava.util.concurrent.Executors;
@Slf4jpublicclassThreadPoolExample1 {
publicstaticvoidmain(String[] args) {
ExecutorServiceexecutorService=Executors.newCachedThreadPool();
for (inti=0; i<10; i++) {
finalintindex=i;
executorService.execute(newRunnable() {
@Overridepublicvoidrun() {
log.info("task:{}", index);
                }
            });
        }
executorService.shutdown();
    }
}
// 输出10:57:37.683 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:010:57:37.683 [pool-1-thread-8] INFOc.m.c.example.threadPool.test9-task:710:57:37.683 [pool-1-thread-6] INFOc.m.c.example.threadPool.test9-task:510:57:37.683 [pool-1-thread-10] INFOc.m.c.example.threadPool.test9-task:910:57:37.683 [pool-1-thread-3] INFOc.m.c.example.threadPool.test9-task:210:57:37.683 [pool-1-thread-9] INFOc.m.c.example.threadPool.test9-task:810:57:37.683 [pool-1-thread-4] INFOc.m.c.example.threadPool.test9-task:310:57:37.683 [pool-1-thread-5] INFOc.m.c.example.threadPool.test9-task:410:57:37.683 [pool-1-thread-2] INFOc.m.c.example.threadPool.test9-task:110:57:37.683 [pool-1-thread-7] INFOc.m.c.example.threadPool.test9-task:6Processfinishedwithexitcode0

newFixedThreadPool

packagecom.mmall.concurrency.example.threadPool;
importlombok.extern.slf4j.Slf4j;
importjava.util.concurrent.ExecutorService;
importjava.util.concurrent.Executors;
@Slf4jpublicclassThreadPoolExample2 {
publicstaticvoidmain(String[] args) {
ExecutorServiceexecutorService=Executors.newFixedThreadPool(3);
for (inti=0; i<10; i++) {
finalintindex=i;
executorService.execute(newRunnable() {
@Overridepublicvoidrun() {
log.info("task:{}", index);
                }
            });
        }
executorService.shutdown();
    }
}
// 输出11:06:59.613 [pool-1-thread-3] INFOc.m.c.example.threadPool.test9-task:211:06:59.613 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:011:06:59.613 [pool-1-thread-2] INFOc.m.c.example.threadPool.test9-task:111:06:59.617 [pool-1-thread-3] INFOc.m.c.example.threadPool.test9-task:311:06:59.617 [pool-1-thread-3] INFOc.m.c.example.threadPool.test9-task:611:06:59.617 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:411:06:59.617 [pool-1-thread-3] INFOc.m.c.example.threadPool.test9-task:711:06:59.618 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:811:06:59.617 [pool-1-thread-2] INFOc.m.c.example.threadPool.test9-task:511:06:59.618 [pool-1-thread-3] INFOc.m.c.example.threadPool.test9-task:9Processfinishedwithexitcode0

newSingleThreadExecutor

packagecom.mmall.concurrency.example.threadPool;
importlombok.extern.slf4j.Slf4j;
importjava.util.concurrent.ExecutorService;
importjava.util.concurrent.Executors;
@Slf4jpublicclassThreadPoolExample3 {
publicstaticvoidmain(String[] args) {
ExecutorServiceexecutorService=Executors.newSingleThreadExecutor();
for (inti=0; i<10; i++) {
finalintindex=i;
executorService.execute(newRunnable() {
@Overridepublicvoidrun() {
log.info("task:{}", index);
                }
            });
        }
executorService.shutdown();
    }
}
// 输出11:08:19.913 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:011:08:19.918 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:111:08:19.919 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:211:08:19.919 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:311:08:19.919 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:411:08:19.919 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:511:08:19.919 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:611:08:19.919 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:711:08:19.919 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:811:08:19.919 [pool-1-thread-1] INFOc.m.c.example.threadPool.test9-task:9Processfinishedwithexitcode0

newScheduledThreadPool

packagecom.mmall.concurrency.example.threadPool;
importlombok.extern.slf4j.Slf4j;
importjava.util.Date;
importjava.util.Timer;
importjava.util.TimerTask;
importjava.util.concurrent.Executors;
importjava.util.concurrent.ScheduledExecutorService;
importjava.util.concurrent.TimeUnit;
@Slf4jpublicclassThreadPoolExample4 {
publicstaticvoidmain(String[] args) {
ScheduledExecutorServiceexecutorService=Executors.newScheduledThreadPool(1);
//        executorService.schedule(new Runnable() {//            @Override//            public void run() {//                log.warn("schedule run");//            }//        }, 3, TimeUnit.SECONDS);executorService.scheduleAtFixedRate(newRunnable() {
@Overridepublicvoidrun() {
log.warn("schedule run");
            }
        }, 1, 3, TimeUnit.SECONDS);
//        executorService.shutdown();Timertimer=newTimer();
timer.schedule(newTimerTask() {
@Overridepublicvoidrun() {
log.warn("timer run");
            }
        }, newDate(), 5*1000);
    }
}
// 输出11:09:04.287 [Timer-0] WARNc.m.c.example.threadPool.test9-timerrun11:09:05.285 [pool-1-thread-1] WARNc.m.c.example.threadPool.test9-schedulerun11:09:08.286 [pool-1-thread-1] WARNc.m.c.example.threadPool.test9-schedulerun11:09:09.286 [Timer-0] WARNc.m.c.example.threadPool.test9-timerrun11:09:11.284 [pool-1-thread-1] WARNc.m.c.example.threadPool.test9-schedulerun11:09:14.286 [pool-1-thread-1] WARNc.m.c.example.threadPool.test9-schedulerun11:09:14.286 [Timer-0] WARNc.m.c.example.threadPool.test9-timerrun11:09:17.287 [pool-1-thread-1] WARNc.m.c.example.threadPool.test9-schedulerun11:09:19.287 [Timer-0] WARNc.m.c.example.threadPool.test9-timerrun11:09:20.286 [pool-1-thread-1] WARNc.m.c.example.threadPool.test9-schedulerun
目录
相关文章
|
6天前
|
安全 Java 调度
Java编程时多线程操作单核服务器可以不加锁吗?
Java编程时多线程操作单核服务器可以不加锁吗?
21 2
|
6天前
|
Java 测试技术
Java接口的生产环境应用注意点
在Java生产环境中,合理使用接口对提升代码质量至关重要。设计接口时应遵循单一职责原则,采用清晰命名,并控制方法数量。默认方法应谨慎使用,避免与实现类产生冲突。通过版本化管理接口更新,确保向后兼容。实现接口时需明确行为,保持实现与接口分离,利用多态增强灵活性。关注性能影响,适当文档注释及充分测试确保接口稳定可靠。综合运用这些策略,可以显著提高系统的可扩展性和维护性。
|
9天前
|
Java API
Java中的Lambda表达式及其应用
本文将深入探讨Java中的Lambda表达式,通过简洁易懂的语言和示例代码,帮助读者理解Lambda表达式的定义、优势以及在实际开发中的应用。同时,我们将解析一些常见的使用场景,并展示如何利用Lambda表达式简化代码,提高编程效率。
18 2
|
5天前
|
Java
JAVA并发编程系列(13)Future、FutureTask异步小王子
本文详细解析了Future及其相关类FutureTask的工作原理与应用场景。首先介绍了Future的基本概念和接口方法,强调其异步计算特性。接着通过FutureTask实现了一个模拟外卖订单处理的示例,展示了如何并发查询外卖信息并汇总结果。最后深入分析了FutureTask的源码,包括其内部状态转换机制及关键方法的实现原理。通过本文,读者可以全面理解Future在并发编程中的作用及其实现细节。
|
6天前
|
Java 数据中心 微服务
Java高级知识:线程池隔离与信号量隔离的实战应用
在Java并发编程中,线程池隔离与信号量隔离是两种常用的资源隔离技术,它们在提高系统稳定性、防止系统过载方面发挥着重要作用。
6 0
|
8天前
|
Java 开发者 UED
Java中的异常处理机制:理解与应用
本文深入探讨Java的异常处理机制,通过实例解析如何有效使用try-catch-finally块、throws关键字及自定义异常,以提升代码的健壮性和可维护性。我们将从基础概念入手,逐步过渡到高级应用,为Java开发者提供全面指导。
|
8天前
|
Java 数据处理 调度
Java中的多线程编程:从基础到实践
本文深入探讨了Java中多线程编程的基本概念、实现方式及其在实际项目中的应用。首先,我们将了解什么是线程以及为何需要多线程编程。接着,文章将详细介绍如何在Java中创建和管理线程,包括继承Thread类、实现Runnable接口以及使用Executor框架等方法。此外,我们还将讨论线程同步和通信的问题,如互斥锁、信号量、条件变量等。最后,通过具体的示例展示了如何在实际项目中有效地利用多线程提高程序的性能和响应能力。
|
9天前
|
安全 算法 Java
Java中的多线程编程:从基础到高级应用
本文深入探讨了Java中的多线程编程,从最基础的概念入手,逐步引导读者了解并掌握多线程开发的核心技术。无论是初学者还是有一定经验的开发者,都能从中获益。通过实例和代码示例,本文详细讲解了线程的创建与管理、同步与锁机制、线程间通信以及高级并发工具等主题。此外,还讨论了多线程编程中常见的问题及其解决方案,帮助读者编写出高效、安全的多线程应用程序。
|
SQL 存储 Java
Java 应用与数据库的关系| 学习笔记
快速学习 Java 应用与数据库的关系。
198 0
Java 应用与数据库的关系| 学习笔记
|
SQL 存储 Java
Java 应用与数据库的关系| 学习笔记
快速学习 Java 应用与数据库的关系。
184 0
Java 应用与数据库的关系| 学习笔记
下一篇
无影云桌面