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

  • Home
  • SEARCH
  • 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 8852227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:20:48+00:00 2026-06-14T13:20:48+00:00

So i have my MainThread class that is responsible for all other threads.(creating, stopping,

  • 0

So i have my MainThread class that is responsible for all other threads.(creating, stopping, monitoring)

 public class MainThread implements Runnable {
      public static volatile boolean keepRunning = true;  

      public void run(){


      //some code..


      while(keepRunning){

      //here i am creating threads for my Wired class (explaining below) 
      //that are stored in a hashmap

      Wired wiredInterface = new Wired(s,defaultSleepTime,c,max,percent);
      Thread t1 = new Thread(wiredInterface);
      t1.start();
      }

far down in my code there is a case where i need to stop the thread t1.

My Wired Class:

   public class Wired implements Runnable {
        private static volatile boolean keepRunning = true;

        public void run(){

        while(keepRunning){

        //code

        try{
            Thread.sleep(((k - i + 1)*defaultTime) - DT);  //just to show that this thread sleeps periodically. doesnt affect the question
        }catch(InterruptedException e){e.printStackTrace();}  

        }
        }

In my Wired class i have this method for changing the volatile flag.

        public static void stopRunning(){
            keepRunning = false;
        }

My Question is..how can i access the method stopRunning from my MainThread for the specific thread that i want to stop? Thread.interrupt() doesnt work for me as a solution.

I have looked at a lot of similar questions referring to this subject but i havent found something suitable for my case. Sorry if i missed something
This code is an oversimplification of my actual code

  • 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-14T13:20:49+00:00Added an answer on June 14, 2026 at 1:20 pm

    Don’t reinvent the wheel here. Use Thread.interrupt(), and properly check the Thread.isInterrupted() flag, and/or properly handle InterruptedException. IE don’t swallow it or printStackTrace() it and continue. If you receive an InterruptedException catch it at the borders of the Runanble.run() method and stop your outer loop and shutdown the thread.

    Your method should be changed to this:

    public void run() {
       try {
          while( !Thread.currentThread().isInterrupted() ) {
              doSomeThreadedThing();
          }
       } catch( InterruptedException e ) {
          // maybe log some info you are shutting down
       }
    }
    

    It’s really simple to properly shutdown a thread provided it isn’t stuck in some IO. If you have a long running task that you don’t want to wait for check Thread.isInterrupted() within your logic periodically. There are no advantages provided by your volatile boolean flag mechanism you have shown over using Thread.interrupted().

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

Sidebar

Related Questions

I have a class class with main thread public class MainThread extends Thread {
I'm using JMagick and have a simple Java class that loops over all images
I have created a ThreadManager class that handles Threads and its task is to
I have an application that has two threads. The first one (the main thread)
I have a program that runs in a few threads. The main thread shares
So I have a service clint class that has a method called fetch. I
I have an application that will be spawning multiple threads. However, I feel there
I have a QThread derived class that communicates with the main thread by sending
I have a C++ helper class that I use with objective-C. I would like
I have a java class that creates a process, called child, using ProcessBuilder. The

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.