I need to stop thread somehow for 1 sec while thread is in critical section locked by ReentrantLock
.
My code is :
public class Lock implements Runnable {
private ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
try {
lock.lock();
System.out.println(Thread.currentThread().getName() + " is running !");
lock.wait(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public static void main(String[] args) {
Lock lock = new Lock();
Thread thread = new Thread(lock);
thread.start();
}
}
When I call lock.wait(1000)
in run() method it throws IllegalMonitorStateException
.
Why is this exception if I obtained monitor by lock.lock()
method?
The same happens when I call super.wait(1000)
instead of lock.wait(1000)
.