A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/blockingqueue-poll-method-in-java-with-examples/ below:

BlockingQueue poll() method in Java with examples

BlockingQueue poll() method in Java with examples

Last Updated : 14 Oct, 2019

The

poll(long timeout, TimeUnit unit)

method of

BlockingQueue

interface returns the head of BlockingQueue by removing that element from the queue. It can be said that this method retrieves and removes element from head of this LinkedBlockingQueue. If queue is empty, then poll() method will wait till a specified time for an element to become available.

Syntax:
public E poll(long timeout, TimeUnit unit) throws 
Parameters:

This method takes two mandatory parameters:

Return Value:

This method retrieves and removes element from head of this LinkedBlockingQueue, or null if the specified waiting time elapses before an element is available.

Exception

This method throws

InterruptedException

if method interrupted while waiting for an element to become available.

Note

: The

poll()

method of

BlockingQueue

has been inherited from the

Queue

class in Java. Below programs illustrates poll(long timeout, TimeUnit unit) method of BlockingQueue:

Program 1

:

Java
// Java program to demonstrate
// poll(long timeout, TimeUnit unit)
// method of LinkedBlockingQueue

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

public class GFG {

    public static void main(String[] args)
        throws InterruptedException
    {
        // define capacity of BlockingQueue
        int capacityOfQueue = 4;

        // create object of BlockingQueue
        BlockingQueue<String> BQ
            = new LinkedBlockingQueue<String>(capacityOfQueue);

        // Add element to BlockingQueue
        BQ.add("Ravi");
        BQ.add("Suraj");
        BQ.add("Harsh");

        // print elements of queue
        System.out.println("Items in Queue are " + BQ);

        // Try to poll elements from BQ
        // using poll(long timeout, TimeUnit unit) method
        System.out.println("Removing item From head: "
                           + BQ.poll(5, TimeUnit.SECONDS));

        // print queue details
        System.out.println("Now Queue Contains" + BQ);

        // using poll(long timeout, TimeUnit unit) method
        System.out.println("Removing item From head: "
                           + BQ.poll(5, TimeUnit.SECONDS));

        // print queue details
        System.out.println("Now Queue Contains" + BQ);

        // using poll(long timeout, TimeUnit unit) method
        System.out.println("Removing item From head: "
                           + BQ.poll(5, TimeUnit.SECONDS));

        // print queue details
        System.out.println("Now Queue Contains" + BQ);

        // using poll(long timeout, TimeUnit unit) method
        System.out.println("Removing item From head: "
                           + BQ.poll(5, TimeUnit.SECONDS));
        // print queue details
        System.out.println("Now Queue Contains" + BQ);
    }
}
Output:
Items in Queue are [Ravi, Suraj, Harsh]
Removing item From head: Ravi
Now Queue Contains[Suraj, Harsh]
Removing item From head: Suraj
Now Queue Contains[Harsh]
Removing item From head: Harsh
Now Queue Contains[]
Removing item From head: null
Now Queue Contains[]
Program 2

:

Java
// Java program to demonstrate
// poll(long timeout, TimeUnit unit)
// method of LinkedBlockingQueue

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

public class GFG {

    public static void main(String[] args)
        throws InterruptedException
    {
        // define capacity of BlockingQueue
        int capacityOfQueue = 2;

        // create object of BlockingQueue
        BlockingQueue<String> BQ
            = new LinkedBlockingQueue<String>(capacityOfQueue);

        // Add element to BlockingQueue
        BQ.add("Gopal");
        BQ.add("GFG");

        // print elements of queue
        System.out.println("Items in Queue are " + BQ);

        // Try to poll elements from BQ
        // using poll(long timeout, TimeUnit unit) method
        System.out.println("Removing item From head: "
                           + BQ.poll(2, TimeUnit.SECONDS));

        // print queue details
        System.out.println("Now Queue Contains" + BQ);
    }
}
Output:
Items in Queue are [Gopal, GFG]
Removing item From head: Gopal
Now Queue Contains[GFG]
Reference

:

https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html#poll(long, %20java.util.concurrent.TimeUnit)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4