Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaLinkedTransferQueue tryTransfer() method in Java with Examples

LinkedTransferQueue tryTransfer() method in Java with Examples

The tryTransfer() method of class LinkedTransferQueue is an inbuilt function in Java which is generally used to transfer an element to a thread which is waiting to receive it, if in case there is no thread waiting then it will terminate without adding element into the queue or you can also make it wait for certain amount time by passing amount of time and its units as parameter to the function.
In LinkedTransferQueue class, there are two types of tryTransfer() methods depending upon the parameters passed to it.
 

tryTransfer(E e)

Generally used to transfer an element to a thread which is in waiting state, if in case there is no thread waiting then it will terminate without adding the element into the queue 
Syntax: 
 

public boolean tryTransfer(E e)

Parameter: 
 

  • Here E e is the element which is to be transferred to the thread which is in waiting state.

Return Value:This method will return a boolean value, i.e true if the element was transferred, else false.
Exception: This method will throw the following exceptions. 
 

  • NullPointerException – if the specified element is null

 

  • InterruptedException – if interrupted while waiting, in which case the element is not left enqueued.

Below programs illustrate the simple LinkedTransferQueue.tryTransfer() method in Java:
Program 1: To illustrate tryTransfer() method in Java. 
 

Java




// Java program to demonstrate
// the tryTransfer() Method.
 
import java.util.*;
import java.util.concurrent.*;
 
class GFG {
    public static void main(String args[])
    {
 
        // creating an object
        // for class LinkedTransferQueue
        LinkedTransferQueue<String> g
            = new LinkedTransferQueue<String>();
 
        new Thread(new Runnable() {
 
            public void run()
            {
 
                try {
 
                    System.out.println("transferring an element");
 
                    // calling tryTransfer() method
                    // to transfer the string
                    g.tryTransfer("is a computer science portal.");
 
                    System.out.println("element transfer is complete");
                }
 
                catch (NullPointerException e2) {
 
                    System.out.println(e2);
                    System.exit(0);
                }
            }
        }).start();
 
        try {
 
            // here the thread is waiting
            // to receive an element.
            System.out.println("Geeks for Geeks "
                               + g.take());
        }
 
        catch (Exception e) {
            System.out.println(e);
        }
    }
}


Output: 

transferring an element
element transfer is complete
Geeks for Geeks is a computer science portal.

 

Program 2: NullPointerException. 
 

Java




// Java program to demonstrate NullPointerException
// thrown by the tryTransfer() Method.
 
import java.util.*;
import java.util.concurrent.*;
 
class GFG {
    public static void main(String args[])
    {
 
        // create a LinkedTransferQueue object
        LinkedTransferQueue<String> g
            = new LinkedTransferQueue<String>();
 
        new Thread(new Runnable() {
 
            public void run()
            {
 
                try {
                    System.out.println("transferring an element");
 
                    // calling tryTransfer() method
                    // to transfer the null element
                    g.tryTransfer(null);
 
                    System.out.println("element transfer is complete");
                }
 
                catch (NullPointerException e2) {
 
                    System.out.println(e2);
                    System.exit(0);
                }
            }
        }).start();
 
        try {
 
            System.out.println("Geeks for Geeks " + g.take());
 
            // here the thread is waiting
            // to receive an element.
        }
        catch (Exception e) {
 
            System.out.println(e);
            System.exit(0);
        }
    }
}


Output: 

transferring an element
java.lang.NullPointerException

 

tryTransfer(E e, long timeout, TimeUnit)

Generally used to transfer an element to a thread which is in the waiting state, if in case there is no thread waiting then it wait for certain amount time by passing amount of time and its units as the parameter to the function.
Syntax: 
 

public boolean tryTransfer(E e, long timeout, TimeUnit)

Parameter: This method accepts three mandatory parameters: 
 

  • Here E e is the element which is to be transferred to the thread which is in waiting state.
  • Here long timeout is the time that specifies the amount of time it should wait before terminating.
  • Here TimeUnit unit mean the unit of long timeout.

Return Value:This method will return a boolean value. It returns true if the element was transferred, else false.
Exception: This method will throw the following exceptions. 
 

  • NullPointerException – if the specified element is null

 

  • InterruptedException – if interrupted while waiting, in which case the element is not left enqueued.

Program 1: To illustrate tryTransfer() method in Java by setting waiting as its parameter.
 

Java




// Java program to demonstrate
// the tryTransfer() Method.
 
import java.util.*;
import java.util.concurrent.*;
 
class GFG {
    public static void main(String args[])
    {
 
        // creating an object for LinkedTransferQueue
        LinkedTransferQueue<String> g
            = new LinkedTransferQueue<String>();
 
        new Thread(new Runnable() {
 
            public void run()
            {
                try {
 
                    System.out.println("transferring an element");
 
                    // calling tryTransfer() method passing amount
                    // of time and its units as the parameter
                    // to the function and storing
                    // its return value in a boolean variable.
                    boolean a = g.tryTransfer(
                        "is a computer science portal.",
                        2000,
                        TimeUnit.MILLISECONDS);
 
                    if (a)
                        System.out.println("element transfer is complete");
                    else
                        System.out.println("element is not transferred ");
                }
                catch (NullPointerException e2) {
                    System.out.println(e2);
                }
                catch (InterruptedException e3) {
                    System.out.println(e3);
                }
            }
        }).start();
 
        try {
 
            // here thread is made inactive or sleep
            // for 2000 milliseconds
            Thread.sleep(2000);
 
            // here the thread is ready to receive
            System.out.println("Geeks for Geeks " + g.take());
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}


Output: 

transferring an element
Geeks for Geeks is a computer science portal.
element transfer is complete

 

Program 2: NullPointerException. 
 

Java




// Java program to demonstrate NullPointerException
// thrown by the tryTransfer() Method.
 
import java.util.*;
import java.util.concurrent.*;
 
class GFG {
    public static void main(String args[])
    {
        // creating an object
        // for class LinkedTransferQueue
        LinkedTransferQueue<String> g
            = new LinkedTransferQueue<String>();
 
        new Thread(new Runnable() {
 
            public void run()
            {
                try {
                    System.out.println("transferring an element");
 
                    // calling tryTransfer() method
                    // to transfer the null element
                    g.tryTransfer(null, 2000, TimeUnit.MILLISECONDS);
 
                    System.out.println("element transfer is complete");
                }
                catch (NullPointerException e2) {
                    System.out.println(e2);
                    System.exit(0);
                }
                catch (InterruptedException e3) {
                    System.out.println(e3);
                }
            }
        }).start();
 
        try {
 
            // here thread is made inactive or sleep
            // for 2000 milliseconds
            Thread.sleep(2000);
 
            // here the thread is ready to receive
            System.out.println("Geeks for Geeks "
                               + g.take());
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}


Output: 

transferring an element
java.lang.NullPointerException

 

References: 
 

RELATED ARTICLES

Most Popular

Recent Comments