Threads : problems with wait() and notify()
Hi,
I'm trying to implement a pause system into a working thread.
In order to make code-reuse, i tried to create an extension of the Thread class like follows:
publicclass ControllableThreadextends Threadimplements Controllable
{
publicenum ThreadActiveStatus{ paused, running};
publicenum ThreadCommand{ start, stop};
private ThreadActiveStatus activeStatus;
private ThreadCommand command;
private Job job;
private ThreadStatusListener listener;
private ExecutionLock lock;
public ControllableThread(ExecutionLock arg0)
{
super();
this.lock = arg0;
this.job =null;
}
publicvoid run()
{
while (true)
{
this.lock.getMonitor();
if (this.job !=null)
{
while (this.command == ThreadCommand.start)
{
// on ns a pas demand?d'arreter, alors, on continue...
this.setActiveStatus(ThreadActiveStatus.running);
this.job.jobRun();
}
//le thread a 閠?mis en pause.
this.setActiveStatus(ThreadActiveStatus.paused);
try
{
this.lock.wait();
}
catch (InterruptedException e){}
}
}
}
privatevoid setActiveStatus(ThreadActiveStatus activeStatus)
{
this.activeStatus = activeStatus;
if (this.listener !=null)
{
this.listener.onThreadStatusUpdate(this.activeStatus);
}
}
public ThreadActiveStatus getActiveStatus()
{
return activeStatus;
}
publicvoid setJob(Job arg0)
{
this.job = arg0;
}
}
ExecutionLock is a very simple object on which i will call wait() and notify().
publicclass ExecutionLock
{
privateint monitorNumber = 0;
public ExecutionLock()
{}
publicsynchronizedvoid getMonitor()
{
this.monitorNumber++;
}
}
The method is completely useless, it's only here to get the monitor (as written here: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html#notify()]
So, when i create that thread and start() it, it should be paused.
but " this.lock.wait() " throws IllegalMonitorStateException().
I really can't figure out why i have such an error, as i owned the monitor 13 lines before. Can you tell me where my error lies?
Thanks in advance.

