Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous Programming and Reactive Programming

This question is in my mind about a year. Actually are there any differences in Asysnchronus and Non-blocking. When we call the blocking part in our code then it becomes blocking which is synchronous and at the same time it will not be non-blocking.

If we are creating another thread apart from main thread to make asynchronous programming and we have to return some value so we have to define join() method in java and join() is blocking operation then is it actually asynchronous?

I need to know answer for the following questions

  1. If blocking is similar to synchronous then what is the different between asynchronous and non blocking. Should it be similar ? if not then why?

  2. Reactive Programming which is non blocking does it fully asynchronous programming?

like image 307
NafazBenzema Avatar asked Mar 03 '23 01:03

NafazBenzema


1 Answers

Consider 2 parallel algorithms, producer and consumer. If consumer works faster than producer, we have to block consumer algorithm until producer provide new data. Generally, we have 2 ways to block consumer:

  1. implement consumer as a thread, and block that thread
  2. implement consumer as a task, running on a thread pool, and make return from that task (and tell producer to restart the task when the data is ready).

The first method of implementing consumer is synchronous, and the second asynchronous.

Now consider an opposite case: producer is faster than consumer. Then again, we have two options to block producer:

  1. implement producer as a thread and block that thread
  2. implement producer as a task, running on a thread pool, and make return from that task (and tell consumer to restart the producer task when it is able to receive data).

Naturally, the first option is synchronous and the second asynchronous. And the second, asynchronous option to define interaction between fast producer and slow consumer is called reactive programming.

So, reactive programming is a subset of asynchronous programming. There are many different protocols to define interaction between asynchronous activities, and reactive programming is only one of them, which is unable to cover all possible cases of asynchronous communications.

I tried to collect asynchronous protocols in the module https://github.com/akaigoro/df4j/tree/API-8/df4j-protocols. Other protocols can be (re)invented, for example, byte streams with or without backpressure, analogue to synchronous InputStream and OutputStream. I am sure any synchronous protocol has its asynchronous analogue.

like image 51
Alexei Kaigorodov Avatar answered Mar 04 '23 14:03

Alexei Kaigorodov