Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

Blocking Queue as Synchronizer

hi

<<A Synchronizer is a object  that coordinates the control flow of threads based on its state,
Blocking queue can act as Synchornizer.>>--
 concurrency in practice- brian goetz page 94

Can anyone explain is simple terms with a simple example what this means?

Thanks
0
royjayd
Asked:
royjayd
2 Solutions
 
krakatoaCommented:
Read this.
(If you want to).
0
 
ValeriCommented:
"A blocking queue is a queue that blocks when you try to dequeue from it and the queue is empty, or if you try to enqueue items to it and the queue is already full."
Because of that "queue" and "dequeue" operations need to be synchronized, because many threads are trying to "queue" and "dequeue" in one and the same time.
That's way "queue" and "dequeue" methods are synchronized, so it means that "this" (which is the BlockingQueue) is used as synchornizer. So in this case the blocking queue work's as synchornizer to it's self.
Btw in java 1.5 implementations of BlockingQueue (for example ArrayBlockingQueue) don't use the object itself for the synchronization. But the meaning in the book is that "queue" and "dequeue" methods could be synchronised by the BlockingQueue.

public synchronized void enqueue(Object item) {
}

public synchronized Object dequeue() {
}

both of this methods are synchronised on "this" object, which is the instance of the BlockingQueue.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now