简体   繁体   English

Scala参与者,期货和系统调用导致线程泄漏

[英]Scala actors, futures and system calls resulting in Thread leaks

I am running a complex software with different actors (scala actors). 我正在运行一个具有不同角色(scala角色)的复杂软件。 Some of them have some executions that uses scala futures to avoid locking and keep processing new received messages (simplified code): 其中一些执行使用scala期货以避免锁定并继续处理收到的新消息(简化代码):

  def act {
    while (true) {
      receive {
        case (code: String) =>
          val codeMatch = future { match_code(code) }
          for (c <- codeMatch)
            yield callback(code)(JSON.parseJSON(c))
      }
    }
  }

  def match_code(code: String) {
     val result = s"my_script.sh $code" !!
  }

I noticed looking at jvisualvm and Eclipse Debugger that the number of active threads keeps increasing when this system is running. 我注意到在jvisualvm和Eclipse Debugger时,该系统正在运行时,活动线程数一直在增加。 I am afraid I am having some kind of Thread leak, but I can't detect where is the problem. 恐怕我遇到某种线程泄漏,但是我无法检测出问题出在哪里。

Here are some screenshots of both finished and live threads (I hided some live threads that are not related to this problem) 这是完成线程和活动线程的一些屏幕截图(我隐藏了一些与此问题无关的活动线程)

Finished Threads 成品螺纹 成品螺纹

Living threads 活动线程 活动线程

Edit 1: In the above graphs example, I run the system with only 3 actors of different classes: Actor1 sends messages to Actor2 that sends message to Actor3 编辑1:在上面的图形示例中,我仅使用3个不同类的actor运行系统:Actor1将消息发送到Actor2,然后将消息发送给Actor3

You are using receive so each actor will use its own thread, and you don't at least in this example provide any way for actors to terminate. 您使用的是receive因此每个actor都将使用其自己的线程,并且至少在此示例中,您没有提供任何方法终止actor。 So you would expect to have one new thread per actor that was ever started. 因此,您可能希望每个actor都有一个新线程已经启动。 If that is what you see, then all is working as expected. 如果您看到的是那样,那么一切都按预期工作。 If you want to have actors cease running, you will have to let them eventually fall out of the while loop or call sys.exit on them or somesuch. 如果要使actor停止运行,则必须让它们最终退出while循环,或在它们上调用sys.exit或类似方法。

(Also, old-style Scala actors are deprecated in favor of Akka actors in 2.11.) (此外,在2.11版中,老式Scala演员被弃用,而Akka演员被弃用。)

You also don't (in the code above) have any indication whether the future actually completed. 您(在上面的代码中)也没有任何迹象表明未来是否真正完成。 If the futures don't finish, they'll keep tying up threads. 如果期货没有完成,他们将继续束缚线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM