从 Java 线程转向 scala Actor

发布于 2024-12-13 23:56:05 字数 480 浏览 6 评论 0原文

我有一个java应用程序,它有各种代理(Java Algo),它们监听消息并处理它们。每个代理在单个线程上执行这些消息,这些消息被添加到阻塞队列中以便一一处理。对单个消息的这种处理涉及与远程机器的 I/O。随着时间的推移,由于java中固有的Bug,这些线程被一一挂起。他们说该错误已修复,但事实并非如此,我已经在所有最新的 java 版本上运行了它,并尝试了一个月的所有解决方法。所以,我计划转向 scala actor。

以下是我的问题:

  1. 上述要求在 scala actor 中是否可行,并且会克服 java bug 吗?

  2. 如果可能的话如何做的简单概述:)

    PS:我一直在阅读《scala 编程》一书,以获取语法和基本功能。

i have a java application which has various Agents(Java Algo's) which listen to messages and process them . Each agent executes these messages on a single thread where these messages are added in a blocking queue to be processed one by one . This processing on a single message involves i/o with a remote machine . Overtime , these threads get hung one by one due to an inherent Bug in java . They say the bug is fixed but its not , i have run it on all latest java versions and tried all workarounds for a month now .So, i am planning to move to scala actors .

Below are my questions :

  1. Is the above requirement possible in scala actors and will it overcome the java bug ?

  2. A simple overview of how to do it if it's possible :)

    P.S : i have been reading through the programming in scala book for getting the syntax and basic features .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

此岸叶落 2024-12-20 23:56:05

您引用的错误是 jvm 错误。因此,如果您在同一个 JVM 上使用“相同”代码,您仍然会遇到该错误。 Scala 仍然在 JVM 上运行,因此您仍然有可能遇到相同的错误。

Scala Actors 可能不会以同样的方式使用 JVM。但你不能保证任何事情。尝试一下看看。

The bug you cite is a jvm bug. Therefore, if you're using the 'same' code on the same JVM, you'll still experience the bug. Scala still runs on the JVM, so there is still a chance that you'll come across the same bug.

Scala Actors probably won't exercise the JVM in the same way. But you can't guarantee anything. Try it and see.

你另情深 2024-12-20 23:56:05

如果您刚刚开始使用 actor 模型,我强烈建议您使用 akka.io 中的 actor 包。当我开始涉足时,我在使用标准 Scala 远程 actor 库时遇到了一些问题。我还听说,随着时间的推移,akka actor 实现将取代 Scala 标准库中当前的实现。

这本身并没有解决您的问题,但是编写基于 Actor 的并发程序比管理您自己的关键部分简单得多 - 至少这是我的经验。

If you're just getting started with the actor model, I highly recommend using the actors package from akka.io. When I started dabbling I had some problems when using the standard Scala remote actors library. I have heard also that in time, the akka actor implementation will replace the current one in the Scala standard library.

This doesn't address your question per se, but writing Actor based concurrent programs is a lot simpler than managing your own critical sections -- at least that has been my experience.

一杆小烟枪 2024-12-20 23:56:05
  1. 对于第一个问题,答案是肯定的,Scala Actor 确实克服了由于其 Actor 并发模型而提到的错误。过去两周我一直在使用它,没有停机。 Actor 是轻量级的,易于扩展,并且绝对比我在 Java 中的线程代理执行得更快。内存消耗也奇怪地下降了。

  2. 现在对于实现部分来说,一旦您了解了 scala 的基础知识,编写 actor 就很容易了。如果您使用 eclipse + maven 插件 m2eclipse-scala 是开始 。我已经使用那里提到的步骤成功构建了一个 osgi 包。 scala ide 是一项正在进行中的工作,编写代码有点痛苦。

  3. 现在对于 scala 参与者来说,处理他们的消息收件箱是很困难的。对它们的访问受到限制,并且当消息传入的速率高于参与者可以处理的速率时,它们似乎很容易脱离内存。 Akka Actor 在这方面绝对有优势,我们可以设置最大邮箱大小、邮箱推送超时等,或者您可以拥有自己的收件箱实现。

谢谢你的帮助

sanre6

  1. For the first question the answer is yes , scala actors do overcome the bug mentioned due to its actor concurrency model . I have been using it over the past 2 weeks with no downtime . Actors are lightweight , easy to scale and are definetly performing faster than my threaded agents in java . The memory consumption has come down as well weirdly.

  2. Now for the implementation part , writing actors is easy once you know the basics of scala . If you are using eclipse + maven plugin m2eclipse-scala is the place to start . i have successfully built an osgi bundle using the steps mentioned there . The scala ide is a work in progress and is a bit of pain to write code .

  3. Now for the scala actors , working with their message inbox's is hard . Accessing them is restricted and they seem to easily get out of memory when the rate of message's coming in is higher than the rate at which the actor could process . Akka actors definitely have an advantage here in by enabling us to set the max mailbox size,mailbox-push-timeout..etc or you can have your own inbox implementation .

Thanks for you help

sanre6

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文