Método DelayQueue poll() en Java con ejemplos

El método poll() de DelayQueue se usa para recuperar el encabezado de DelayQueue. Este método también elimina la cabeza de DelayQueue. Por lo tanto, cuando se llama al método de encuesta, el tamaño de DelayQueue se reduce en uno.
Sintaxis: 
 

public E poll ()

Parámetros: 
Este método no acepta ningún parámetro.
Devoluciones: 
este método devuelve el encabezado de DelayQueue y también lo elimina de esta DelayQueue.
Excepción:  
puntero nulo Excepción: si el encabezado no está presente, esta función devolverá un valor nulo.
El siguiente programa para ilustrar DelayQueue poll() en Java:
Ejemplo: 
 

Java

// Java Program Demonstrate DelayQueue poll() method
 
import java.util.concurrent.*;
import java.util.*;
 
// The DelayObject for DelayQueue
// It must implement Delayed and
// its getDelay() and compareTo() method
class DelayObject implements Delayed {
 
    private String name;
    private long time;
 
    // Constructor of DelayObject
    public DelayObject(String name, long delayTime)
    {
        this.name = name;
        this.time = System.currentTimeMillis()
                    + delayTime;
    }
 
    // Implementing getDelay() method of Delayed
    @Override
    public long getDelay(TimeUnit unit)
    {
        long diff = time - System.currentTimeMillis();
        return unit.convert(diff, TimeUnit.MILLISECONDS);
    }
 
    // Implementing compareTo() method of Delayed
    @Override
    public int compareTo(Delayed obj)
    {
        if (this.time < ((DelayObject)obj).time) {
            return -1;
        }
        if (this.time > ((DelayObject)obj).time) {
            return 1;
        }
        return 0;
    }
 
    // Implementing toString() method of Delayed
    @Override
    public String toString()
    {
        return "\n{"
            + " " + name + ", time=" + time + "}";
    }
}
 
// Driver Class
public class GFG {
    public static void main(String[] args) throws InterruptedException
    {
 
        // create object of DelayQueue
        // using DelayQueue() constructor
        BlockingQueue<DelayObject> DQ
            = new DelayQueue<DelayObject>();
 
        // Add numbers to end of DelayQueue
        // using add() method
        DQ.add(new DelayObject("A", 1));
        DQ.add(new DelayObject("B", 2));
        DQ.add(new DelayObject("C", 3));
        DQ.add(new DelayObject("D", 4));
 
        // Print delayqueue
        System.out.println("Original DelayQueue: "
                           + DQ + "\n");
        // Print size of original DelayQueue
        System.out.println("Original DelayQueue size: "
                           + DQ.size() + "\n");
 
        // poll() method for returning head of the DelayQueue
        System.out.println("Head of the DelayQueue: "
                           + DQ.poll());
        // print DelayQueue size after poll method
        System.out.println("\nAfter poll() method, DelayQueue size: "
                           + DQ.size());
    }
}
Producción: 

Original DelayQueue: [
{ A, time=1545817370442}, 
{ B, time=1545817370443}, 
{ C, time=1545817370444}, 
{ D, time=1545817370445}]

Original DelayQueue size: 4

Head of the DelayQueue: 
{ A, time=1545817370442}

After poll() method, DelayQueue size: 3

 

Ejemplo: 
 

Java

// Java Program Demonstrate DelayQueue poll() method
 
import java.util.concurrent.*;
import java.util.*;
 
// The DelayObject for DelayQueue
// It must implement Delayed and
// its getDelay() and compareTo() method
class DelayObject implements Delayed {
 
    private String name;
    private long time;
 
    // Constructor of DelayObject
    public DelayObject(String name, long delayTime)
    {
        this.name = name;
        this.time = System.currentTimeMillis()
                    + delayTime;
    }
 
    // Implementing getDelay() method of Delayed
    @Override
    public long getDelay(TimeUnit unit)
    {
        long diff = time - System.currentTimeMillis();
        return unit.convert(diff, TimeUnit.MILLISECONDS);
    }
 
    // Implementing compareTo() method of Delayed
    @Override
    public int compareTo(Delayed obj)
    {
        if (this.time < ((DelayObject)obj).time) {
            return -1;
        }
        if (this.time > ((DelayObject)obj).time) {
            return 1;
        }
        return 0;
    }
 
    // Implementing toString() method of Delayed
    @Override
    public String toString()
    {
        return "\n{"
            + " " + name + ", time=" + time + "}";
    }
}
 
// Driver Class
public class GFG {
    public static void main(String[] args) throws InterruptedException
    {
 
        // create object of DelayQueue
        // using DelayQueue() constructor
        BlockingQueue<DelayObject> DQ
            = new DelayQueue<DelayObject>();
 
        // Print size of original DelayQueue
        System.out.println("Original DelayQueue size: "
                           + DQ.size() + "\n");
 
        // poll() method for returning head of the DelayQueue
        System.out.println("Head of the DelayQueue: "
                           + DQ.poll());
    }
}
Producción: 

Original DelayQueue size: 0

Head of the DelayQueue: null

 

Publicación traducida automáticamente

Artículo escrito por ProgrammerAnvesh y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *