[英]Runnable or Executor Service
Which is easier and more suitable to use for running things in another thread, notably so that the program waits for the result but doesn't lock up an ui. 哪个更容易,更适合用于在另一个线程中运行,特别是使程序等待结果但不锁定ui。
There may be a method that is better than either of these also, but I don't know of them. 可能有一种方法比这两种方法都要好,但我不知道它们。
Thanks :) 谢谢 :)
Runnable
represents the code to be executed. Runnable
表示要执行的代码。 Executor
and its subclasses represent execution strategies. Executor
及其子类代表执行策略。 This means that the former is actually consumed by the later. 这意味着前者实际上是由后者消耗的。 What you probably meant is: between simple threads and executors, which are more suitable?
您可能的意思是:简单线程和执行器之间哪个更合适?
The answer to this question is basically: it depends. 这个问题的答案基本上是:它取决于。
Executor
s are sophisticated tools, which let you choose how many concurrent tasks may be running, and tune different aspects of the execution context. Executor
是复杂的工具,可让您选择可运行的并发任务数,并调整执行上下文的不同方面。 They also provide facilities to monitor the tasks' executions, by returning a token (called a Future
or sometimes a promise) which let the code requesting the task execution to query for that task completion. 它们还提供监视任务执行的工具,方法是返回一个令牌(称为
Future
或有时是一个promise),让令代码请求执行任务以查询该任务的完成情况。
Threads are less elaborate (or more barebone) a solution to executing code asynchronously. 线程不那么精细(或更准确)是异步执行代码的解决方案。 You can still have them return a
Future
by hand, or simply check if the thread is still running. 您仍然可以让他们手动返回
Future
,或者只是检查线程是否仍在运行。
So maybe depending on much sophistication you require, you will pick one or the other: Executor
s for more streamlined requirements (many tasks to execute and monitor), Threads for one shot or simpler situations. 因此,根据您需要的复杂程度,您可以选择其中一个:
Executor
用于更简化的要求(执行和监视的许多任务),一次性线程或更简单的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.