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.IllegalMonitorSt ateExcepti on
at java.lang.Object.wait(Nati ve Method)
at java.lang.Object.wait(Obje ct.java:48 5)
at callspool.workerThread.run (workerThr ead.java:6 0)
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.IllegalMonitorSt ateExcepti on
at java.lang.Object.notify(Na tive Method)
at callspool.workerThread.sta rtWork(wor kerThread. java:72)
at callspool.threadPool$jobs. run(thread Pool.java: 93)
at java.util.TimerThread.main Loop(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
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
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
{
}
}
}
}
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.IllegalMonitorSt
at java.lang.Object.wait(Nati
at java.lang.Object.wait(Obje
at callspool.workerThread.run
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.IllegalMonitorSt
at java.lang.Object.notify(Na
at callspool.workerThread.sta
at callspool.threadPool$jobs.
at java.util.TimerThread.main
at java.util.TimerThread.run(
What is causing these errors and how do I fix them?
Worth 500 points.
Thanks,
Rick
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
See http://www.javaworld.com/jw-04-1996/jw-04-synch.html
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();
}
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();
}
:-)