Link to home
Start Free TrialLog in
Avatar of richardsimnett
richardsimnett

asked on

Why do I keep getting IllegalMonitorStateException on thread wait() and notify()?

Hello,
I am writing a worker thread, for a thread pool. Basically what the thread does after it is initialized and started, is sit idle until it is given a job, and is notified to start. Here is my code for this thread:


package callspool;
import java.util.*;

/**
 *
 * @author rick
 */
public class workerThread extends Thread{
   
    Hashtable server;
    Database db;
    boolean running = false;
    public boolean exit = false;
    Hashtable job = null;
   
    /** Creates a new instance of workerThread */
    public workerThread(Hashtable server)
    {
        super();
        this.server = server;
    }
   
    public void run()
    {
        System.out.println("Worker Thread Started.");
        while (exit == false)
        {
            //sleep or process the job
            if (job != null)
            {
                System.out.println("Start Job #" + job.get("id").toString());
                running = true;
               
                //mark the job in progress
               
                //execute the job
               
                //mark the job completed
               
                //de-initialize the job
                System.out.println("End Job.");
                running = false;
                job = null;
            }
            else
            {
               
                try
                {
                    this.wait();
                }
                catch(InterruptedException e)
                {
                }
            }
        }
    }
   
    public void startWork(Hashtable job)
    {
        this.job = job;
        this.notify();
    }
   
   
}


For some reason when I try to wait() initially I get the following error:

Worker Thread Started.
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
        at java.lang.Object.wait(Native Method)
        at java.lang.Object.wait(Object.java:485)
        at callspool.workerThread.run(workerThread.java:60)

Then I get this error when the notify is triggered:

Found 1 available worker threads.
Assigning job #0
Exception in thread "Timer-0" java.lang.IllegalMonitorStateException
        at java.lang.Object.notify(Native Method)
        at callspool.workerThread.startWork(workerThread.java:72)
        at callspool.threadPool$jobs.run(threadPool.java:93)
        at java.util.TimerThread.mainLoop(Timer.java:512)
        at java.util.TimerThread.run(Timer.java:462)

What is causing these errors and how do I fix them?

Worth 500 points.

Thanks,
Rick
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is beacuse your wait & notify methods are not within the code that is synchronized.  Please take the following as the reference:
http://www.jchq.net/tutorial/07_03Tut.htm

You may wrap your wait & notify call as the following:
synchronized(this) {
  this.wait();
}

synchronized(this) {
  this.notify();
}
:-)