CodePudding user response:
Use SynchronousQueue will: thread A put add data to the queue, if there are no other threads to take to get the data, then thread A block, until the other threads access to data, in the same way if thread B to get the data from the queue is empty, is blocked, waiting for the thread to add dataUse put LinkedTransferQueue: LinkedTransferQueue, tryTransfer and transfer multiple items can be added, LinkedTransferQueue has the function of SynchronousQueue will but LinkedTransferQueue producers won't block
TryTransfer and transfer with put, tryTransfer and transfer can detect whether there is a thread waiting to get the data, data directly detected awakens the waiting thread to this thread and not in the queue