2

The Thread should end if I press a button, which sets the isButtonPressed to true. My problem is, that if a want to start the thread with thread.start(runnable) by clicking the button, I get this: IllegalThreadStateException: Thread already started (I thought the thread was terminated after the break because the the loop is over, but it seems that I am wrong).

Thread thread = new Thread(runnable);
thread.start(runnable);

The runnable Runnable:

    Runnable runnable = new Runnable() {
    @Override
    public void run() {
        time = 10;
        for (int i = 10; i <= 10; i--) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    txt_Time.setText(String.valueOf(time));
                }
            });

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }

            if (isButtonPressed) {
                break;
            }

            if (time == 0) {
                resetVisibleState();
                break;
            } else {
                time--;
            }
        }
    }
};

Thanks for your help!

Ole
  • 49
  • 1
  • 9
  • Adding to Andrew answer, use executor service instead of restarting a new thread. Have a look at this question : http://stackoverflow.com/questions/36398144/multiple-newsinglethreadexecutor-vs-newfixedthreadpool-of-executorservice/36404133#36404133 – Ravindra babu Apr 17 '16 at 16:16

3 Answers3

3

Java threads are not restartable. For what you are trying to achieve, you could create a new thread each time, or you could look at an ExecutorService. Just create a single threaded executor (Executors.newSingleThreadExecutor), and submit your runnable to it every time you need it to run.

ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(runnable);
Andrew Williamson
  • 8,299
  • 3
  • 34
  • 62
0

Take a boolean variable and wrap the contents you need to run continusly in the thread with a while loop that runs forever till Run is set to false then on clicking the button set the variable to false, for example :-

volatile boolean run = true;
Thread t = new Thread()
{
   while(run)
   {
     // whatever is here runs till Run is false
   }
}
t.start();

/*now when the button is pressed just trigger Run as false and the thread will be ended
later call t.start() when you need to start the thread again.*/
  • 2
    While this probably is a valid answer to the question, it would be advisable to declare `run` as `volatile boolean`. When the thread starts running, it will cache the flag from the surrounding scope. As far as I know, it is completely OS dependent when the value will actually be refreshed and it might be a long time. – Dave Feb 17 '16 at 16:27
0

From my understanding you need to start a new thread. You cannot re-start a thread that has ran its course.

Since you are correctly stopping the old one via your isButtonPressed. You should just be able to start a new instance of the thread in its place

IAmGroot
  • 13,760
  • 18
  • 84
  • 154