當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。