Running code when all threads are finished processing.

Posted by rich97 on Stack Overflow See other posts from Stack Overflow or by rich97
Published on 2011-01-04T06:56:23Z Indexed on 2011/01/04 7:53 UTC
Read the original article Hit count: 234

Filed under:
|
|
|

Quick note: Java and Android noob here, I'm open to you telling me I'm stupid (as long as you tell me why.)

I have an android application which requires me start multiple threads originating from various classes and only advance to the next activity once all threads have done their job. I also want to add a "failsafe" timeout in case one the the threads takes too long (HTTP request taking too long or something.)

I searched Stack Overflow and found a post saying that I should create a class to keep a running total of open threads and then use a timer to poll for when all the threads are completed.

I think I've created a working class to do this for me, it's untested as of yet but has no errors showing in eclipse.

Is this a correct implementation? Are there any APIs that I should be made aware of (such as classes in the Java or Android APIs that could be used in place of the abstract classes at the bottom of the class?)

package com.dmp.geofix.libs;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;

public class ThreadMonitor {

    private Timer timer = null;
    private TimerTask timerTask = null;
    private OnSuccess onSuccess = null;
    private OnError onError = null;

    private static ArrayList<Thread> threads;

    private final int POLL_OPEN_THREADS = 100;
    private final int TIMEOUT = 10000;

    public ThreadMonitor() {
        timerTask = new PollThreadsTask();
    }

    public ThreadMonitor(OnSuccess s) {
        timerTask = new PollThreadsTask();
        onSuccess = s;
    }

    public ThreadMonitor(OnError e) {
        timerTask = new PollThreadsTask();
        onError = e;
    }

    public ThreadMonitor(OnSuccess s, OnError e) {
        timerTask = new PollThreadsTask();
        onSuccess = s;
        onError = e;
    }

    public void start() {
        Iterator<Thread> i = threads.iterator();
        while (i.hasNext()) {
            i.next().start();
        }

        timer = new Timer();
        timer.schedule(timerTask, 0, POLL_OPEN_THREADS);
    }

    public void finish() {
        Iterator<Thread> i = threads.iterator();
        while (i.hasNext()) {
            i.next().interrupt();
        }

        threads.clear();
        timer.cancel();
    }

    public void addThread(Thread t) {
        threads.add(t);
    }

    public void removeThread(Thread t) {
        threads.remove(t);
        t.interrupt();
    }

    class PollThreadsTask extends TimerTask {

        private int timeElapsed = 0;

        @Override
        public void run() {
            timeElapsed += POLL_OPEN_THREADS;
            if (timeElapsed <= TIMEOUT) {
                if (threads.isEmpty() == false) {
                    if (onSuccess != null) {
                        onSuccess.run();
                    }
                }
            } else {
                if (onError != null) {
                    onError.run();
                }
                finish();
            }
        }

    }

    public abstract class OnSuccess {
        public abstract void run();
    }

    public abstract class OnError {
        public abstract void run();
    }

}

© Stack Overflow or respective owner

Related posts about java

Related posts about android