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


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


BlockingQueue的remainingCapacity()方法返回可以添加到BlockingQueue而不會阻塞的更多元素的數量。

返回的容量在以下三種情況下出現:

  • 如果剩餘容量為零,則不能將更多元素添加到BlockingQueue。
  • 如果BlockingQueue的剩餘Capacity等於Queue的大小,則無法從隊列中刪除任何元素,因為在這種情況下,隊列為空。
  • 在任何其他情況下,Capacity始終等於此BlockingQueue的初始容量與此BlockingQueue的當前大小之間的差。

用法:


public int remainingCapacity()

返回值:此方法返回BlockingQueue的剩餘容量。

注意:BlockingQueue的remainingCapacity()方法已從Java中的Queue類繼承。

以下示例程序旨在說明BlockingQueue類的remainingCapacity()方法:

示例1:

// Java Program Demonstrate remainingCapacity() 
// method of BlockingQueue 
  
import java.util.concurrent.LinkedBlockingQueue; 
import java.util.concurrent.BlockingQueue; 
public class GFG { 
  
    public static void main(String[] args) 
    { 
        // define capacity of BlockingQueue 
        int capacityOfQueue = 7; 
  
        // create object of BlockingQueue 
        BlockingQueue<String> BQ 
            = new LinkedBlockingQueue<String>( 
                capacityOfQueue); 
  
        // Add element to BlockingQueue 
        BQ.add("John"); 
        BQ.add("Tom"); 
        BQ.add("Clark"); 
        BQ.add("Kat"); 
  
        // find remaining Capacity  of BQ 
        // using remainingCapacity() method 
        int remainingCapacity 
            = BQ.remainingCapacity(); 
  
        // print result 
        System.out.println("Queue is " + BQ); 
  
        // print head of the queue 
        System.out.println("Remaining Capacity of Queue is "
                           + remainingCapacity); 
    } 
}
輸出:
Queue is [John, Tom, Clark, Kat]
Remaining Capacity of Queue is 3

示例2:

// Java Program Demonstrate remainingCapacity() 
// method of BlockingQueue 
  
import java.util.concurrent.LinkedBlockingQueue; 
import java.util.concurrent.BlockingQueue; 
public class GFG { 
  
    public void findPeek() 
    { 
        // define capacity of BlockingQueue 
        int capacityOfQueue = 7; 
  
        // create object of BlockingQueue 
        BlockingQueue<Employee> BQ 
            = new LinkedBlockingQueue<Employee>( 
                capacityOfQueue); 
  
        // Add element to BlockingQueue 
        Employee emp1 
            = new Employee("Ravi", "Tester", "39000"); 
        Employee emp2 
            = new Employee("Sanjeet", "Manager", "98000"); 
  
        // Add Employee Objects to BQ 
        BQ.add(emp1); 
        BQ.add(emp2); 
  
        // add element and find remaining capacity 
        // follow same process again 
        // until the queue becomes full 
        while (BQ.size() != 7) { 
  
            // adding emp2 again and again to queue 
            System.out.println( 
                "Adding employee is success "
                + BQ.offer(emp2)); 
  
            // find remaining capacity of BQ 
            // using remainingCapacity() method 
            int remain = BQ.remainingCapacity(); 
  
            // print remaining capacity  value 
            System.out.println( 
                "Remaining Capacity of list :"
                + remain); 
        } 
    } 
  
    // create an Employee Object with name, 
    // position and salary as an attribute 
    public class Employee { 
  
        public String name; 
        public String position; 
        public String salary; 
        Employee(String name, 
                 String position, 
                 String salary) 
        { 
            this.name = name; 
            this.position = position; 
            this.salary = salary; 
        } 
        @Override
        public String toString() 
        { 
            return "Employee [name=" + name 
                + ", position="
                + position + ", salary="
                + salary + "]"; 
        } 
    } 
  
    // Main Method 
    public static void main(String[] args) 
    { 
        GFG gfg = new GFG(); 
        gfg.findPeek(); 
    } 
}
輸出:
Adding employee is success true
Remaining Capacity of list :4
Adding employee is success true
Remaining Capacity of list :3
Adding employee is success true
Remaining Capacity of list :2
Adding employee is success true
Remaining Capacity of list :1
Adding employee is success true
Remaining Capacity of list :0

參考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html#remainingCapacity()



相關用法


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