项目作者: nch14

项目描述 :
Java并发语法糖
高级语言: Java
项目地址: git://github.com/nch14/Parallel.git
创建时间: 2019-05-25T17:31:30Z
项目社区:https://github.com/nch14/Parallel

开源协议:

下载


This is an framework for concurrent tasks.

It use CompletableFuture and provides fluent api.

Use Parallel and start from now.

How to use ?

First, you need implement ConcurrentFactory and ConcurrentConfigFactory for your own. I provide you an example.

Then, you need use Spring manage your beans and scan ‘cn.chenhaonee.paralle’.

When you done those, you can use Paralle now.

  1. @Autowired
  2. private ConcurrentService concurrentService;
  3. SyncResult<Integer, String> result = Parallel.of(bizIds)
  4. .timeout(1000, TimeUnit.SECONDS) //default for 5000 millseconds. This step is Optional
  5. .allowPartSuccess(true) //default for false. This step is Optional
  6. .executor(concurrentService) //must set our ConcurrentService here
  7. .submit(bizId -> {
  8. MoreResource moreResource = preparePartDownload(bizId, origin);
  9. log.info("Start download part {}/{}", bizId + 1, packs);
  10. down(moreResource, bizId);
  11. return moreResource.getTarget();
  12. });
  13. if you do not need result back, you can use excute other than submit.
  14. excute method will back you only excution status while submit method will give your function result.

这是一个并发任务调用框架

使用线程池与CompletableFuture实现。提供了Fluent Api便于使用。

使用Parallel类,开始体验。

如何使用

首先,你需要实现自己的ConcurrentFactory类与ConcurrentConfigFactory类,我提供了一个示例。

接着,你需要使用Spring管理你的这些bean,并且把’cn.chenhaonee.paralle’添加到包扫描的路径下。

完成上面两个步骤以后,就可以使用Parallel的流式API了。示例见上面的代码。

execute与submit方法的区别在于,execute的入参为Consumer,submit的入参为Function