The transfer() 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 there is no thread waiting then it will wait till a thread comes to waiting state as soon as waiting thread arrives element will be transferred into it.
Syntax:
public void transfer(E e)
Parameter: This method accepts a mandatory parameter e which is the element to be transferred to the thread which is in waiting state.
Return value: This method returns nothing, hence the return type is void. .
Exceptions: This method throws the following exceptions:
- NullPointerException: If a program attempts to use an object reference that has the NULL value.
- InterruptedException: If interrupted while a thread is waiting or sleeping .
Below programs illustrate the LinkedTransferQueue.transfer() method in Java:
Program 1: To illustrate LinkedTransferQueue.transfer() method in Java.
| // Java program to illustrate// LinkedTransferQueue.transfer() method Âimportjava.util.*;importjava.util.concurrent.*; ÂclassGFG {    publicstaticvoidmain(String args[])    { Â        LinkedTransferQueue<String> g            = newLinkedTransferQueue<String>(); Â        newThread(newRunnable() { Â            publicvoidrun()            {                try{                    System.out.println("Transferring"                                       + " an element"); Â                    // Transfer a String element                    // using transfer() method                    g.transfer("is a computer"                               + " science portal.");                    System.out.println("Element "                                       + "transfer is complete");                }                catch(InterruptedException e1) {                    System.out.println(e1);                }                catch(NullPointerException e2) {                    System.out.println(e2);                }            }        }).start(); Â        try{ Â            // Get the transferred element            System.out.println("Geeks for Geeks "                               + g.take());        }        catch(Exception e) {            System.out.println(e);        }    }} | 
Transferring an element Geeks for Geeks is a computer science portal. Element transfer is complete
Program 2: To illustrate NullPointerException
| // Java program to illustrate// LinkedTransferQueue.transfer() method Âimportjava.util.*;importjava.util.concurrent.*; ÂclassGFG {    publicstaticvoidmain(String args[])    { Â        LinkedTransferQueue<String> g            = newLinkedTransferQueue<String>(); Â        newThread(newRunnable() { Â            publicvoidrun()            {                try{                    System.out.println("Transferring"                                       + " an element"); Â                    // Transfer a null element                    // using transfer() method                    g.transfer(null); Â                    System.out.println("Element "                                       + "transfer is complete");                }                catch(Exception e) {                    System.out.println(e);                    System.exit(0);                }            }        }).start(); Â        try{ Â            // Get the transferred element            System.out.println("Geeks for Geeks "                               + g.take());        }        catch(Exception e) {            System.out.println(e);        }    }} | 
Transferring an element java.lang.NullPointerException

 
                                    







