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


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


Java中的DelayQueue類的size()方法用於返回給定隊列中存在的元素數。如果元素數大於Integer.MAX_VALUE,則返回Integer.MAX_VALUE作為此DelayQueue的大小。

句法

public int size()

參數:它不接受任何參數。


返回值:它返回一個整數,它是隊列的長度。

以下示例程序旨在說明Java中DelayQueue的size()方法:

// Java program to illutrate the size() method 
// of DelayQueue class 
  
import java.util.concurrent.DelayQueue; 
import java.util.concurrent.Delayed; 
import java.util.concurrent.TimeUnit; 
  
public class GFG { 
    public static void main(String args[]) 
    { 
        // Create a DelayQueue Instance 
        DelayQueue<Delayed> queue = new DelayQueue<Delayed>(); 
  
        // Create an object of type Delayed 
        Delayed obj = new Delayed() { 
  
            public long getDelay(TimeUnit unit) 
            { 
                return 24; // some value is returned 
            } 
  
            public int compareTo(Delayed o) 
            { 
                if (o.getDelay(TimeUnit.DAYS) >  
                           this.getDelay(TimeUnit.DAYS)) 
                    return 1; 
                else if (o.getDelay(TimeUnit.DAYS) ==  
                           this.getDelay(TimeUnit.DAYS)) 
                    return 0; 
                return -1; 
            } 
        }; 
  
        // Insert the object to this DelayQueue 
        // Size of the queue after insertion will be 1 
        queue.add(obj); 
  
        // print the size of this DelayQueue using 
        // size() method 
        System.out.println("Size : " + queue.size()); 
    } 
}
輸出:
Size : 1


相關用法


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