![]() ![]() Producers will keep adding the items in a blocking queue, and the consumer will drain items, repeatedly, after some delay. To keep things simple, let us call the Producer and Consumer. We are creating two tasks to demonstrate the usage of the drainTo () method. Int drainTo(Collection c, int ma圎lements) The second method drains at most the ma圎lements number of available elements. In the case of poll() method, it retrieves the head element of the queue and then removes the head element of the queue. For generic collections, incompatible class types will result in ClassCastException.If collection is immutable the method will throw UnsupportedOperationException.The behavior of this method is undefined if the provided collection is modified while the draining is in progress.It provides better performance than polling all elements one by one.It deletes the element from the container. The poll() method is present in the Queue interface (java.util package) The poll() method is used to remove the element present at the front of the Queue and return it. The drainTo() removes all available elements from the specified queue and adds them to the given collection. There are various methods present in Queue such as add(), peak(), element(), poll()etc.And a report writer thread periodically polling to queue and writing records in excel. There could be an ExecutorService with multiple threads processing records and putting them in the blocking queue. One such example could be an excel report generator. ![]() ![]() Draining is needed in situations when multiple producer threads are adding elements into a BlockingQueue and a consumer thread, periodically, polls multiple items from the queue and processes them together. Use poll() to return the first Reference object on the queue the method. Learn to use BlockingQueue.drainTo() method for draining a queue (polling all or a specific number of elements from the queue) into a Collection. Java 1.2 This abstract class represents some type of indirect. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |