El método put(E e) de LinkedBlockingQueue inserta el elemento pasado como parámetro al método al final de esta LinkedBlockingQueue, si la cola no está llena. Si la cola está llena, este método esperará a que haya espacio disponible y, una vez que haya espacio disponible, insertará el elemento en LinkedBlockingQueue.
Sintaxis:
public void put(E e) throws InterruptedException
Parámetro: este método toma un parámetro obligatorio e que es el elemento que se insertará en LinkedBlockingQueue.
Valor devuelto: el método no devuelve nada.
Excepción: este método arroja las siguientes excepciones:
- InterruptedException : cuando la interrupción ocurrió en el momento de esperar a que la cola estuviera disponible
- NullPointerException : si el elemento pasado al método es nulo
Los siguientes programas ilustran el método put(E e) de la clase LinkedBlockingQueue:
Programa 1: Imprime el elemento de LinkedBlockingQueue después de agregar nuevos nombres a la cola usando el método put().
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<String> linkedQueue = new LinkedBlockingQueue<String>(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); } }
Items in Queue are [Karan, Suraj, Harsh, Rahul]
Programa 2: agregue el objeto Employee usando el método put en LinkedBlockingQueue
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<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]
Programa 3: para mostrar NullPointerException lanzada por el método put()
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<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
Referencia: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#put-E-
Publicación traducida automáticamente
Artículo escrito por AmanSingh2210 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA