Last Updated : 26 Nov, 2018
offer(E e, long timeout, TimeUnit unit)The
offer(E e, long timeout, TimeUnit unit)method of
java.util.concurrent.LinkedTransferQueueClass is an in-built function in Java which inserts the element passed as parameter to method at the tail of this queue, if queue is not full.
public boolean offer(E e, long timeout, TimeUnit unit)Parameters:
The function accepts the following parameters:
The method returns a boolean
true. As the queue is unbounded, this method will never block or return false.
Exception:The function shows
NullPointerExceptionwhen the specified element is Null. Below programs illustrate the use of java.util.concurrent.LinkedTransferQueue.offer() :
Program 1:To create LinkedTransferQueue 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()
// method of LinkedTransferQueue
import java.util.concurrent.*;
class GFG {
public static void main(String[] args)
throws InterruptedException
{
// create object of LinkedTransferQueue
LinkedTransferQueue<Integer>
queue = new LinkedTransferQueue<Integer>();
// 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 15 "
+ queue.offer(15, 5, TimeUnit.SECONDS));
System.out.println("adding 25: "
+ queue.offer(25, 5, TimeUnit.SECONDS));
System.out.println("adding 35: "
+ queue.offer(35, 5, TimeUnit.SECONDS));
System.out.println("adding 45: "
+ queue.offer(45, 5, TimeUnit.SECONDS));
System.out.println("adding 55: "
+ queue.offer(55, 5, TimeUnit.SECONDS));
// print the elements of queue
System.out.println("list of numbers of queue: "
+ queue);
}
}
Output:
adding 15 true adding 25: true adding 35: true adding 45: true adding 55: true list of numbers of queue: [15, 25, 35, 45, 55]Program 2:
Program to show NullPointerException.
Java
// Java Program Demonstrate offer()
// method of LinkedTransferQueue
import java.util.concurrent.*;
class GFG {
public static void main(String[] args)
throws InterruptedException
{
// Initializing the queue
LinkedTransferQueue<Integer>
queue = new LinkedTransferQueue<Integer>();
// add elements to queue
try {
queue.offer(null, 5, TimeUnit.SECONDS);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Output:
Exception: java.lang.NullPointerExceptionoffer(E e)
The
offer(E e)method of
java.util.concurrent.LinkedTransferQueueClass is an in-built function in Java which inserts the element e passed as parameter to method at the tail of this LinkedTransferQueue if queue has space i.e Queue is not full. If queue is full then applying offer() method shows no effect because LinkedTransferQueue will blocks element to be inserted. offer() method returns true when the operation of addition to LinkedTransferQueue 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:
The function accepts a single parameter
ei.e. the element to be inserted.
Return Value:The method returns
trueupon successful insertion.
Exception:The function shows
NullPointerExceptionwhen the specified element is Null.
Program 1:Adding String in the queue.
Java
// Java Program Demonstrate offer()
// method of LinkedTransferQueue
import java.util.concurrent.*;
class GFG {
public static void main(String[] args)
{
// Initializing the queue
LinkedTransferQueue<String>
queue = new LinkedTransferQueue<String>();
// Adding elements to this queue
queue.offer("alex");
queue.offer("bob");
queue.offer("chuck");
queue.offer("drake");
queue.offer("eric");
// Printing the elements of the queue
System.out.print("Queue: ");
for (String i : queue)
System.out.print(i + " ");
}
}
Output:
Queue: alex bob chuck drake ericProgram 2:
Program to show NullPointerException.
Java
// Java Program Demonstrate offer()
// method of LinkedTransferQueue
import java.util.concurrent.LinkedTransferQueue;
class GFG {
public static void main(String[] args)
throws InterruptedException
{
// Initializing the queue
LinkedTransferQueue<Integer>
queue = new LinkedTransferQueue<Integer>();
// add elements to queue
queue.offer(10);
queue.offer(20);
queue.offer(30);
try {
queue.offer(null);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Output:
Exception: java.lang.NullPointerExceptionReference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedTransferQueue.html#offer(E) https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedTransferQueue.html#offer(E, %20long, %20java.util.concurrent.TimeUnit)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4