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

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

Java中的 LinkedTransferQueue tryTransfer() 方法及示例

LinkedTransferQueue 类的tryTransfer()方法是Java中的一个内置函数,通常用于将元素传输到等待接收它的线程,如果没有线程等待则它将终止而不将元素添加到队列中或者您也可以通过将时间量及其单位作为参数传递给函数来使其等待一定时间。
在 LinkedTransferQueue 类中,根据传递给它的参数,有两种类型的 tryTransfer() 方法。

尝试转移(E e)

一般用于将元素传递给处于等待状态的线程,如果没有线程等待则终止而不将元素添加到队列中
句法:

public boolean tryTransfer(E e)

范围:

  • 这里E e是要转移到处于等待状态的线程的元素。

返回值:此方法将返回一个布尔值,即如果元素被转移则为真,否则为假。
异常:此方法将抛出以下异常。

  • NullPointerException – 如果指定元素为 null

  • InterruptedException – 如果在等待时被中断,在这种情况下,元素不会留在队列中。

下面的程序说明了Java中的简单 LinkedTransferQueue.tryTransfer() 方法:
程序 1:说明Java中的 tryTransfer() 方法。

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


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


Java
// 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 g
            = new LinkedTransferQueue();
 
        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 transferred ");
                }
                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);
        }
    }
}


Java
// 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 g
            = new LinkedTransferQueue();
 
        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
element transfer is complete
Geeks for Geeks is a computer science portal.

程序 2: NullPointerException。

Java

// 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 g
            = new LinkedTransferQueue();
 
        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, 长时间超时, TimeUnit)

一般用于将元素传递给处于等待状态的线程,如果没有线程在等待,则通过将时间量及其单位作为参数传递给函数来等待一定的时间。
句法:

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

参数:此方法接受三个强制参数:

  • 这里E e是要转移到处于等待状态的线程的元素。
  • 这里的 long timeout是指定在终止之前应该等待的时间量的时间。
  • 这里TimeUnit 单位是指长时间超时的单位。

返回值:此方法将返回一个布尔值。如果元素被转移,则返回 true,否则返回 false。
异常:此方法将抛出以下异常。

  • NullPointerException – 如果指定元素为 null

  • InterruptedException – 如果在等待时被中断,在这种情况下,元素不会留在队列中。

程序1:通过设置wait作为参数来说明Java中的tryTransfer()方法。

Java

// 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 g
            = new LinkedTransferQueue();
 
        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 transferred ");
                }
                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

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

参考:

  • https://docs.oracle.com/javase/7/docs/api/java Java)
  • https://docs.oracle.com/javase/7/docs/api/java Java, %20long, %20java.util.concurrent.TimeUnit)