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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。