0

I want to pause and resume a thread by pressing a key. The idea is that the thread generates numbers which are send through a pipe to another thread and the user can pause and resume the thread by pressing the key 'p'. What I have at the moment is this: The thread waits until I press any key and the random number is displayed on screen (output is another thread), then the thread waits until I press another key... but if I press 'p' the thread stops and I cannot get it to resume.

import java.io.IOException;
import java.util.Random;
import java.io.PipedOutputStream;
import java.util.Scanner;


public class Producer extends Thread {
    private static final int MIN = 0;
    private static final int MAX = 60;
    private volatile boolean pause;
    private PipedOutputStream output = new PipedOutputStream();

public Producer(PipedOutputStream output) {
    this.output = output;
}

@Override
public void run() {
    Random rand = new Random();

    Scanner reader = new Scanner(System.in);
    int random;
    String key = "p";
    String keyPressed;
    try {     
        while (true) {
            keyPressed = reader.next();
            if (keyPressed.equalsIgnoreCase(key)) {
                pauseThread();
            } else {
                random = rand.nextInt(MAX - MIN + 1);
                output.write((int) random);
                output.flush();
                Thread.sleep(1000);

            }
            if (pause = true && keyPressed.equalsIgnoreCase(key)) {
                    resumeThread();
            }
        }
        output.close();
    } catch (InterruptedException ex) {
        interrupt();
    } catch (IOException ex) {
        System.out.println("Could not write to pipe.");
    }
}

public synchronized void pauseThread() throws InterruptedException {
    pause = true;
    while (pause)
        wait();
}

public synchronized void resumeThread() throws InterruptedException {
    while (pause) {
        pause = false;
    }
    notify();
}

}

Marta
  • 173
  • 1
  • 2
  • 11
  • okay. what is the question? – Jobin Dec 05 '16 at 13:34
  • You should not do it this way. Pass a `BlockingQueue` to your thread and make it push all of it's numbers through it. You can then just stop reading from the queue to pause your process. – OldCurmudgeon Dec 05 '16 at 13:36
  • Alternatively - you could try http://stackoverflow.com/a/10669623/823393 – OldCurmudgeon Dec 05 '16 at 13:41
  • 1) I think you need at least two Threads, one "worker" and another which pauses/unpauses the worker 2) You should know about `volatile` and `synchronized` before. 3) `pause = true` is not what you think - either avoid this kind of comparisons (just use `pause`) or adopt the C convention of writing the constant to the left (`true = pause` will give an compilation error). 4) Unless its for educational purposes, you should go with the Executors Framework – Gyro Gearless Dec 05 '16 at 13:42

2 Answers2

0

Perhaps the thread being paused is not the one that should read the keyboard.

Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
0

Just comment pauseThread() method inside run Of course, you should comment resumeThread due to these methods are not neede. I mean when you prss 'p' u just need to skip you major methods.

Vyacheslav
  • 26,359
  • 19
  • 112
  • 194