-1

Possible Duplicate:
Java Wait and Notify: IllegalMonitorStateException

What is the problem with

    private final Object lock; 
public synchronized void run() {
    while (numItersCompleted < maxNumIters) {
        while (guiState == GuiState.PAUSED) {
            try {
                lock.wait(); // problematic line

Throws:

java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:502)
Community
  • 1
  • 1
Trup
  • 1,635
  • 13
  • 27
  • 40
  • This is a duplicate question. You can't `wait()` or `notify()` on an object unless you are in a `synchronized(lock)` block on it. – Gray Jul 26 '12 at 23:23
  • You synchronised on `this`, but you waited on `lock`. – Neil Jul 26 '12 at 23:40

1 Answers1

0

You didn't synchronized around the lock.

public void run() {
    while (numItersCompleted < maxNumIters) {
        while (guiState == GuiState.PAUSED) {
            try {
                synchronized (lock) {
                    lock.wait(); // problematic line
                }

I was going to add a link, but Gary bet me to it

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366