actor akka

by Martin Budi

马丁·布迪(Martin Budi)

如果您仍在使用同步,则应改用Akka Actor-这就是为什么 (If you’re still using Synchronized, you should try Akka Actor instead — here’s why)

Synchronized is Java’s traditional concurrency mechanism. Although it is probably not something we see often these days, it is still fueling many libraries. The problem is, synchronized is both blocking and complicated. In this article, I’d like to illustrate the issue in a simple way and my reasoning to move to Akka Actor for better and easier concurrency.

同步是Java的传统并发机制。 尽管这些天我们可能很少见到它,但它仍在推动着许多图书馆的发展。 问题是,同步既阻塞又复杂。 在本文中,我想以一种简单的方式来说明这个问题,并说明我选择Akka Actor获得更好和更轻松的并发性的理由。

Consider this simple code:

考虑以下简单代码:

int x; if (x > 0) {return true;} else {return false;}

So return true if x is positive. Simple.

因此,如果x为正,则返回true。 简单。

Next, consider this even simpler code:

接下来,考虑以下更简单的代码:

x++;

Yes, a counter. Very simple right?

是的,柜台。 很简单吧?

However, all these codes can blow up spectacularly in a multi-threaded environment.

但是,所有这些代码都可能在多线程环境中爆炸。

In the first example, true or false isn’t determined by the value of x. It is actually determined by the if-test. So, if another thread changes x to a negative right after the first thread passed the if-test, we’d still get true even if x is no longer positive.

在第一个示例中,true或false不是由x的值确定的。 它实际上由if-test确定。 因此,如果在第一个线程通过了if-test之后另一个线程将x更改为负数,即使x不再为正数,我们仍将为true。

The second example is pretty deceptive. Although it is just one line, there are actually three operations: reading x, incrementing it and putting the updated value back. If two threads run at exactly the same time, the update might be lost.

第二个例子具有欺骗性。 尽管只是一行,但实际上有三个操作:读取x ,将其递增和将更新后的值放回去。 如果两个线程恰好同时运行,则更新可能会丢失。

When we have different threads simultaneously accessing and modifying a variable, we have a race condition. If we just want to build a counter, Java provides thread-safe Atomic Variables, among them Atomic Integer which we can use for this purpose. Atomic Integer, however, only works on single variables. How do we make several operations atomic?

当我们有不同的线程同时访问和修改变量时,我们就有了竞争条件。 如果我们只想构建一个计数器,则Java提供了线程安全的Atomic变量,其中包括Atomic Integer,我们可以将其用于此目的。 但是,原子整数仅适用于单个变量。 我们如何使几个操作原子化?

By using synchronized block. First, let’s take a look at a more elaborate example.

通过使用同步块。 首先,让我们看一个更详细的例子。

int x;
public int withdraw(int deduct){int balance = x - deduct; if (balance > 0) {x = balance;return deduct;} else {return 0;}
}

This is a very basic cash withdraw method. It also happens to be dangerous. Two threads running at the same time may cause the bank to issue two withdrawals even if the balance is no longer enough. Now let’s see how it works with synchronized block:

这是一种非常基本的现金提取方法。 这也很危险。 即使余额不足,两个线程同时运行也可能导致银行发出两次提款。 现在让我们看看它如何与同步块一起工作:

volatile int x;
public int withdraw(int deduct){synchronized(this){int balance = x - deduct; if (balance > 0) {x = balance;return deduct;} else {return 0;}}
}

The idea of synchronized block is simple. One thread enters it and locks it, while other threads wait outside. The lock is an object, in our case this. After it’s done, the lock is released and passed to another thread which then does the same thing. Also, note the esoteric keyword volatile which is needed to prevent the thread from using the local CPU cache of x.

同步块的想法很简单。 一个线程进入并锁定它,而其他线程在外面等待。 锁是一个对象,在我们的例子中是this 。 完成后,将释放该锁并将其传递给另一个线程,然后该线程将执行相同的操作。 另外,请注意需要使用深奥的关键字volatile ,以防止线程使用x的本地CPU缓存

Now with the threads untangled, the bank won’t accidentally issue unfunded withdrawals. However, this structure tends to grow complex with more blocks and more locks. Dealing with multiple locks is particularly risky. The blocks might inadvertently hold the key for each other and end up locking the entire app. And on top of it, we have an efficiency issue. Remember that while a thread works inside, all the other threads wait outside. And waiting threads are well … waiting. They don’t do anything else but wait.

现在,随着线程的混乱,银行将不会意外地发行无资金的提款。 但是,这种结构往往随着更多的块和更多的锁而变得复杂。 处理多个锁特别危险。 这些块可能会在不经意间互相持有密钥并最终锁定整个应用程序。 最重要的是,我们存在效率问题。 记住,当一个线程在内部运行时,所有其他线程在外部等待。 等待线程很好……等待。 除了等待,他们别无选择。

So instead of doing such mechanism, why not just drop the job in a queue? To better visualize it, imagine an email system. When you send an email, you drop the email in the recipient’s mailbox. You don’t wait around until the person reads it.

因此,为什么不将工作放在队列中而不是采用这种机制呢? 为了更好地可视化它,请想象一个电子邮件系统。 发送电子邮件时,会将电子邮件拖放到收件人的邮箱中。 您无需等到该人读完它。

These are the basics of the Actor model and Akka framework in general.

这些通常是Actor模型和Akka框架的基础。

Actor encapsulates state and behavior. Unlike OOP’s encapsulation, though, actors do not expose their state and behavior at all. The only way for an actor to communicate with each other is by exchanging messages. Incoming messages are dropped in a mailbox and digested in first-in-first-out order. Here’s a reworked sample in Akka and Scala.

Actor封装状态和行为。 但是,与OOP的封装不同,actor根本不公开其状态和行为。 演员相互交流的唯一方法是交换消息。 传入邮件将被放入邮箱中,并按照先进先出的顺序进行摘要。 这是Akka和Scala中经过重做的示例。

case class Withdraw(deduct: Int)
class ReplicaActor extends Actor {var x = 10;def receive: Receive = {case Withdraw(deduct) => val r = withdraw(deduct)}
}
class BossActor extends Actor {var replica = context.actorOf(Props[ReplicaActor])replica ! Withdraw(6)replica ! Withdraw(9)
}

We have a ReplicaActor that does the work and BossActor that orders the replica around. First, notice the ! sign or tell. This is one of two methods (the other is ask) for an actor to asynchronously send a message to another actor. tell in particular does so without waiting for a reply. So the boss tells the replica to do two withdraw orders and immediately leaves. These messages arrive in the replica's receive where each one is popped and matched with the corresponding handler. In this case, Withdraw executes the withdraw method from the previous example and deducts the requested amount from state x. After it is done, the actor proceeds to the next message in the queue.

我们有一个ReplicaActor负责这项工作,而BossActor负责订购该副本。 首先,注意! 签名或告诉 。 这是一种Actor异步地将消息发送给另一个Actor的两种方法之一(另一个是ask )。 告诉尤其是在执行时不等待答复。 因此,老板告诉副本执行两次撤回命令并立即离开。 这些消息到达副本的接收器 ,其中每个消息都会弹出并与相应的处理程序匹配。 在这种情况下, 退出执行从前面的例子中的退出方法和扣除从状态X所请求的量。 完成后,参与者将前进到队列中的下一条消息。

So what do we get here? For one, we no longer need to worry about locking and working with atomic/concurrent types. Actor’s encapsulation and queuing mechanism already guarantee thread-safety. And there is no more waiting since threads just drop the message and return. Results can be delivered later with ask or tell. It’s simple and sane.

那我们到这里来做什么? 首先,我们不再需要担心锁定和使用原子/并发类型。 Actor的封装和排队机制已经保证了线程安全性。 由于线程只是丢弃消息并返回,因此不再等待。 结果可以稍后通过AskTell传递。 简单而理智。

Akka is based on JVM and available in both Scala and Java. Although this article isn’t debating Java vs Scala, Scala’s pattern matching and functional programming would be very useful in managing Actor’s data messaging. At the very least it can help you write shorter code by avoiding Java’s brackets and semicolons.

Akka基于JVM,并且在Scala和Java中均可用。 尽管本文并未讨论Java与Scala的争论,但Scala的模式匹配和函数式编程在管理Actor的数据消息传递方面将非常有用。 至少它可以避免Java的方括号和分号,从而帮助您编写较短的代码。

翻译自: https://www.freecodecamp.org/news/still-using-synchronized-try-akka-actor-instead-ac2f2b22a9ed/

actor akka

actor akka_如果您仍在使用同步,则应改用Akka Actor-这就是为什么相关推荐

  1. akka actor java_Akka:使用非默认构造函数在Scala中定义一个actor并从Java代码创建它 - java...

    Akka Scala演员必须扩展akka.actor.Actor Akka Java actor必须扩展akka.actor.UntypedActor 因此,在使用非默认构造函数定义Scala act ...

  2. akka actor java_现代化的Java(三)——从Hello Akka说起

    接下来打算花一些篇幅介绍一些异步编程的范式,也算是给自己一个学习笔记吧. 异步编程是个很诡异的领域,每个人都在谈论它,但是工作中很少有人能驾驭:很多很新潮很热的异步编程概念,运用起来却完全达不到它宣称 ...

  3. Akka Actor模型的简介与Actor的创建方式

    Akka Actor其具有以下特点: 系统中的所有事物都可以扮演一个Actor Actor之间完全独立 在收到消息时,Actor所采取的所有动作都是并行的,在一个方法中的动作没有明确的顺序 Actor ...

  4. java akka actor,【Akka】Actor引用

    Actor系统的实体 在Actor系统中,actor之间具有树形的监管结构,并且actor可以跨多个网络节点进行透明通信. 对于一个Actor而言,其源码中存在Actor,ActorContext,A ...

  5. scala中akka actor例子

    抛开复杂的业务逻辑,让我们从一个超级简单的例子学习Akka Actor的用法. Scala cookbook的作者Alvin Alexander在他的网站上提供了两个例子. 本文翻译.整理于他的两篇文 ...

  6. akka actor父子监管的实现

    akka中,父actor可以定义supervisorStartegy来实现对子actor的异常监管应对策略. * override val supervisorStrategy = OneForOne ...

  7. AKKA Actor创建

    Actor 类定义 Actor 类需要继承AbstractActor类 实现createReceive方法,绑定各类actor收到不同类型消息对应处理不同业务逻辑 默认提供了ReceiveBuilde ...

  8. java akka actor性能_akka-http - 如何通过Akka HTTP(Java)与Akka Actor进行交互 - 堆栈内存溢出...

    话题 我想通过Akka HTTP与Akka Actor进行交互 . 这个想法是要有一个系统,其中HTTP客户端调用Akka HTTP服务器方法,该方法处理对Akka Actor的请求. actor处理 ...

  9. Akka Actor Inbox_信箱

    2019独角兽企业重金招聘Python工程师标准>>> Akka Actor Inbox_信箱 Inbox_send_receive Inbox 形象的表示为Actor的信箱,具有收 ...

最新文章

  1. rmi 反序列化漏洞_提醒:Apache Dubbo存在反序列化漏洞
  2. 微信公众平台两种消息处理流程对比
  3. Java基础day15
  4. 动态决定viewarea应该加载哪个view
  5. 操作系统:Windows映射网络文件夹的方法介绍
  6. Akka(19): Stream:组合数据流,组合共用-Graph modular composition
  7. Redis面试常问3 如何实现分布式锁 记住Redis的原子性
  8. 模拟电子技术不挂科学习笔记3(放大电路的分析方法)
  9. 设计HTML标签title属性值换行
  10. linux 远程shell,linux 远程shell 实现
  11. c语言程序设计基础所有知识点,《C语言程序设计》基础知识点总结.doc
  12. c#使用word、excel、pdf ——转
  13. 德国的“隐形冠军”是怎么造成的?
  14. 一篇基金研报--《外包服务:后安迪-比尔时代IT产业的大餐》
  15. GBIT51231-2016装配式混凝土结构建筑技术标准
  16. 泰坦尼克号数据_泰坦尼克号数据分析案例实战
  17. 基于FPGA的ROM-VGA图像处理(老师好帅系列)
  18. QQ丶微信分享URL Schemes填写
  19. Vue:插槽属性prop的使用示例
  20. java 运费_如何在Java中创建运费成本计算器

热门文章

  1. 自动化测试selenum
  2. django-项目的创建-应用注册-项目运行
  3. 一键安装zabbix percona mysql插件监控mysql
  4. Redis+Twemproxy安装与使用
  5. 取模(mod)与取余(rem)的区别——Matlab学习笔记
  6. PostgreSQL 范围过滤 + 其他字段排序OFFSET LIMIT(多字段区间过滤)的优化与加速
  7. BZOJ 1032 JSOI 2007 祖码Zuma 区间DP
  8. 关于在大网段中拆出小网段地址
  9. springMVC视频教程
  10. HTMLParser使用举例