在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:linkedin/parseq开源软件地址:https://github.com/linkedin/parseq开源编程语言:Java 92.4%开源软件介绍:ParSeqParSeq is a framework that makes it easier to write asynchronous code in Java. Some of the key benefits of ParSeq include:
Our Wiki includes an introductory example, a User's Guide, javadoc, and more. See CHANGELOG for list of changes. Introductory ExampleIn this example we show how to fetch several pages in parallel and how to combine them once they've all been retrieved. You can find source code here: IntroductoryExample. First we can retrieve a single page using an asynchronous HTTP client as follows: final Task<Response> google = HttpClient.get("http://www.google.com").task();
engine.run(google);
google.await();
System.out.println("Google Page: " + google.get().getResponseBody()); This will print:
In this code snippet we don't really get any benefit from ParSeq. Essentially we create a task that can be run asynchronously, but then we block for completion using final Task<String> google = HttpClient.get("http://www.google.com").task()
.map(Response::getResponseBody)
.andThen(body -> System.out.println("Google Page: " + body));
engine.run(google); We used private Task<String> fetchBody(String url) {
return HttpClient.get(url).task().map(Response::getResponseBody);
} Next, we will compose tasks to run in parallel using final Task<String> google = fetchBody("http://www.google.com");
final Task<String> yahoo = fetchBody("http://www.yahoo.com");
final Task<String> bing = fetchBody("http://www.bing.com");
final Task<String> plan = Task.par(google, yahoo, bing)
.map((g, y, b) -> "Google Page: " + g +" \n" +
"Yahoo Page: " + y + "\n" +
"Bing Page: " + b + "\n")
.andThen(System.out::println);
engine.run(plan); This example is fully asynchronous. The home pages for Google, Yahoo, and Bing are all fetched in parallel while the original thread has returned to the calling code. We used We can do various transforms on the data we retrieved. Here's a very simple transform that sums the length of the 3 pages that were fetched: final Task<Integer> sumLengths =
Task.par(google.map(String::length),
yahoo.map(String::length),
bing.map(String::length))
.map("sum", (g, y, b) -> g + y + b); The Notice that we added descriptions to tasks. e.g. Graphviz diagram best describes relationships between tasks: For more in-depth description of ParSeq please visit User's Guide. For many more examples, please see the parseq-examples contrib project in the source code. BuildBuild and test whole parseq code Build ParSeq subproject(modules) instead of the whole project:
Building on MacOS Catalina (>=10.15): Follow this guide to install the required Xcode Command Line Tools and add below environment variables export LDFLAGS="-mmacosx-version-min=10.13"
export CXXFLAGS="-mmacosx-version-min=10.13" |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论