1: 介紹
BeeTP是stone包下一款小型任務池組件,它支持單次任務,定時任務,聚合任務的處理,整體功能類似JUC包中的線程池。
2: 參考例子
public class HelloTask implements BeeTask { public Object call() { return "Hello World"; } }
public class MyTaskTest { public static void main(String[] args) throws Exception { BeeTaskServiceConfig config = new BeeTaskServiceConfig(); config.setWorkerKeepAliveTime(TimeUnit.SECONDS.toMillis(10)); BeeTaskService service = new BeeTaskService(config); //作為單次任務 BeeTaskHandle handle = service.submit(new HelloTask()); //作為定時任務 service.scheduleAtFixedRate(new HelloTask(), 0, 2, TimeUnit.SECONDS); //打印單次任務結果 System.out.println("Result:" + handle.get()); } }
單次任務,定時任務,聚合任務的接口提交方法使用的均是基于BeeTask的實現。
3: 修改內容
1:任務池代碼重構,并進行精簡處理
2:修復任務句柄中的取消時的并發性bug
3:在重構的基礎上增加樹狀聚合型任務: BeeTreeTask,源碼如下
public interface BeeTreeTask<E> { /** * return pre-split sub tasks * * @return sub tasks of current task */ BeeTreeTask<E>[] getSubTasks(); /** * execute call with handle array of sub tasks * * @param subTaskHandles handle array of sub tasks * @return execution value of method call * @throws Exception occurred in execution */ E call(BeeTaskHandle<E>[] subTaskHandles) throws Exception; }
4: 版本下載
<dependency> <groupId>io.github.chris2018998</groupId> <artifactId>stone</artifactId> <version>1.2.4</version> </dependency>
5: 項目地址