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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:08:17+00:00 2026-05-17T01:08:17+00:00

I have a thread in java/Android like this: Handler handler = new Handler() {

  • 0

I have a thread in java/Android like this:

Handler handler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        update_i();
    }
};



@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();

    Thread myThread = new Thread(new Runnable() {
        public void run() {
            while (true) {
                try {
                    handler.sendMessage(handler.obtainMessage());
                    Thread.sleep(timer);
                } catch (Throwable t) {
                }
            }
        }
    });

    myThread.start();
}

The thread works fine when I run my application. But I want to start/restart the thread with a button.

Button.OnClickListener StartButtonOnClickListener = new Button.OnClickListener() {
    @Override
    public void onClick(View v) {

        //start/restart the thread
    }
};

If I copy the thread into the button I just make a new thread every time the user clicks on the button. I want to run the thread when the user first time click on the button, “kill it” and start from the beginning if the user click on the button a second time (I don’t want to start a second thread).

  • 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-17T01:08:18+00:00Added an answer on May 17, 2026 at 1:08 am

    You can’t restart a Thread.

    From the documentation :

    Throws IllegalThreadStateException  
        if the Thread has been started before
    

    You can kill the previous thread, but in the end you will have to create a second instance of your thread.


    Resources :

    • Javadoc Android – Thread.start()
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a java code that looks like this: //UI thread //Some code Job
I'm new in Android and even Java. I have to save and read message
I have this very straight forward question regarding Thread and Timer classes in Java
Java 6 API question. Does calling LockSupport.unpark(thread) have a happens-before relationship to the return
I have in my Java application a thread that run a while(True) loop with
In Java I have the necessity of implementing a class that extends Thread within
I have a fairly straightforward Java class here which creates 2 thread pools.... Connects
I have written a simple Java dispatcher with a daemon thread to handle the
I think this is a general Java question, but it does involve some Android
ok so im very new to android development and fairly new to java and

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.