Java中的LinkedBlockingDeque offerLast()方法

这个 报价人(E) 方法 LinkedBlockingDeque 在Deque容器的末尾插入传入参数的元素。如果容器的容量已超过,那么它不会像add()和addLast()函数那样返回异常。

null

语法:

public boolean offerLast(E e)

参数: 此方法接受一个强制参数 E 这是要插入到LinkedBlockingDeque末尾的元素。

返回: 此方法返回 符合事实的 如果元素已插入,则返回 错误的

下面的程序演示了LinkedBlockingDeque的offerLast()方法:

项目1:

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


输出:

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

项目2:

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


输出:

The element 'hii' cannot be inserted as capacity is full
Linked Blocking Deque: [abc, gopu, geeks, richik]

参考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingDeque.html#offerLast(E)

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享