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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:23:46+00:00 2026-05-28T19:23:46+00:00

I have created a little example(below) on my attempt to safely stop multiple threads

  • 0

I have created a little example(below) on my attempt to safely stop multiple threads when a user clicks on a Stop button on the user interface. However it seems like after this attempt, the threads still seems to be running. Can you please point me in the right direction?

Edit

Thanks for the comments everyone, just to go along with the post, I have modified the code below with the volatile boolean flag, and it seems to be working fine until I do some I/O operations.
If I add I/O operations, the threads seems to be running even if call fstream.close and then call the stop function to turn the boolean flag on… (Profiled the program to double-check that the threads were still running).
Is there any other thing I need to do in order to take care of the opened files, and eventually stop the threads?
Thanks again.

Fixed and working code.

class MultiThreadExample implements Runnable {
    private static final MultiThreadExample threadObj = new MultiThreadExample();
    ArrayList<Thread> threadList = new ArrayList<Thread>();

    public static MultiThreadExample getInstance() {
      return threadObj;
    }

    public void tester() {
      File file = new File("data/");
      File[] files = file.listFiles();
      for (int i = 0; i < files.length; i++) {
        Thread thread = new Thread(new ThreadedCrawler(), files[i].toString());
        thread.start();
        threadList.add(thread);
      }
    }

    public void run() {
      try {
        ProgramTester.getInstance().doSomething();          
      }
      finally { 
        do other stuff 
      }
    }


    public synchronized void stop() throws IOException {
      for (Thread threads : threadList) 
         threads.interrupt();
    }

    public void doSomething() {
      FileInputStream fstream = new FileInputStream(file);
      BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
      String inLine;
      while (((inLine = br.readLine()) != null) &&  !Thread.currentThread().isInterrupted()) {
        do something...
           for()...
               ....
               }
    }
}

public class GuiExample extends JFrame {
    ....
    public void mousePressed(MouseEvent e) {
      try {
        MultiThreadExample.getInstance().stop();
      }
      catch (InterruptedException e1) {
        e1.printStackTrace();
      }
    }
}
  • 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-28T19:23:47+00:00Added an answer on May 28, 2026 at 7:23 pm

    interrupt() doesn’t stop the thread (see the API). The preferred way of handling this is for your run() method to periodically check a flag and exit when it’s set (of course you set the flag when you want terminate the thread).

    stop() kills the thread, but it’s been deprecated for a long time (with good reason).

    same question here

    Update

    Since you check the flag outside of doSomething(), it won’t exit until the method completes, so long running tasks (like file io) will continue.

    Move the check to the loop over br.readLine(), as others have suggested, using interrupt() / isInterrupted() is probably better here, since it will also stop threads blocked on io.

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

Sidebar

Related Questions

Hi I have created a little application to move some files around and put
I have a page which contains a jQuery-UI horizontal slider, created using a little
I'm a jQuery newbie, and I have trouble with a little script I created.
I have only a little question: Why the CFPreferences-API creates multiple files in my
Update: I have provided complete code example in answer below. I have built my
I have a challenge - I have created this little concept here . The
I created the example below where the page loads the temp.js dynamically at the
In this little example below in PHP what would be a good way to
I have a swf file (little animation) created in swish and I want to
Have created a c++ implementation of the Hough transform for detecting lines in images.

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.