Java Concurrency: synchronized (this) => and this.wait () and this.notify ()

I would appreciate your help in understanding the "Concurrency Example" from: http://forums.sun.com/thread.jspa?threadID=735386

Qute start:

public synchronized void enqueue(T obj) {
    // do addition to internal list and then...
    this.notify();
}

public synchronized T dequeue() {
    while (this.size()==0) {
        this.wait(); 
    }
    return // something from the queue
}

End quote:

My question is: Why is this code valid?

=> When I synchronize a method like " public synchronized" =>, I synchronize in "Object Instance ==> this". However, in the above example:

  • Call "dequeue" I will get a "lock / monitor" on this

  • Now I am in the dequeue method. Since the list is zero, the calling thread will be " waited"

  • , , - ( ), "dequeue" , "dequeue" this: "enequeue", "this".

Backround: : - (List of Connections) , . , ?

Jens

+3
2

: http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#wait()

. , , , notify notifyAll. , .

, . wait(), , , , enqueue ( , this). , , .

+7

, , . : wait()

+1

Source: https://habr.com/ru/post/1746121/


All Articles