Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
LinkedBlockingQueue remainingCapacity() method in Java
Next article icon

LinkedBlockingQueue put() method in Java with Examples

Last Updated : 30 Sep, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

The put(E e) method of LinkedBlockingQueue inserts element passed as parameter to method at the tail of this LinkedBlockingQueue, if queue is not full. If the queue is full, then this method will wait for space to become available and after space is available, it inserts the element to LinkedBlockingQueue.

Syntax: 

public void put(E e) throws InterruptedException

Parameter: This method takes a mandatory parameter e which is the element to be inserted in LinkedBlockingQueue.
Return Value: The method does not return anything.

Exception: This method throws following exceptions:  

  • InterruptedException- when the interruption occurred at time of waiting for queue to become available
  • NullPointerException- if the element passed to method is null

Below programs illustrates put(E e) method of LinkedBlockingQueue class:

Program 1: Prints Element of LinkedBlockingQueue after adding new names to queue using put() method. 

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);     } } 

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

 

Program 2: Add Employee object using put method in 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();         }     } } 

Output: 
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]

 

Program 3: To show NullPointerException thrown by put() method 

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);         }     } } 

Output: 
Exception: java.lang.NullPointerException

 

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


Next Article
LinkedBlockingQueue remainingCapacity() method in Java

A

AmanSingh2210
Improve
Article Tags :
  • Java
  • Java-Collections
  • Java - util package
  • java-basics
  • Java-Functions
  • Java-LinkedBlockingQueue
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

    LinkedBlockingQueue Class in Java
    The LinkedBlockingQueue in Java is part of the java.util.concurrent package and implements the BlockingQueue interface. It provides a thread-safe, bounded, or unbounded queue used for managing tasks in a producer-consumer scenario. This queue can be used in multithreaded environments where one threa
    8 min read
    LinkedBlockingQueue clear() method in Java
    The clear() method of LinkedBlockingQueue removes all of the elements from this queue. After applying this method the queue will become empty. Syntax: public void clear() Below programs illustrates clear() method of LinkedBlockingQueue class: Program 1: Java // Java Program Demonstrate clear() // me
    2 min read
    LinkedBlockingQueue iterator() method in Java
    The iterator() method of LinkedBlockingQueue returns an iterator of the same elements, as this LinkedBlockingQueue, in a proper sequence. The elements returned from this method contains all the elements in order from first(head) to last(tail) of LinkedBlockingQueue. The returned iterator is weakly c
    3 min read
    LinkedBlockingQueue drainTo() method in Java
    The drainTo(Collection col) method of LinkedBlockingQueue removes all available elements from this LinkedBlocking Queue and adds them to the given collection passed as a parameter. drainTo(Collection<? super E> col) The drainTo(Collection<? super E> col) method of LinkedBlockingQueue rem
    7 min read
    LinkedBlockingQueue | offer() Method in JAVA
    There is two types of offer() method for LinkedBlockingQueue class : offer(E e, long timeout, TimeUnit unit) The offer(E e, long timeout, TimeUnit unit) method of LinkedBlockingQueue inserts the element passed as parameter to method at the tail of this LinkedBlockingQueue if queue is not full. It wi
    6 min read
    LinkedBlockingQueue peek() method in Java
    The peek() method of LinkedBlockingQueue returns the head of the LinkedBlockingQueue. It retrieves the value of the head of LinkedBlockingQueue but does not remove it. If the LinkedBlockingQueue is empty then this method returns null. Syntax: public E peek() Return Value: This method returns the hea
    3 min read
    LinkedBlockingQueue poll() method in Java
    There is two types of poll() method in LinkedBlockingQueue. poll() The poll() method of LinkedBlockingQueue returns the head of LinkedBlockingQueue by removing that element from queue. It can be said that this method retrieves and removes element from head of this LinkedBlockingQueue. If queue is em
    6 min read
    LinkedBlockingQueue put() method in Java with Examples
    The put(E e) method of LinkedBlockingQueue inserts element passed as parameter to method at the tail of this LinkedBlockingQueue, if queue is not full. If the queue is full, then this method will wait for space to become available and after space is available, it inserts the element to LinkedBlockin
    3 min read
    LinkedBlockingQueue remainingCapacity() method in Java
    The remainingCapacity() method of LinkedBlockingQueue returns the number of more elements that can be added to LinkedBlockingQueue without blocking. The Capacity returned arises three cases: If remaining Capacity is Zero, then no more elements can be added to the LinkedBlockingQueue.If remaining Cap
    3 min read
    LinkedBlockingQueue remove() method in Java
    The remove(Object obj) method of LinkedBlockingQueue removes only one instance of the given Object, passed as parameter, from this LinkedBlockingQueue if it is present. It removes an element e such that obj.equals(e) and if this queue contains one or more instance of element e. This method returns t
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences