异步编程与响应式编程

huangapple go评论63阅读模式
英文:

Asynchronous Programming and Reactive Programming

问题

这个问题在我脑海中已经有大约一年的时间了。实际上,异步和非阻塞之间是否有任何区别呢?当我们在代码中调用阻塞部分时,它变成了同步的,与此同时它将不是非阻塞的。

如果我们除了主线程之外再创建一个线程来进行异步编程,并且我们必须返回一些值,那么我们就必须在Java中使用join()方法,而join()是一个阻塞操作,那么它真的是异步的吗?

我需要了解以下问题的答案:

  1. 如果阻塞与同步类似,那么异步和非阻塞之间有什么区别?它们应该是类似的吗?如果不是,那么为什么?

  2. 非阻塞的反应式编程是否就是完全的异步编程?

英文:

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?

答案1

得分: 14

考虑两个并行算法,生产者和消费者。如果消费者的工作速度快于生产者,我们必须阻塞消费者算法,直到生产者提供新数据。一般来说,我们有两种方式来阻塞消费者:

  1. 将消费者实现为一个线程并阻塞该线程。
  2. 将消费者实现为在线程池上运行的任务,并从该任务返回(并告诉生产者在数据准备好时重新启动任务)。
    将消费者实现为线程的方法是同步的,而第二种方法是异步的。

现在考虑相反的情况:生产者比消费者更快。然后,我们再次有两种选择来阻塞生产者:

  1. 将生产者实现为一个线程并阻塞该线程。
  2. 将生产者实现为在线程池上运行的任务,并从该任务返回(并告诉消费者在能够接收数据时重新启动生产者任务)。
    自然地,第一个选项是同步的,第二个选项是异步的。而描述快速生产者和慢速消费者之间交互的第二个异步选项被称为响应式编程。

因此,响应式编程是异步编程的一个子集。有许多不同的协议来定义异步活动之间的交互方式,而响应式编程只是其中的一种,它不能涵盖所有可能的异步通信情况。

我尝试在此模块中收集了异步协议:https://github.com/akaigoro/df4j/tree/API-8/df4j-protocols。其他协议可以被重新发明,例如,带有或不带有背压的字节流,类似于同步的InputStreamOutputStream。我确信任何同步协议都有其异步对应物。

英文:

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

  1. Implement the consumer as a thread and block that thread.
  2. Implement the consumer as a task running on a thread pool and return from that task (and tell the producer to restart the task when the data is ready).
    The first method of implementing the consumer is synchronous, and the second is asynchronous.

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

  1. Implement the producer as a thread and block that thread.
  2. Implement the producer as a task running on a thread pool and return from that task (and tell the consumer to restart the producer task when it is able to receive data).
    Naturally, the first option is synchronous and the second is asynchronous. And the second, asynchronous option to define the interaction between a fast producer and a slow consumer is called reactive programming.

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

I tried to collect asynchronous protocols in this 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, analogous to synchronous InputStream and OutputStream. I am sure any synchronous protocol has its asynchronous counterpart.

huangapple
  • 本文由 发表于 2020年9月24日 02:13:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64033955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定