- Java并发是什么?
- Java并发开发环境配置
- Java并发主要操作
- Java并发线程间通信
- Java并发同步
- Java并发死锁
- 实用类实例
- 锁示例
- 原子变量示例
- 执行者示例
- 线程池示例
- 高级示例
- 并发集合
Java并发ThreadPoolExecutor类
java.util.concurrent.ThreadPoolExecutor
是一个ExecutorService
,可以使用可能的几个池线程来执行每个提交的任务,通常使用Executors
工厂方法进行配置。 它还提供了各种实用方法来检查当前线程统计信息并进行控制。
实例
以下TestThread
程序显示在线程环境中ThreadPoolExecutor
接口的使用。
import java.util.concurrent.Executors; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class TestThread { public static void main(final String[] arguments) throws InterruptedException { ThreadPoolExecutor executor = (ThreadPoolExecutor)Executors.newCachedThreadPool(); //Stats before tasks execution System.out.println("Core threads: " + executor.getCorePoolSize()); System.out.println("Largest executions: " + executor.getLargestPoolSize()); System.out.println("Maximum allowed threads: " + executor.getMaximumPoolSize()); System.out.println("Current threads in pool: " + executor.getPoolSize()); System.out.println("Currently executing threads: " + executor.getActiveCount()); System.out.println("Total number of threads(ever scheduled): " + executor.getTaskCount()); executor.submit(new Task()); executor.submit(new Task()); //Stats after tasks execution System.out.println("Core threads: " + executor.getCorePoolSize()); System.out.println("Largest executions: " + executor.getLargestPoolSize()); System.out.println("Maximum allowed threads: " + executor.getMaximumPoolSize()); System.out.println("Current threads in pool: " + executor.getPoolSize()); System.out.println("Currently executing threads: " + executor.getActiveCount()); System.out.println("Total number of threads(ever scheduled): " + executor.getTaskCount()); executor.shutdown(); } static class Task implements Runnable { public void run() { try { Long duration = (long) (Math.random() * 5); System.out.println("Running Task! Thread Name: " + Thread.currentThread().getName()); TimeUnit.SECONDS.sleep(duration); System.out.println("Task Completed! Thread Name: "+ Thread.currentThread().getName()); } catch (InterruptedException e) { e.printStackTrace(); } } } }
执行上面程序,得到以下结果 -
Core threads: 0 Largest executions: 0 Maximum allowed threads: 2147483647 Current threads in pool: 0 Currently executing threads: 0 Total number of threads(ever scheduled): 0 Core threads: 0 Largest executions: 2 Maximum allowed threads: 2147483647 Current threads in pool: 2 Currently executing threads: 2 Total number of threads(ever scheduled): 2 Running Task! Thread Name: pool-1-thread-1 Running Task! Thread Name: pool-1-thread-2 Task Completed! Thread Name: pool-1-thread-2 Task Completed! Thread Name: pool-1-thread-1
分类导航
- Java教程
- Vim教程
- Swing教程
- Spring教程
- Spring Web Services教程
- Spring MVC教程
- Spring JDBC教程
- Spring Cloud教程
- Spring Boot教程
- Spring Boot CLI教程
- Spring Batch教程
- Spring AOP教程
- PDFBox教程
- JSP教程
- JSF教程
- JPA教程
- Java面向对象设计
- Java设计模式
- Java虚拟机教程
- Java泛型教程
- Java正则表达式教程
- Java数据类型教程
- Java并发编程教程
- Java密码学教程
- Java多线程教程
- Java国际化(i18n)教程
- JavaFX教程
- Java9教程
关注微信小程序
扫描二维码
程序员编程王