java: LinkedBlockingQueue之drainTo

amadeus_liu2 2022-08-15 18:04:52 阅读数:421

javalinkedblockingqueuedrainto

This method is used to transfer elements in the LinkedBlockingQueue to a new Collection object

package com.example.app;import java.util.LinkedList;import java.util.List;import java.util.concurrent.LinkedBlockingDeque;public class TestDrain {public static void main(String[] args) {LinkedBlockingDeque queue=new LinkedBlockingDeque<>();queue.add("aaa");queue.add("bcd");queue.add("fgh");List stringList=new LinkedList<>();queue.drainTo(stringList);System.out.println(stringList);System.out.println(queue.size());}}

ArrayBlockingQueue is also similar

copyright:author[amadeus_liu2],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/227/202208151743338853.html