4

I want to have a class that starts a Thread and provides methods to pause and continue this Thread. My first approach was to have flag, which loops a sleep method as long as the value is true. Something like :

public class Bot {
private Thread t ;
private boolean isPaused;

public Bot(){
    t = new Thread(new Runnable(){
        @Override
        public void run() {
            while (true) {
                System.out.println("Hi");


                while(isPaused){
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    });

    t.start();
}
public void pauseBot(){
    isPaused = true;
}

public void continueBot(){
    isPaused = false;
}
}

But since the Thread is still running and wasting CPU, I dont find this to be a good solution. How would this look with wait() and notify(). I had a look at various tutorials about that topic but somehow I couldnt apply them to my issue.

Everytime I tried it I either got IllegalMonitorStateException or the code stopped my whole application and not just the Thread I wanted to be stopped.

Another question I have is: How do prevent the Thread from beeing paused at a critical moment e.g.

Runnable r = new Runnable(){

    @Override
    public void run() {
        while(true){
            task1();
            task2();

            //Thread mustn't be stopped from here....
            task3();
            task4();
            task5();
            task6();
            task7();
            //... to here

            task8();
            task9();
            task10();
        }

    }

};

Because when task3() .... task7() deal with something that would expire while the Thread is paused there must be a way to let the Thread finish task7() until it pauses.

I hope you can help me with my issue. Thanks in advance, Flo

flxh
  • 565
  • 4
  • 19
  • possible duplicate of [How to stop and resume thread safely in Java?](http://stackoverflow.com/questions/11856287/how-to-stop-and-resume-thread-safely-in-java) – Anshul Nov 05 '14 at 18:47

1 Answers1

4

So given this is your Thread class:

public class MyThread extends Thread
{

First, you need an lock object. This object can be everything, and if you use an existing object this takes less memory. Also define a flag if the bot should be paused.

    public Object lock = this;
    public boolean pause = false;

Now, define a pause() and continue() method for the thread. This sets the pause flag.

    public void pause ()
    {
        pause = true;
    }

    public void continue ()
    {
        pause = false;

Here you need to wake up the thread. Note the synchronized on the lock object so that you don't get an IllegalMonitorStateException.

        synchronized (lock)
        {
            lock.notifyAll();
        }
    }

No, define a method that automatically pauses the thread when it should be paused. You might call this at every moment when the thread can be paused.

    private void pauseThread ()
    {
        synchronized (lock)
        {
            if (pause)
                lock.wait(); // Note that this can cause an InterruptedException
        }
    }

Now, you can define your thread in the run() method:

    public void run ()
    {
        task1();
        task2();

        pauseThread();

        task3();
        task4();
        task5();
        task6();
        task7();

        pauseThread();

        task8();
        task9();
        task10();
    }
}
msrd0
  • 7,816
  • 9
  • 47
  • 82
  • 1
    What does "this can be everything" mean? – Solomon Slow Nov 05 '14 at 20:25
  • @jameslarge This can be any object, as long as only the thread calls the `wait()` and `notify()` methods – msrd0 Nov 05 '14 at 20:38
  • For me this is strange, too. I just dont understand why you have to create a new object that is used for nothing but for the sync. Couldn't you just use "this" instead. Sorry but I think I need some beackground information on what is the sync Object about. THanks for your answer btw – flxh Nov 05 '14 at 21:19
  • @user3187049 as I said you can use everythung, so this is okay, you just need to take care that you use it only in the thread as a lock object – msrd0 Nov 06 '14 at 05:57