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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:08:10+00:00 2026-05-25T22:08:10+00:00

How can I stop a thread running after it has been running for 5

  • 0

How can I stop a thread running after it has been running for 5 seconds ?

I am unable to use java 5+ threading features (must work in j2me).


Possible solution –

Schedule two threads. One which performs the actual work(T1) and the other which acts as monitor of T1(T2).

Once T1 has started then start T2. T2 calls the isalive() method on T1 every second and if after 10 seconds T2 has not died then T2 invokes an abort on T1, which kills the T1 thread.

is this viable ?

Timer timer = new Timer();

TimerTask timerTask  = new TimerTask() {

public void run() {
        getPostData();
    }
};

timer.schedule(timerTask, 0);

public void abortNow() {
    try {
        _httpConnection.close();
    }
    catch(Exception e){
        e.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-25T22:08:11+00:00Added an answer on May 25, 2026 at 10:08 pm

    There is no unique answer to your question. It depends on what the thread does. First of all, how do you plan to stop that thread?

    1. If it elaborates some stuff, and this stuff is made of “tokens” such as single “tasks” to do, and the thread grabs them from a BlockingQueue for example, then you can stop the thread with a so-called “poison pill”, like a mock task that the thread reads as: “Hey man, I have to shut down”.
    2. If the thread does something which cannot be “divided” into “steps” then you should actually tell us how do you think it can be shut down. If it blocks on read()s on a socket, you can only close the socket to unblock it.

    Please note interrupt() is not meant to stop a Thread in normal circumstances, and if you interrupt() a thread, in most cases it’ll just go on doing its stuff. In 99.9% of cases, it is just bad design to stop a thread with interrupt().

    Anyway, to stop it after 5 seconds, just set a Timer which does so. Or better join it with a timeout of 5 seconds, and after that, stop it. Problem is “how” to stop it. So please tell me how do you think the thread should be stopped so that I can help you in better way.

    Tell me what the thread does.

    EDIT: in reply to your comment, just an example

    class MyHTTPTransaction extends Thread {
        public MyHTTPTransaction(.....) {
            /* ... */
        }
        public void run() {
            /* do the stuff with the connection */
        }
        public void abortNow() {
            /* roughly close the connection ignoring exceptions */
        }
    }
    

    and then

    MyHTTPTransaction thread = new MyHTTPTransaction(.....);
    thread.start();
    thread.join(5000);
    /* check if the thread has completed or is still hanging. If it's hanging: */
        thread.abortNow();
    /* else tell the user everything went fine */
    

    🙂

    You can also set a Timer, or use Condition on Locks because I bet there can be some race condition.

    Cheers.

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

Sidebar

Related Questions

I am trying to interrupt a thread that is running AES encryption on a
I am running a application with a LOT of thread. All these threads are
Recently I've been designing a Thread class library, I've made a Thread abstract class
I have written a Django app that makes use of Python threading to create
After running the program, Logcat shows some errors (picture). But after that program runs
I have a simple question, how can I stop timer? Button bCancel = (Button)findViewById(R.id.bt1);
I know this has been asked before , but I don't think these solutions
My program running on a one thread. And I have another thread and I
I have a Thread running in a Service... its run method looks something like
I have a web-app that has a master mysql db and four slave dbs.

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.