Synchronous queues are similar to rendezvous channels used in CSP and Ada. They are well suited for handoff designs, in which an object running in one thread must sync up with an object running in another thread in order to hand it some information, event, or task. –From Java offical document here
From my perspective, it’s useful when passing data from one thread to another and there is a need to make the consumer thread wait until the data made by the producer thread is ready.
true Thread 2 is taking: hello 1 true Thread 1 is taking: hello 2
Process finished with exit code 0
Note the order of the output is NOT the same as the order of the threads starting. If you want the same output order as the threads starting, change the “fair” argument of the constructor method of SynchronousQueue class, as below: