Couchbase Java Client Sync Vs Async blocking Vs Async Non-Blocking

@dmartyanov examples and snippets should usually be easily runnable. When you do such a snippet eg. in a main method, if you don’t block somehow then the asynchronous call will happen in a background thread and method execution will continue immediately… in effect terminating the program before anything could happen.

So it is not very interesting! From here you have two choices: either you sleep() or you block on your async call (toBlocking()). Sleeping is even less elegant, as it will potentially force the program to run for more time that it really needs (eg. the async code executes in 1ms but you sleep for 1000ms).

Hence the use of toBlocking() in various examples: if you copy paste that in a main() method and run it, something happens :slight_smile:

But yeah, to rip the most benefits from the asynchronous API, for a “normal” long-running application, you should avoid use of toBlocking() and try to make as much of the application code reactive as possible.