简体   繁体   English

Runnable或Executor服务

[英]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.

相关问题 Executor 服务:SingleThreadExecutor 不触发可运行对象 - Executor Service: SingleThreadExecutor not firing runnable object 多次提交相同(Runnable)命令到Java中的executor服务 - Submitting the same (Runnable) command multiple times to executor service in Java 如何在Java 7中使用Executor服务并具有多个可运行项 - How to Use Executor Service and have multiple runnable in java 7 如果将一个可运行对象提交给具有多个线程的执行程序服务,那么多个线程会执行该可运行对象吗? - If you submit one runnable to an executor service with multiple threads, will multiple threads execute that runnable? 中断执行程序线程池中的可运行对象 - Interrupting a runnable in executor threadpool 执行服务中的执行服务? - Executor service in executor service? 当runnable抛出异常时,Future <T> .get()卡住了 - 在单独的执行器服务上运行时? - Future<T>.get() stuck when the runnable throws exception--while running on separate executor service? 是否存在通过调用执行程序服务的可运行实例的提交而获得的Future对象上使用get的用例 - Is there a usecase for using get on Future object obtained from calling submit of runnable instance of executor service Java EE EJB作为可运行执行器 - Java EE EJB as Runnable executor 我应该使用执行器仅运行1个Runnable吗? 什么时候使用执行器? - Should I use an Executor to run only 1 Runnable? When to use an executor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM