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 7932101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:54:53+00:00 2026-06-03T20:54:53+00:00

I have a thread, A , which performs periodic actions inside a while(running) loop.

  • 0

I have a thread, A, which performs periodic actions inside a while(running) loop. I then have another thread,B, which listens for user input. When the user presses a button, thread B spawns a new thread to perform a task (let’s call it an instance of Thread C). This task involves sending information over the internet and can take up to 20 seconds. There can be more than one instance of C active at once.

The problem I am having is this. I would like thread A to only run it’s code if there are no threads of type ‘C’ currently active. To be more clear, thread A looks something like this:

 class ThreadA extends Thread{

      public void run(){
            while(running){
                performAction();
                Thread.sleep(1000);
            }
      }
 }

If an instance of C is detected, then obviously I would not like performAction() to be called. If the thread is currently executing performAction(), then I would like to stop its execution at its current progress.

I’m not sure how to proceed, as I’m not sure how I would obtain a reference of the spawned threads from inside thread A.

  • 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-06-03T20:54:55+00:00Added an answer on June 3, 2026 at 8:54 pm

    Use java.lang.ThreadGroup. By constructing all instances of ThreadC with a static ThreadGroup, you can maintain all of the instances of ThreadC and get a count of the active ones using ThreadGroup#activeCount(). Simply add a condition in ThreadA to only invoke performAction() if your public static ThreadC.threadGroup field returns 0 when activeCount() is called upon it.

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

Sidebar

Related Questions

I am building an application in swt, I also have a thread running which
I have a thread which performs some tasks. At the end I want to
I currently have a long running thread which uses a hibernate session to perform
I have a Windows service which performs a fairly long running task. At the
I have an external library which has a method which performs a long running
I have an external library which has a method which performs a long running
I have a thread which handles all the HTTP Connections to the server and
i have this thread which run as a service: public void run() { try
I have an application where i have created a thread which must check the
I have the main thread from which I start a window using invokeLater .

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.