This was an interview question to me in Oracle.
There is a stack and a push operation should wait till it completes even if a stack is full and a pop operation should wait till it completes even if the stack is empty.
How can we achieve this?
My answer
Let a thread do the push & pop operation. Push thread has to wait till stack has a vacant space and the pop thread has to wait till the stack has at least one element.
Blocking/waiting stack operation can be achieved by having a thread do this task.
If the stack is full, the push thread(thread which does the push operation) makes a blocking push operation. So it waits to push until the stack has a space left(until a pop happens).
If the stack is empty, the pop thread(thread which does pop operation) makes a blocking pop operation. So it waits to pop until the stack has at least one element(until a push happens).