Java collections TransferQueue

Thread-safe

transfer (T t) content delivery directly to a consumer, if the consumer will not clog

public static void main(String[] args) throws InterruptedException {
        TransferQueue<String>  tq = new LinkedTransferQueue<>();

        tq.transfer("test");
        
        new Thread(()->{
            try {
                Thread.sleep(500);
                System.out.println(tq.poll());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }

The above code will be blocked.

tryTransfer (T t, long long, TimeUnit unit) to try a certain time, if not consumers, returns false.

 

Published 16 original articles · won praise 3 · Views 4527

Guess you like

Origin blog.csdn.net/qq_29697901/article/details/90406399