How to make a code thread safe in scala?

后端 未结 4 822
[愿得一人]
[愿得一人] 2021-01-14 02:17

I have a code in scala that, for various reasons, have few lines of code that cannot be accessed by more threads at the same time.

How to easily make it thread-safe?

4条回答
  •  情深已故
    2021-01-14 02:48

    You can use any Java concurrency construct, such as Semaphores, but I'd recommend against it, as semaphores are error prone and clunky to use. Actors are really the best way to do it here.

    Creating actors is not necessarily hard. There is a short but useful tutorial on actors over at scala-lang.org: http://www.scala-lang.org/node/242

提交回复
热议问题