0

I'm hoping someone can help me with this. I've been searching for about a week for an answer to this issue, with no avail.

I currently have a custom thread class that implements Runnable, which I'd like to pause upon a key press. Based on my research, I've learned that the best way to go about this is by using wait() and notify(), triggered by a key that's using a key binding.

My question is, how can I get this to work? I can't seem to set up a key binding without something going wrong, and how I might implement wait() and notify() without running into a deadlock is beyond me.

xav
  • 5,452
  • 7
  • 48
  • 57
Jacob R
  • 11
  • 1
  • So have you already went through for example [this thread](http://stackoverflow.com/questions/11989589/how-to-pause-and-resume-a-thread-in-java-from-another-thread)? Seems like the same thing. – eis Apr 06 '14 at 20:20
  • It's impossible to guess what you might be doing wrong without seeing your attempt. Otherwise your question is little more than a request for someone to re-write tutorials that already exist. Please improve your question by showing your code attempt and by asking specific questions about just where you might be stuck. – Hovercraft Full Of Eels Apr 06 '14 at 20:24

2 Answers2

1

wait and notify are meant to be used for synchronization. It seems to me that you wanted to use methods like Thread.suspend(), Thread.stop() and Thread.resume(), but those have been deprecated for the risk of problems with lock that they cause.

The solution is to use a helper variable that the thread will check periodically to see if it should be running, otherwise, yield(or sleep)

Why not to use suspend, stop or resume: http://docs.oracle.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html

Simple solutions: How to Pause and Resume a Thread in Java from another Thread

http://www.tutorialspoint.com/java/java_thread_control.htm

Community
  • 1
  • 1
eduardohl
  • 1,176
  • 2
  • 10
  • 23
0

Here is a simple snapshot that might get you started :

class PausableThread extends Thread {
        private volatile boolean isPaused;

        @Override
        public void run() {
            while (true /* or some other termination condition */) {
                try {
                    waitUntilResumed();
                    doSomePeriodicAction();
                } catch (InterruptedException e) {
                    // we've been interrupted. Stop
                    System.out.println("interrupted. Stop the work");
                    break;
                }
            }
        }

        public void pauseAction() {
            System.out.println("paused");
            isPaused = true;
        }

        public synchronized void resumeAction() {
            System.out.println("resumed");
           isPaused = false; 
           notifyAll();
        }

        // blocks current thread until it is resumed
        private synchronized void waitUntilResumed() throws InterruptedException {
            while (isPaused) {
                wait();
            }
        }

        private void doSomePeriodicAction() throws InterruptedException {
            System.out.println("doing something");
            thread.sleep(1000);
        }

    }

So, you start your thread somewhere new PausableThread().start();

And then in your button/keypress listeners on UI thread you call
in OnPauseKeyPress listener mPausableThread.pauseAction();,
and for OnResumeKeyPress you call mPausableThread.resumeAction();

To stop the tread altogether, just interrupt it : mPausableThread.interrupt();

Hope that helps.

kiruwka
  • 9,250
  • 4
  • 30
  • 41