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

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

Java中的 BlockingDeque offerFirst() 方法及示例

BlockingDequeofferFirst(E e)方法将参数中传入的元素插入到 Deque 容器的最前面。如果容器的容量已超出,则它不会像 add() 和 addFirst()函数那样返回异常。

句法:

public boolean offerFirst(E e)

参数:此方法接受一个强制参数e ,它是要插入到 BlockingDeque 前面的元素。

返回:如果元素已被插入,此方法返回true ,否则返回false。

注意BlockingDeque的 offerFirst() 方法继承自Java中的 LinkedBlockingDeque 类。

下面的程序说明了 LinkedBlockingDeque 的 offerFirst() 方法:

方案一:

Java
// Java Program Demonstrate offerFirst()
// method of BlockingDeque
 
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
 
        // create object of BlockingDeque
        BlockingDeque BD
            = new LinkedBlockingDeque(4);
 
        // Add numbers to end of BlockingDeque
        BD.offerFirst(7855642);
        BD.offerFirst(35658786);
        BD.offerFirst(5278367);
        BD.offerFirst(74381793);
 
        // Cannot be inserted
        BD.offerFirst(10);
 
        // cannot be inserted hence returns false
        if (!BD.offerFirst(10))
            System.out.println("The element 10 cannot be inserted"
                               + " as capacity is full");
 
        // before removing print queue
        System.out.println("Blocking Deque: " + BD);
    }
}


Java
// Java Program Demonstrate offerFirst()
// method of BlockingDeque
 
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
 
        // create object of BlockingDeque
        BlockingDeque BD
            = new LinkedBlockingDeque(4);
 
        // Add numbers to end of BlockingDeque
        BD.offerFirst("abc");
        BD.offerFirst("gopu");
        BD.offerFirst("geeks");
        BD.offerFirst("richik");
 
        // Cannot be inserted
        BD.offerFirst("hii");
 
        // cannot be inserted hence returns false
        if (!BD.offerFirst("hii"))
            System.out.println("The element 'hii' cannot be"
                               + " inserted as capacity is full");
 
        // before removing print queue
        System.out.println("Blocking Deque: " + BD);
    }
}


输出:
The element 10 cannot be inserted as capacity is full
Blocking Deque: [74381793, 5278367, 35658786, 7855642]

方案二:

Java

// Java Program Demonstrate offerFirst()
// method of BlockingDeque
 
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
 
        // create object of BlockingDeque
        BlockingDeque BD
            = new LinkedBlockingDeque(4);
 
        // Add numbers to end of BlockingDeque
        BD.offerFirst("abc");
        BD.offerFirst("gopu");
        BD.offerFirst("geeks");
        BD.offerFirst("richik");
 
        // Cannot be inserted
        BD.offerFirst("hii");
 
        // cannot be inserted hence returns false
        if (!BD.offerFirst("hii"))
            System.out.println("The element 'hii' cannot be"
                               + " inserted as capacity is full");
 
        // before removing print queue
        System.out.println("Blocking Deque: " + BD);
    }
}
输出:
The element 'hii' cannot be inserted as capacity is full
Blocking Deque: [richik, geeks, gopu, abc]

参考: https: Java/util/concurrent/BlockingDeque.html#offerFirst(E)