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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:53:31+00:00 2026-06-12T15:53:31+00:00

I have walked through many existing answers about stopping a thread.I copy a typical

  • 0

I have walked through many existing answers about stopping a thread.I copy a typical piece of code below.

public class HelloWorld {

    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(new Runnable() {

            public void run() {
                try {
                    while (!Thread.interrupted()) {
                        Thread.sleep(5000);
                        System.out.println("Hello World!");
                    }
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        });
        thread.start();
        System.out.println("press any key to quit");
        System.in.read();
        thread.interrupt();
    }
}

But this solution need to check the interrupt status in the while loop code.If what I am doing in the run method is a sequential and single path thing So I have no chance to check the interrupt status twice.For example,I want to stop a thread that is searching all disk stuff and it takes a lot of time.

How can I stop it?

  • 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-12T15:53:32+00:00Added an answer on June 12, 2026 at 3:53 pm

    In your long running process, you have to have some points where you can say if (!keepRunning) { break; } else { do next directory/file(); } to get out of the loop.

    In your straw man example, if you are searching the disk, you have points in there before you process every file and directory to include this check logic.

    If you have a really long running process, like encrypting a giant file, or transfering a extremely large file, you still have the ability to embed these flag checks into the process. In this example, you would be transferring blocks of bytes, before you build each block to send you would check the flag condition. Everything has to be done in steps at some point, and you can put your flag check in before each of those steps. I can’t think of anything that doesn’t have stages or steps that is long running other than a contrived example with .sleep() or some connection logic that waits forever for something to happen.

    Without an exact problem you are encountering it will be very hard to answer you question in a more specific way. Show some code that you are actually having a problem with.

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

Sidebar

Related Questions

Am integrating APNS in my iPhone app. I have walked through on Apple Push
I have some VB code which makes use of a COM api and results
I have a ruby on rails application and thinking about porting it to java.
I have a string, which could look like this: Sandra <as type=1>walked</as> in the
This is my first tabbed application for Android. I walked through the HelloTabWidget app
I have run into this issue too many times and need this to be
I have a need to wrap an existing C++ library for use in Python.
There's a great tutorial on IBM's website which walked me through a simple search/results
Imagine, that u have a Method: public void SometimesIFail(string text) { bool everythingOk =
I have a tree walker like this: function: ^(FUNCTION_TOK fcname=IDENTIFIER param=functionParameters*){ a_param_arrayList.add(param); } ;

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.