János
János

Reputation: 35060

How to create serial queue in Android?

In iOS I can create GCD serial queue like this:

let serialQueue = DispatchQueue(label: "serialQueue")

Blocks are executed in a FIFO order, one block at a time. How can I do it in Android?

This is how chunk of code can be performed on the queue:

serialQueue.async {

As I see AsyncTask is serial: "Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution."

I guess t is FIFO, the execution.

Found this JAVA 8 concurrency tutorial.

Upvotes: 1

Views: 2442

Answers (2)

TheWaterProgrammer
TheWaterProgrammer

Reputation: 8239

There are number of options based on what your exact requirement is. If I'm getting your question correctly, an executor is what you need. And extend it to create a SerialExecutor like below,

https://developer.android.com/reference/java/util/concurrent/Executor.html

What are these blocks ? functions ? You need to make runnable objects for the executor to execute each of your blocks

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006869

Use Executor.newSingleThreadExecutor(). You can pass a Runnable or Callable<V> that represents the work to be done.

Upvotes: 2

Related Questions