Monday, November 18, 2024
Google search engine
HomeLanguagesJavaLinkedBlockingQueue | offer() Method in JAVA

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 will wait till a specified time for space to become available if the LinkedBlockingQueue is full. The specified waiting time and TimeUnit for time will be given as parameters to the offer() method. So it will wait till that time for LinkedBlockingQueue to remove some elements so that this method can add elements to LinkedBlockingQueue. Syntax:

public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException

Parameters: This method accepts three parameters:

  • e– the element to be inserted into LinkedBlockingQueue.
  • timeout– the time till which offer method will wait for inserting new element if queue is full.
  • unit– the Time unit for timeout parameter.

Return Value: The method returns true if insertion of element successful. Else it returns false if the specified waiting time elapses before space is available. Exception: This method throws following exceptions:

  • NullPointerException– if the specified element is null.
  • InterruptedException– if interrupted while waiting.

Below programs illustrates offer(E e, long timeout, TimeUnit unit) method of LinkedBlockingQueue class: Program 1: Create LinkedBlockingQueue by inserting the names of students using offer(E e, long timeout, TimeUnit unit) method whose timeunit parameter is given in seconds and the timeout parameter is 5sec. 

Java




// Java Program Demonstrate
// offer(Element e, long timeout, TimeUnit unit)
// method of LinkedBlockingQueue.
 
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
 
public class GFG {
 
    // Main method
    public static void main(String[] args)
        throws InterruptedException
    {
        // define capacity of LinkedBlockingQueue
        int capacityOfQueue = 4;
 
        // create object of LinkedBlockingQueue
        LinkedBlockingQueue<Integer>
            linkedQueue = new LinkedBlockingQueue<Integer>(capacityOfQueue);
 
        // Add 5 elements to ArrayBlockingQueue having
        // Timeout in seconds with value 5 secs in
        // offer(Element e, long timeout, TimeUnit unit)
        System.out.println("adding 32673821 "
                           + linkedQueue.offer(32673821,
                                               5,
                                               TimeUnit.SECONDS));
        System.out.println("adding 88527183: "
                           + linkedQueue.offer(88527183,
                                               5,
                                               TimeUnit.SECONDS));
        System.out.println("adding 431278539: "
                           + linkedQueue.offer(431278539,
                                               5,
                                               TimeUnit.SECONDS));
        System.out.println("adding 351278693: "
                           + linkedQueue.offer(351278693,
                                               5,
                                               TimeUnit.SECONDS));
        System.out.println("adding 647264: "
                           + linkedQueue.offer(647264,
                                               5,
                                               TimeUnit.SECONDS));
 
        // print the elements of queue
        System.out.println("list of numbers of queue:"
                           + linkedQueue);
 
        // now queue is full check remaining capacity of queue
        System.out.println("Empty spaces of queue : "
                           + linkedQueue.remainingCapacity());
 
        // try to add more Integer
        boolean response = linkedQueue.offer(2893476,
                                             5,
                                             TimeUnit.SECONDS);
        System.out.println("Adding new Integer 2893476 is successful: "
                           + response);
    }
}


Output:

adding 32673821 true
adding 88527183: true
adding 431278539: true
adding 351278693: true
adding 647264: false
list of numbers of queue:[32673821, 88527183, 431278539, 351278693]
Empty spaces of queue : 0
Adding new Integer 2893476 is successful: false

Program 2: Showing Exception thrown by offer(Element e, long timeout, TimeUnit unit) method 

Java




// Java Program Demonstrate
// offer(Element e, long timeout, TimeUnit unit)
// method of LinkedBlockingQueue.
 
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
 
public class GFG {
 
    public static void main(String[] args)
        throws InterruptedException
    {
        // define capacity of LinkedBlockingQueue
        int capacityOfQueue = 4;
 
        // create object of LinkedBlockingQueue
        LinkedBlockingQueue<Integer>
            linkedQueue = new LinkedBlockingQueue<Integer>(capacityOfQueue);
 
        // Add elements to ArrayBlockingQueue having
        // Timeout in seconds with value 5 secs in
        // offer(Element e, long timeout, TimeUnit unit)
        System.out.println("Adding 283239 in Queue :"
                           + linkedQueue.offer(283239,
                                               5,
                                               TimeUnit.SECONDS));
 
        // try to put null value in offer method
        try {
 
            System.out.println("Adding null in Queue: "
                               + linkedQueue.offer(null,
                                                   5,
                                                   TimeUnit.SECONDS));
        }
        catch (Exception e) {
 
            // print error details
            System.out.println("Exception: " + e);
        }
 
        // print elements of queue
        System.out.println("Items in Queue are "
                           + linkedQueue);
    }
}


Output:

Adding 283239 in Queue :true
Exception: java.lang.NullPointerException
Items in Queue are [283239]

offer(E e)

The offer(E e) method of LinkedBlockingQueue inserts the element e, passed as parameter, at the tail of this LinkedBlockingQueue, if queue has space i.e Queue is not full. If queue is full then applying offer() method shows no effect because LinkedBlockingQueue will blocks element to be inserted. offer() method returns true when the operation of addition to LinkedBlockingQueue is successful and false if this queue is full. This method is preferred over add() method because add method throws error when queue is full but offer() method returns false in such situation. Syntax:

public boolean offer(E e)

Parameters: This method takes a mandatory parameter e which is the element to be inserted into LinkedBlockingQueue. Return Value: This method returns true if insertion of element successful. Else it returns false. Exception: The method throws NullPointerException if the specified element is null. Below programs illustrates offer() method of LinkedBlockingQueue class Program 1: Create LinkedBlockingQueue by inserting the names of students using offer() method. 

Java




// Java Program Demonstrate
// offer(Element e)
// method of LinkedBlockingQueue.
 
import java.util.concurrent.LinkedBlockingQueue;
 
public class GFG {
 
    // Main method
    public static void main(String[] args)
    {
        // define capacity of LinkedBlockingQueue
        int capacityOfQueue = 4;
 
        // create object of LinkedBlockingQueue
        LinkedBlockingQueue<String>
            linkedQueue = new LinkedBlockingQueue<String>(capacityOfQueue);
 
        // Add element to LinkedBlockingQueue using offer
        linkedQueue.offer("dean");
        linkedQueue.offer("kevin");
        linkedQueue.offer("sam");
        linkedQueue.offer("jack");
 
        // print the elements of queue
        System.out.println("list of names of queue:");
        System.out.println(linkedQueue);
    }
}


Output:

list of names of queue:
[dean, kevin, sam, jack]

Program 2: Checking if LinkedBlockingQueue is full, then it inserts new element or not. 

Java




// Java Program Demonstrate
// offer(Element e)
// method of LinkedBlockingQueue.
 
import java.util.concurrent.LinkedBlockingQueue;
 
public class GFG {
 
    // Main method
    public static void main(String[] args)
    {
        // define capacity of LinkedBlockingQueue
        int capacityOfQueue = 4;
 
        // create object of LinkedBlockingQueue
        LinkedBlockingQueue<Integer>
            linkedQueue = new LinkedBlockingQueue<Integer>(capacityOfQueue);
 
        // Add element to LinkedBlockingQueue using offer
        linkedQueue.offer(34567);
        linkedQueue.offer(45678);
        linkedQueue.offer(98323);
        linkedQueue.offer(93758);
 
        // print the elements of queue
        System.out.println("list of numbers of queue:");
        System.out.println(linkedQueue);
 
        // now queue is full check remaining capacity of queue
        System.out.println("Empty spaces of queue : "
                           + linkedQueue.remainingCapacity());
 
        // try to add extra Integer
        boolean response = linkedQueue.offer(2893476);
 
        System.out.println("Adding new Integer 2893476 is successful: "
                           + response);
 
        response = linkedQueue.offer(456751);
 
        System.out.println("Adding new Integer 456751 is successful: "
                           + response);
    }
}


Output:

list of numbers of queue:
[34567, 45678, 98323, 93758]
Empty spaces of queue : 0
Adding new Integer 2893476 is successful: false
Adding new Integer 456751 is successful: false

Program 3: Showing Exception thrown by offer() method 

Java




// Java Program Demonstrate offer(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 offer() method
        linkedQueue.offer("Karan");
 
        // try to put null value in offer method
        try {
            linkedQueue.offer(null);
        }
        catch (Exception e) {
            // print error details
            System.out.println("Exception: " + e);
        }
 
        // print elements of queue
        System.out.println("Items in Queue are "
                           + linkedQueue);
    }
}


Output:

Exception: java.lang.NullPointerException
Items in Queue are [Karan]

Reference:

RELATED ARTICLES

Most Popular

Recent Comments