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

Future接口的使用场景 #83

Open
alwaystest opened this issue Dec 5, 2018 · 0 comments
Open

Future接口的使用场景 #83

alwaystest opened this issue Dec 5, 2018 · 0 comments

Comments

@alwaystest
Copy link
Owner

Future接口的使用场景

标签(空格分隔): Java


ThreadPool实际上就是一个生产者消费者的实现,CoreThread作为消费者一直从队列中阻塞式取Task来执行。当线程池内线程数量超过CorePoolSize,从队列取Task会考虑超时,超时取不到Task,线程自然结束,当线程数量不足CorePoolSize,从队列取Task的时候会一直阻塞。

RxJava的ComputationThread实际上是利用ThreadPool的阻塞CoreThread的机制,产生和CPU核心数量一致的CoreThread数量为1的ThreadPool,往这些ThreadPool的队列中生产Task。

IOThread实际上也是控制若干ThreadPool,定期清理空闲一定时间的ThreadPool。

Future和Runnable相比,一个是提供了检查任务状态和取消任务的方法,另一个是返回值不是Void。相对来说,获取任务返回值,使用观察者模式更加高效,所以更大的优势在于可以检查任务状态和取消任务。用途并不是让一个线程Sleep一段时间后调用get方法,如果任务没有完成,仍然需要阻塞主线程,并不高效。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant