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


Java LinkedTransferQueue tryTransfer()用法及代碼示例


LinkedTransferQueue類的tryTransfer()方法是Java中的內置函數,通常用於將元素傳輸到等待接收該元素的線程中,如果沒有線程等待,則它將終止而無需將元素添加到隊列中還可以通過將時間量及其單位作為參數傳遞給函數來使其等待一定量的時間。

在LinkedTransferQueue類中,根據傳遞給它的參數,有兩種類型的tryTransfer()方法。

tryTransfer(E e)


通常用於將元素轉移到處於等待狀態的線程中,如果沒有線程在等待,則它將終止而無需將元素添加到隊列中

用法:

public boolean tryTransfer(E e)

參數:

  • E e是要轉移到處於等待狀態的線程的元素。

返回值:此方法將返回布爾值,即,如果元素已傳輸,則為true,否則為false。

異常:此方法將引發以下異常。

  • NullPointerException -如果指定的元素為null
  • InterruptedException-如果在等待期間被打斷,則在這種情況下該元素不會被排隊。

以下程序說明了Java中的簡單LinkedTransferQueue.tryTransfer()方法:

程序1:為了說明Java中的tryTransfer()方法。

// 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); 
        } 
    } 
}
輸出:
transferring an element
element transfer is complete
Geeks for Geeks is a computer science portal.

程序2: NullPointerException 。

// 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); 
        } 
    } 
}
輸出:
transferring an element
java.lang.NullPointerException

tryTransfer(E e, long timeout, TimeUnit)

通常用於將元素傳輸到處於等待狀態的線程,如果在沒有線程等待的情況下,則它通過將時間量及其單位作為參數傳遞給函數來等待一定量的時間。

用法:

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

參數:此方法接受三個強製參數:

  • E e是要轉移到處於等待狀態的線程的元素。
  • long timeout是指定終止之前應等待的時間的時間。
  • TimeUnit unit表示長時間超時的單位。

返回值:此方法將返回布爾值。如果已傳輸元素,則返回true,否則返回false。

異常:此方法將引發以下異常。

  • NullPointerException -如果指定的元素為null
  • InterruptedException-如果在等待期間被打斷,則在這種情況下該元素不會被排隊。

程序1:通過將wait設置為其參數來說明Java中的tryTransfer()方法。

// 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 transfered "); 
                } 
                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); 
        } 
    } 
}
輸出:
transferring an element
Geeks for Geeks is a computer science portal.
element transfer is complete

程序2: NullPointerException 。

// 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); 
        } 
    } 
}
輸出:
transferring an element
java.lang.NullPointerException

參考文獻:

相關用法


注:本文由純淨天空篩選整理自suman_ptnl大神的英文原創作品 LinkedTransferQueue tryTransfer() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。