Let’s think of following code fragment, that behaves as expected. Thread runs, then it’s paused and then it’s unpaused and finishes it’s execution:
public static void main(final String[] args) throws InterruptedException {
Executor exec = Executors.newSingleThreadExecutor();
MyThread thread = new MyThread();
exec.execute(thread);
thread.pause();
thread.pause(); // unpause
}
Now let’s add add some sleeping to thread so it’s paused for a while:
public static void main(final String[] args) throws InterruptedException {
Executor exec = Executors.newSingleThreadExecutor();
MyThread thread = new MyThread();
exec.execute(thread);
thread.pause();
Thread.sleep(500);
thread.pause(); // unpause
}
But that code never finishes. Why ?
Here’s implementation of pause method, it checks private boolean field for pausing:
public synchronized void pause() {
paused = (paused) ? false : true;
}
And here is implementation of overriden run method:
@Override
public void run() {
// don't worry, I just need som dummy data to take some cpu time ;)
PriorityQueue<Double> queue = new PriorityQueue<Double>();
Random random = new Random(System.currentTimeMillis());
System.out.println("I stared");
checkPause();
// let's do some big computation
for (int i=0; i<10000000; i++) { // 10 mio
System.out.println(i);
queue.add(random.nextDouble());
if (i % 3 == 0) {
queue.poll(); // more complex operation
}
}
System.out.println("I'm done");
}
private void checkPause() {
synchronized (this) {
if (paused) {
while (paused != false) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
When I tried debugging, I’ll end on wait() method. Then it just waits :/
When you call
wait(), your thread waits until another thread calls itsnotify()method.You’re not calling
notify()on the thread from your main thread.Also note that
synchronize(this)is the same thing as synchronizing the method; it’s using the object itself as the lock. Once your thread hitswait()your main thread will block onthread.unpause()because thecheckPause()method has the lock.