📌  相关文章
📜  Java中的 LinkedTransferQueue transfer() 方法及示例

📅  最后修改于: 2022-05-13 01:54:22.390000             🧑  作者: Mango

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 g
            = new LinkedTransferQueue();
  
        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 g
            = new LinkedTransferQueue();
  
        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