How to make a code thread safe in scala?

后端 未结 4 805
[愿得一人]
[愿得一人] 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:36

    If it is really very simple you can use synchronized: http://www.ibm.com/developerworks/java/library/j-scala02049/index.html

    Or you could use some of the classes from the concurrent package in the jdk: http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/package-summary.html

    If you want to use actors, you should use akka actors (they will replace scala actors in the future), see here: http://doc.akka.io/docs/akka/2.0.1/. They also support things like FSM (Finite State Machine) and STM (Software Transactional Memory).

提交回复
热议问题