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


Java LinkedBlockingQueue put()用法及代碼示例


如果隊列未滿,LinkedBlockingQueue的put(E e)方法將作為參數傳遞的元素插入此LinkedBlockingQueue尾部的方法。如果隊列已滿,則此方法將等待空間變為可用,並且在空間可用之後,它將元素插入LinkedBlockingQueue。

用法:

public void put(E e) throws InterruptedException

參數:此方法采用強製性參數e,該參數是要插入LinkedBlockingQueue中的元素。


返回值:該方法不返回任何內容。

異常:此方法引發以下異常:

  • InterruptedException–在等待隊列可用時發生中斷
  • NullPointerException –如果傳遞給方法的元素為null

下麵的程序演示了LinkedBlockingQueue類的put(E e)方法:

示例1:使用put()方法將新名稱添加到隊列後,打印LinkedBlockingQueue的元素。

// Java Program Demonstrate put(E e) 
// method of LinkedBlockingQueue 
  
import java.util.concurrent.LinkedBlockingQueue; 
  
public class GFG { 
  
    public static void main(String[] args) 
        throws InterruptedException 
    { 
        // define capacity of LinkedBlockingQueue 
        int capacityOfQueue = 4; 
  
        // create object of LinkedBlockingQueue 
        LinkedBlockingQueue<String> linkedQueue 
            = new LinkedBlockingQueue<String>(capacityOfQueue); 
  
        // Add element usin put() method 
        linkedQueue.put("Karan"); 
        linkedQueue.put("Suraj"); 
        linkedQueue.put("Harsh"); 
        linkedQueue.put("Rahul"); 
  
        // print elements of queue 
        System.out.println("Items in Queue are " + linkedQueue); 
    } 
}
輸出:
Items in Queue are [Karan, Suraj, Harsh, Rahul]

示例2:在LinkedBlockingQueue中使用put方法添加Employee對象

// Java Program Demonstrate put() 
// method of LinkedBlockingQueue 
  
import java.util.Iterator; 
import java.util.concurrent.LinkedBlockingQueue; 
public class GFG { 
  
    public void PutDemo() throws InterruptedException 
    { 
        // define capacity of LinkedBlockingQueue 
        int capacityOfQueue = 5; 
  
        // create object of LinkedBlockingQueue 
        LinkedBlockingQueue<Employee> linkedQueue 
            = new LinkedBlockingQueue<Employee>(capacityOfQueue); 
  
        // Add element to LinkedBlockingQueue 
        Employee emp1 = new Employee("Ranjeet", "Tester", "29000", 27); 
        Employee emp2 = new Employee("Sanjeet", "Manager", "98000", 34); 
        Employee emp3 = new Employee("Karan", "Analyst", "44000", 30); 
  
        // Add Employee Objects to linkedQueue 
        // Using put(E e) 
        linkedQueue.put(emp1); 
        linkedQueue.put(emp2); 
        linkedQueue.put(emp3); 
  
        System.out.println("Details of Employees:"); 
        // print details of linkedQueue 
        Iterator itr = linkedQueue.iterator(); 
        while (itr.hasNext()) 
            System.out.println(itr.next()); 
    } 
  
    // create an Employee Object with name, 
    // position, salary and age as attributes 
    public class Employee { 
  
        public String name; 
        public String position; 
        public String salary; 
        public int Age; 
        Employee(String name, String position, 
                 String salary, int age) 
        { 
            this.name = name; 
            this.position = position; 
            this.salary = salary; 
            this.Age = age; 
        } 
        @Override
        public String toString() 
        { 
            return "Employee [name=" + name + ", position="
                + position + ", salary=" + salary 
                + ", Age=" + Age + "]"; 
        } 
    } 
  
    // Main Method 
    public static void main(String[] args) 
    { 
        GFG gfg = new GFG(); 
        try { 
            gfg.PutDemo(); 
        } 
        catch (InterruptedException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 
    } 
}
輸出:
Details of Employees:
Employee [name=Ranjeet, position=Tester, salary=29000, Age=27]
Employee [name=Sanjeet, position=Manager, salary=98000, Age=34]
Employee [name=Karan, position=Analyst, salary=44000, Age=30]

示例3:顯示put()方法拋出的NullPointerException

// Java Program Demonstrate put(E e) 
// method of LinkedBlockingQueue 
  
import java.util.concurrent.LinkedBlockingQueue; 
  
public class GFG { 
  
    public static void main(String[] args) 
        throws InterruptedException 
    { 
        // define capacity of LinkedBlockingQueue 
        int capacityOfQueue = 4; 
  
        // create object of LinkedBlockingQueue 
        LinkedBlockingQueue<String> linkedQueue 
            = new LinkedBlockingQueue<String>(capacityOfQueue); 
  
        // try to put null value in put method 
        try { 
            linkedQueue.put(null); 
        } 
        catch (Exception e) { 
            // print error details 
            System.out.println("Exception: " + e); 
        } 
    } 
}
輸出:
Exception: java.lang.NullPointerException

參考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#put-E-



相關用法


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