📌  相关文章
📜  Java中的 LinkedBlockingQueue put() 方法及示例

📅  最后修改于: 2022-05-13 01:55:14.395000             🧑  作者: Mango

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
// 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 linkedQueue
            = new LinkedBlockingQueue(capacityOfQueue);
 
        // Add element using 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);
    }
}


Java
// 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 linkedQueue
            = new LinkedBlockingQueue(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();
        }
    }
}


Java
// 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 linkedQueue
            = new LinkedBlockingQueue(capacityOfQueue);
 
        // try to put null value in put method
        try {
            linkedQueue.put(null);
        }
        catch (Exception e) {
            // print error details
            System.out.println("Exception: " + e);
        }
    }
}


输出:
Items in Queue are [Karan, Suraj, Harsh, Rahul]

程序 2:在 LinkedBlockingQueue 中使用 put 方法添加 Employee 对象

Java

// 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 linkedQueue
            = new LinkedBlockingQueue(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

// 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 linkedQueue
            = new LinkedBlockingQueue(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: Java/util/concurrent/LinkedBlockingQueue.html#put-E-