我对以下两个描述感到非常困惑:
- "wait方法会阻塞调用线程并释放监视器锁"
- "notify方法会解除一个等待线程的阻塞,但不会释放监视器锁"
我的问题如下:
I know each object in Java has a lock, but what is the "monitor lock" means? is it the same as the oject's lock?
Why notify method needs to give up the monitor lock?
If I try to make a object waiting with the following code:
class simpleTask extends Thread { int waitingTime; public simpleTask(int waitingTime) { this.waitingTime = waitingTime; } public void run() { synchronized(this) // this is a reference of current object { try { this.wait(waitingTime); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
像上面的第一个描述一样,这是指当前对象被synchronized关键字阻塞,然后wait方法释放锁吗?