Runnable or Executor Service
Asked Answered
D

1

5

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.

There may be a method that is better than either of these also, but I don't know of them.

Thanks :)

Duma answered 12/4, 2013 at 20:57 Comment(2)
It depends on what kind of threads you need to run, can you give us some specifics?Isadoraisadore
I'm using the Cling UPNP library and want to get information from it without blocking the UI as it often seems to only get results after a period of timeDuma
P
13
  • Runnable represents the code to be executed.
  • Executor and its subclasses represent execution strategies.

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.

Executors are sophisticated tools, which let you choose how many concurrent tasks may be running, and tune different aspects of the execution context. 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.

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.

So maybe depending on much sophistication you require, you will pick one or the other: Executors for more streamlined requirements (many tasks to execute and monitor), Threads for one shot or simpler situations.

Pyridoxine answered 12/4, 2013 at 22:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.