pdiddy pdiddy - 1 year ago 126
Android Question

Android thread queue

I'd like to have a queue of work/tasks to be done on a separate thread, but can only process one work at a time. So not simultaneously.

Is there something built-in android for this?


The work = get information from Database. Once done, update the UI with the fetched information.

gwa gwa
Answer Source

Have you checked out java.util.concurrent.Executors ? You could do something like this:

final static ExecutorService tpe = Executors.newSingleThreadExecutor();
tpe.submit(new Runnable() {
    public void run() {
        // your work

It's not android specific, it is part of the jdk5.

From the doc:

Creates an Executor that uses a single worker thread operating off an unbounded queue. (Note however that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.) Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time. Unlike the otherwise equivalent newFixedThreadPool(1) the returned executor is guaranteed not to be reconfigurable to use additional threads.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download