Java - Executors 有什么了不起的?

发布于 2024-09-14 03:53:53 字数 397 浏览 3 评论 0原文

在没有 Java Executors 的生活中,必须为每个 Runnable 任务创建新线程。创建新线程需要线程开销(创建和拆卸),这会增加非执行程序程序的复杂性并浪费时间。

参考代码:

没有 Java Executor -

new Thread (aRunnableObject).start ();

使用 Java Executor -

Executor executor = some Executor factory method;
exector.execute (aRunnable);

底线是 Executors 抽象了如何管理线程的低级细节。

这是真的吗?

谢谢。

In a life without Java Executors, new threads would have to be created for each Runnable tasks. Making new threads requires thread overhead (creation and teardown) that adds complexity and wasted time to a non-Executor program.

Referring to code:

no Java Executor -

new Thread (aRunnableObject).start ();

with Java Executor -

Executor executor = some Executor factory method;
exector.execute (aRunnable);

Bottom line is that Executors abstract the low-level details of how to manage threads.

Is that true?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恋你朝朝暮暮 2024-09-21 03:53:53

底线是执行器抽象了如何管理线程的低级细节。这是真的吗?

是的。

它们处理诸如创建线程对象、维护线程池、控制正在运行的线程数量以及优雅/不优雅关闭等问题。手工完成这些事情并不简单。

编辑

这样做可能会或可能不会影响性能...与完全适应应用程序的精确需求的自定义实现相比。但很可能是这样的:

  1. 您的自定义实现不会得到完美的调整,而且
  2. 性能差异也不会很大。

此外,如果存在需要解决的问题,Executor 支持类允许您简单地调整各种参数(例如线程池大小)。我不知道使用 Executor 会如何显着影响垃圾收集开销,无论哪种方式。

作为一般规则,您应该专注于简单而稳健地编写应用程序(例如,使用高级并发支持类),并且仅在以下情况下担心性能:

  1. 您的应用程序运行“太慢”,并且
  2. 分析工具告诉您您在某个特定领域遇到了问题。

Bottom line is that Executors abstract the low-level details of how to manage threads. Is that true?

Yes.

They deal with issues such as creating the thread objects, maintaining a pool of threads, controlling the number of threads are running, and graceful / less that graceful shutdown. Doing these things by hand is non-trivial.

EDIT

There may or may not be a performance hit in doing this ... compared with a custom implementation perfectly tuned to the precise needs of your application. But the chances are that:

  1. your custom implementation wouldn't be perfectly tuned, and
  2. the performance difference wouldn't be significant anyway.

Besides, the Executor support classes allow you to simply tune various parameters (e.g. thread pool sizes) if there is an issue that needs to be addressed. I don't see how garbage collection overheads would be significantly be impacted by using Executors, one way or the other.

As a general rule, you should focus on writing your applications simply and robustly (e.g. using the high level concurrency support classes), and only worry about performance if:

  1. your application is running "too slow", and
  2. the profiling tools tell you that you've got a problem in a particular area.
梦里南柯 2024-09-21 03:53:53

与普通线程相比,执行器有几个好处。

  1. 通过改变线程池的大小可以轻松实现节流。这有助于控制/检查流经应用程序的线程数量。在对您的承载应用程序进行基准测试时特别有用。
  2. 使用 RejectionHandler 可以更好地管理可运行任务。

Couple of benefits of executors as against normal threads.

  1. Throttling can be achieved easily by varying the size of ThreadPools. This helps keeping control/check on the number of threads flowing through your application. Particularly helpful when benchmarking your application for load bearing.
  2. Better management of Runnable tasks can be achieved using the RejectionHandlers.
我一直都在从未离去 2024-09-21 03:53:53

我认为执行者所做的只是执行低级任务
对你来说,但你仍然必须明智地决定哪个线程池做什么
你想要的。我的意思是,如果您的用例最多需要 5 个线程,那么您就可以
并使用具有 100 个线程的线程池,那么肯定会
对性能有影响。除此之外,还有额外的注意事项
在低级别完成,这将导致系统停止。最后的
总之,最好了解一下在低水平上正在做什么
水平,这样它就能让我们对地下事物有一个公平的了解。

I think all that executors do is that they will do the low level tasks
for you, but you still have to judiciously decide which thread pool do
you want. I mean if your use case needs maximum 5 threads and you go
and use thread pool having 100 threads, then certainly it is going to
have impact on performance. Other than this there is noting extra
being done at low level which is going to halt the system. And last of
all, it is always better to get an idea what is being done at low
level so that it will give us fair idea about the underground things.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文