当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java LinkedTransferQueue transfer()用法及代码示例


LinkedTransferQueue类的transfer()方法是Java中的内置函数,通常用于将元素传输到正在等待接收的线程中,如果没有线程等待,则它将等待直到线程尽快进入等待状态等待线程到达的元素将被转移到其中。

用法:

public void transfer(E e)

参数:此方法接受强制性参数e,该参数是要传输到处于等待状态的线程的元素。


返回值:此方法不返回任何内容,因此返回类型为void。 。

异常:此方法引发以下异常:

  • NullPointerException :如果程序尝试使用具有NULL值的对象引用。
  • InterruptedException:如果在线程等待或休眠时中断。

以下程序说明了Java中的LinkedTransferQueue.transfer()方法:

示例1:为了说明Java中的LinkedTransferQueue.transfer()方法。

// Java program to illustrate 
// LinkedTransferQueue.transfer() method 
  
import java.util.*; 
import java.util.concurrent.*; 
  
class GFG { 
    public static void main(String args[]) 
    { 
  
        LinkedTransferQueue<String> g 
            = new LinkedTransferQueue<String>(); 
  
        new Thread(new Runnable() { 
  
            public void run() 
            { 
                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

示例2:来说明NullPointerException

// Java program to illustrate 
// LinkedTransferQueue.transfer() method 
  
import java.util.*; 
import java.util.concurrent.*; 
  
class GFG { 
    public static void main(String args[]) 
    { 
  
        LinkedTransferQueue<String> g 
            = new LinkedTransferQueue<String>(); 
  
        new Thread(new Runnable() { 
  
            public void run() 
            { 
                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


相关用法


注:本文由纯净天空筛选整理自suman_ptnl大神的英文原创作品 LinkedTransferQueue transfer() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。