Método DelayQueue que queda de la capacidad() en Java con ejemplos

El método restante de la capacidad() de DelayQueue siempre devuelve Integer.MAX_VALUE porque DelayQueue no tiene restricciones de capacidad. Eso significa que, independientemente del tamaño de DelayQueue, devuelve el mismo resultado, es decir, Integer.MAX_VALUE.

Sintaxis: 

public int remainingCapacity ()

Valor devuelto: la función devuelve Integer.MAX_VALUE .
Excepción: No hay excepciones presentes.

Los siguientes programas ilustran el método DelayQueue.remainingCapacity():

Programa:  

Java

// Java Program Demonstrate DelayQueue remainingCapacity() 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));
 
        // print the size of DelayQueue
        System.out.println("Size of the DelayQueue: "
                           + DQ.size());
 
        // remainingCapacity() method always returns Integer.MAX_VALUE
        System.out.println("DelayQueue Remaining Capacity:"
                           + DQ.remainingCapacity());
        // poll() method for removing head of the DelayQueue
        DQ.poll();
        // print the size of DelayQueue
        System.out.println("Size of the DelayQueue: "
                           + DQ.size());
        // remainingCapacity() method always returns Integer.MAX_VALUE
        System.out.println("DelayQueue Remaining Capacity:"
                           + DQ.remainingCapacity());
        // poll() method for removing head of the DelayQueue
        DQ.poll();
        // print the size of DelayQueue
        System.out.println("Size of the DelayQueue: "
                           + DQ.size());
        // remainingCapacity() method always returns Integer.MAX_VALUE
        System.out.println("DelayQueue Remaining Capacity:"
                           + DQ.remainingCapacity());
    }
}
Producción: 

Size of the DelayQueue: 2
DelayQueue Remaining Capacity:2147483647
Size of the DelayQueue: 1
DelayQueue Remaining Capacity:2147483647
Size of the DelayQueue: 0
DelayQueue Remaining Capacity:2147483647

 

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 *