Executor Interfaces 执行器接口

本文详细介绍了Java.util.concurrent包中的三种执行器接口:Executor、ExecutorService 和 ScheduledExecutorService。解释了这些接口如何支持任务的执行、管理和调度,并探讨了它们之间的区别。
java.util.Concurrent包定义了三类执行器接口:

      1 Executor,一个简单的执行器支持启动新的任务
      2 ExecutorService,Executor的子接口,增加了管理个体任务和执行器自身的生命周期的特性。
      3 ScheduledExecutorService,ExecutorService的子接口,支持将来和/或者周期性任务的执行。


   典型地,与Executor对象关联的变量被申明为这三类接口中的一个,而不是用一个执行器类。

   The Executor Interface(执行器接口)

   Executor接口提供了一个单一方法,execute,被设计成为一个线程-创建形式的可插入式替代。如果r是一个Runnable对象,e是一个Executor对象,你可以用e.execute(r)替代(new Thread(r)).start();

   可是,execute的定义是笼统的。低级实行创建一个新的线程并立即启动它。虽然取决于Executor的实现,execute可能做那同样的事情,但是更有可能使用一个已经存在的工作线程来运行r,或者将r放置在一个队列中来等待一个工作线程变得可用。(工作线程将在线程池中讨论)

   Executor的实现被设计成充分利用更先进的ExecutorService和ScheduledExecutorService接口,虽然它们也以基本的Executor接口工作。

   The ExecutorService Interface

   ExecutorService 接口使用一个相似但更全能的submit方法增强execute方法。如同executr,submit接受Runnable对象,但也接受回调对象,允许任务返回一个值。submit方法返回一个future对象,用来取回回调函数的返回值。并管理回调对象和Runnable任务的两者状态。

   ExecutorService 也为提交海量回调对象提供了方法。最后,ExecutorService 也提供了大量方法来管理执行器的关闭。为了支持立即关闭,任务应该正确处理中断。

   The ScheduledExecutorService Interface

   ScheduledExecutorService 接口用schedue方法增强它父类ExecutorService的方法,将在一个指定的延迟后执行一个Runnable或者回调任务。另外,接口定义了scheduleAtFixedRate 和scheduleWithFixedDelay,将在定义的时间间隔多次执行指定的任务。
### MyBatis Centralized Executor Implementation and Usage In a typical application using MyBatis, SQL statements are executed through mappers that correspond to database operations. However, implementing a centralized executor can provide better control over how these operations occur, allowing for more efficient management of transactions or batching processes. A centralized executor pattern consolidates all execution logic into one component responsible for handling mapper calls. This approach ensures consistency across different parts of an application while also making it easier to implement features like logging, caching, or transactional boundaries around multiple operations[^1]. To achieve this with MyBatis, consider creating a service layer where each method corresponds to specific business actions involving several CRUD (Create Read Update Delete) operations performed via mapped interfaces. Within such methods, use `SqlSession` instances obtained from `SqlSessionFactory`, ensuring proper resource cleanup after completing tasks: ```java public class MyBatisExecutorService { private final SqlSessionFactory sqlSessionFactory; public void performComplexOperation() { try (SqlSession session = sqlSessionFactory.openSession()) { MapperInterface mapper = session.getMapper(MapperInterface.class); // Execute various mapper methods here session.commit(); } catch (Exception e) { // Handle exceptions appropriately } } } ``` For advanced scenarios requiring asynchronous processing capabilities similar to what is mentioned regarding Akka's utilization of Netty and Protocol Buffers for seamless remoting, integrating frameworks supporting reactive programming models could be beneficial when building highly scalable applications leveraging MyBatis as ORM tooling.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值