Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3669196
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:12:51+00:00 2026-05-19T02:12:51+00:00

Quick note : Java and Android noob here, I’m open to you telling me

  • 0

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();
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T02:12:52+00:00Added an answer on May 19, 2026 at 2:12 am

    Take a look at Thread.join

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quick note: I'm using the SoundPool class http://developer.android.com/reference/android/media/SoundPool.html What I have here is a
Quick note: Examples from the tutorial Scala for Java Refugees Part 5: Traits and
I'm doing some quick Java-.NET interop and have decided on POX with WCF. However,
Okay, hopefully this is quick and easy. I have two separate java projects, 'Library'
I am a complete Android and C in general noob, I have done a
Quick question. What do you think, I have a few sites that use a
In an Android application I run a task in a separate thread. While the
Does Java have anything like any of the following macros a way to iterate
Ok, so I've got an Open Source Java client/server program that uses packets to
I love the windows 7 sticky note app and need to write a quick

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.