[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2656
  • Last Modified:

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
0
richardsimnett
Asked:
richardsimnett
  • 3
1 Solution
 
CEHJCommented:
wait and notify have to be call from synchronized blocks
0
 
Peter KwanCommented:
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();
}
0
 
CEHJCommented:
:-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now