HomeLanguagesJavaLinkedTransferQueue take() method in Java

LinkedTransferQueue take() method in Java

The java.util.concurrent.LinkedTransferQueue.take() method is an in-built function in Java which retrieves and remove the first element of the queue. This method also waits (if necessary) until an element becomes available.
Syntax:

LinkedTransferQueue.take()  

Parameters: The function does not accept any parameter.

Return Value: The function returns the first element of the queue.

Exceptions: The function throws InterruptedException if interrupted while waiting.

Below programs illustrate the java.util.concurrent.LinkedTransferQueue.take() :

Program 1:




// Java Program Demonstrate take()
// method of LinkedTransferQueue 
  
import java.util.concurrent.LinkedTransferQueue;
  
class LinkedTransferQueueTakeExample1 {
    public static void main(String[] args) throws Exception
    {
        // Initializing the queue
        LinkedTransferQueue<String> queue = 
                        new LinkedTransferQueue<String>();
  
        // Adding elements to this queue
        queue.add("Alex");
        queue.add("Bob");
        queue.add("Chuck");
        queue.add("Drake");
        queue.add("Eric");
  
        // Printing the elements
        System.out.println("Elements are :");
        for (String xyz : queue) {
            // will take and remove the head of the queue
            System.out.println(queue.take());
        }
  
        // Printing the size of the Queue
        System.out.println("Queue Size: " + queue.size());
    }
}


Output:

Elements are :
Alex
Bob
Chuck
Drake
Eric
Queue Size: 0

Program 2:




// Java Program Demonstrate take()
// method of LinkedTransferQueue 
  
import java.util.concurrent.LinkedTransferQueue;
  
class LinkedTransferQueueTakeExample2 {
    public static void main(String[] args) throws Exception
    {
        // Initializing the queue
        LinkedTransferQueue<Integer> queue = 
                        new LinkedTransferQueue<Integer>();
  
        // Adding elements to this queue
        for (int i = 1; i <= 5; i++)
            queue.add(i);
  
        // Printing the elements
        System.out.println("Elements are :");
        for (Integer xyz : queue) {
            // will take and remove the head of the queue
            System.out.println(queue.take());
        }
        // Printing the size of the Queue
        System.out.println("Queue Size: " + queue.size());
    }
}


Output:

Elements are :
1
2
3
4
5
Queue Size: 0

Reference : https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedTransferQueue.html#take()

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

4 COMMENTS

Most Popular

Dominic
32548 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6922 POSTS0 COMMENTS
Nicole Veronica
12037 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12143 POSTS0 COMMENTS
Shaida Kate Naidoo
7057 POSTS0 COMMENTS
Ted Musemwa
7298 POSTS0 COMMENTS
Thapelo Manthata
7015 POSTS0 COMMENTS
Umr Jansen
7001 POSTS0 COMMENTS