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程序演示LinkedBlockingQueue的put(E e)方法
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String [] args)
throws InterruptedException
{
//定义LinkedBlockingQueue的容量
int capacityOfQueue = 4;
//创建LinkedBlockingQueue对象
LinkedBlockingQueue linkedQueue
= new LinkedBlockingQueue (capacityOfQueue);
//使用put()方法添加元素
linkedQueue.put(“Karan”);
linkedQueue.put(“Suraj”);
linkedQueue.put(“Harsh”);
linkedQueue.put(“Rahul”);
//打印队列的元素
System.out.println(“排队物品是”+ linkedQueue);
}
} ```
输出:
排队物品是 [Karan,Suraj,Harsh,Rahul]
程序2: 使用put方法在LinkedBlockingQueue中添加Employee对象。
// Java程序演示LinkedBlockingQueue的put()方法
import java.util.Iterator;
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public void PutDemo() throws InterruptedException
{
// 定义LinkedBlockingQueue的容量
int capacityOfQueue = 5;
// 创建LinkedBlockingQueue对象
LinkedBlockingQueue<Employee> linkedQueue
= new LinkedBlockingQueue<Employee>(capacityOfQueue);
// 添加元素到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);
// 使用put(E e)添加Employee对象到linkedQueue中
linkedQueue.put(emp1);
linkedQueue.put(emp2);
linkedQueue.put(emp3);
System.out.println("员工的详细信息:");
// 遍历linkedQueue的迭代器并输出
Iterator itr = linkedQueue.iterator();
while (itr.hasNext())
System.out.println(itr.next());
}
//创建一个名字、职位、薪水和年龄为属性的Employee对象
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 "员工 [name=" + name + ", position="
+ position + ", salary=" + salary
+ ", Age=" + Age + "]";
}
}
//主方法
public static void main(String[] args)
{
GFG gfg = new GFG();
try {
gfg.PutDemo();
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
输出:
员工的详细信息:
员工 [name=Ranjeet, position=Tester, salary=29000, Age=27]
员工 [name=Sanjeet, position=Manager, salary=98000, Age=34]
员工 [name=Karan, position=Analyst, salary=44000, Age=30]
程序3: 演示put()方法抛出NullPointerException异常
// Java程序演示LinkedBlockingQueue的put(E e)方法
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// 定义LinkedBlockingQueue的容量
int capacityOfQueue = 4;
// 创建LinkedBlockingQueue对象
LinkedBlockingQueue<String> linkedQueue
= new LinkedBlockingQueue<String>(capacityOfQueue);
// 尝试将null值放入put方法中
try {
linkedQueue.put(null);
}
catch (Exception e) {
// 打印错误信息
System.out.println("异常: " + e);
}
}
}
输出:
异常: java.lang.NullPointerException
参考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#put-E-
极客教程