Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FutureTask/CompletableFuture的异步任务场景下,TTL的使用 #154

Closed
liaofanxin opened this issue Nov 21, 2019 · 8 comments
Closed
Assignees
Labels
❓question Further information is requested

Comments

@liaofanxin
Copy link

liaofanxin commented Nov 21, 2019

请问在JDK1.8,CompletableFuture的异步任务的时候,如果做兼容?有demo可以参考一下吗

@oldratlee oldratlee changed the title FutureTask 对于FutureTask/CompletableFuture的异步任务时,TTL的使用方式? Nov 29, 2019
@oldratlee oldratlee changed the title 对于FutureTask/CompletableFuture的异步任务时,TTL的使用方式? FutureTask/CompletableFuture的异步任务场景下,TTL的使用 Nov 29, 2019
@oldratlee
Copy link
Member

oldratlee commented Nov 29, 2019

1. FutureTask

你说到的FutureTask是指java.util.concurrent.FutureTask,是吧。 @liaofanxin

java.util.concurrent.FutureTaskRunnable,一般是在ThreadPoolExecutor中执行,可以通过TTL文档的说明的方法来使用:

  • 2.1 修饰Runnable和Callable
  • 2.2 修饰线程池
  • 2.3 使用Java Agent来修饰JDK线程池实现类

这3个背后都是修饰Runnable,只是使用方式不同。

2. CompletableFuture

CompletableFuture的执行,缺省使用ForkJoinPool作为执行器,底下转成ForkJoinPoolForkJoinTask来执行,使用文档中2.3 使用Java Agent来修饰JDK线程池实现类的方式,透明支持CompletableFuture的TTL:

image

参见Issue

3. Stream

与上面的CompletableFuture一样,(并行执行的)Stream底层通过ForkJoinPool执行。

所以也是使用 文档中2.3 使用Java Agent来修饰JDK线程池实现类的方式,透明支持Stream的TTL。

参见Issue

@oldratlee oldratlee self-assigned this Nov 29, 2019
@oldratlee oldratlee added the ❓question Further information is requested label Nov 29, 2019
@liaofanxin
Copy link
Author

liaofanxin commented Nov 29, 2019 via email

@oldratlee
Copy link
Member

oldratlee commented Nov 29, 2019

已经找到解决办法,项目中已经引入,测试没问题,已发布上线

👏👍

能说一下你的场景和解决方法吗?😀 ❤️@liaofanxin

@liaofanxin
Copy link
Author

liaofanxin commented Nov 29, 2019 via email

@oldratlee
Copy link
Member

oldratlee commented Nov 30, 2019

引入TTL后的方案

……
3. 使用CompletableFuture并制定Executor

关于『使用CompletableFuture并制定Executor』这点,对CompletableFuture及其使用ForkJoinPool Executor你是如何定制的? @liaofanxin 😁

  • 使用了『2.3 使用Java Agent来修饰JDK线程池实现类』的Java Agent方式吗?
  • 如果没有使用Java Agent,你是如何做的?

@liaofanxin
Copy link
Author

liaofanxin commented Nov 30, 2019 via email

@LouisProgress
Copy link

CompletableFuture

对的CompletableFuture的支持,能否分享一个demo,感谢!

@liaofanxin
Copy link
Author

liaofanxin commented Jul 20, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
❓question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants