How it works...

All the operations between the Producer and the Consumer classes can be easily resumed with the help of the following schema:

Thread synchronization with event objects

In particular, the Producer and the Consumer classes have the following behavior:

  • Producer acquires a lock, adds an item to the queue, and notifies this event to Consumer (set event). It then sleeps until it receives a new item to add.
  • Consumer acquires a block and then begins to listen to the elements in a continuous cycle. The moment the event arrives, the consumer abandons the block, thus allowing other producers/consumers to enter and acquire the block. If Consumer is reactivated, then it reacquires the lock by safely processing new items from the queue:
2019-02-02 18:23:35,125 Thread-1 INFO Producer notify: item 68 appended by Thread-1
2019-02-02 18:23:35,133 Thread-2 INFO Consumer notify: 68 popped by Thread-2
2019-02-02 18:23:37,138 Thread-1 INFO Producer notify: item 45 appended by Thread-1
2019-02-02 18:23:37,143 Thread-2 INFO Consumer notify: 45 popped by Thread-2
2019-02-02 18:23:39,148 Thread-1 INFO Producer notify: item 78 appended by Thread-1
2019-02-02 18:23:39,153 Thread-2 INFO Consumer notify: 78 popped by Thread-2
2019-02-02 18:23:41,158 Thread-1 INFO Producer notify: item 22 appended by Thread-1
2019-02-02 18:23:43,173 Thread-1 INFO Producer notify: item 48 appended by Thread-1
2019-02-02 18:23:43,178 Thread-2 INFO Consumer notify: 48 popped by Thread-2
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.171.180