當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java DelayQueue remainingCapacity()用法及代碼示例


DelayQueue的remainingCapacity()方法始終返回Integer.MAX_VALUE,因為DelayQueue不受容量限製。這意味著,無論DelayQueue的大小如何,它都會返回相同的結果,即Integer.MAX_VALUE。

用法:

public int remainingCapacity ()

返回值:該函數返回Integer.MAX_VALUE。


異常:沒有例外。

以下示例程序旨在說明DelayQueue.remainingCapacity()方法:

程序:

// Java Program Demonstrate DelayQueue remainigCapacity() 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; 
  
    // Contructor 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()); 
  
        // remainigCapacity() 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()); 
        // remainigCapacity() 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()); 
        // remainigCapacity() method always returns Integer.MAX_VALUE 
        System.out.println("DelayQueue Remaining Capacity:"
                           + DQ.remainingCapacity()); 
    } 
}
輸出:
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


相關用法


注:本文由純淨天空篩選整理自ProgrammerAnvesh大神的英文原創作品 DelayQueue remainingCapacity() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。