4

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? I know I could use Actors model, but I find it a bit too overkill for few lines of code.

I would use some kind of lock, but I cannot find any concrete examples on either google or on StackOverflow.

Karel Bílek
  • 36,467
  • 31
  • 94
  • 149
  • 1
    as several answers indicate, it isn't hard to change your code such that you don't need to have a critical section at all – that is almost certainly the best solution. – Jed Wesley-Smith May 23 '12 at 13:28

4 Answers4

15

I think that the most simple solution would be to use synchronized for critical sections (just like in Java). Here is Scala syntax for it:

someObj.synchronized {
    // tread-safe part
}

It's easy to use, but it blocks and can easily cause deadlocks, so I encourage you to look at java.util.concurrent or Akka for, probably, more complicated, but better/non-blocking solutions.

tenshi
  • 26,268
  • 8
  • 76
  • 90
3

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

Community
  • 1
  • 1
evilcandybag
  • 1,942
  • 17
  • 17
  • How to do that easily? I really have a simple code (that I can for example put in a procedure call) and I don't want to make the code too complicated. All the Actors example want me to make loops and receive messages and so on, I think it is a little overkill. – Karel Bílek May 23 '12 at 10:44
  • Added a link to a good beginners' tutorial on actors to my answer. – evilcandybag May 23 '12 at 10:47
  • I *do* understand Actors (a bit). I am just not sure how to use it in my situation. Should I make the whole object with the code an actor? That would probably break more stuff than not, because it's subclassed and the calls needed as regular calls. Should I create one more object with just one message type which will do just this short code? That's possible I guess, but probably too complicated. – Karel Bílek May 23 '12 at 10:53
  • If you really don't wanna use actors, you might want to check out this rather nice tutorial on Java concurrency, specifically monitors: http://www.baptiste-wicht.com/2010/09/java-concurrency-part-5-monitors-locks-and-conditions/ – evilcandybag May 23 '12 at 10:54
  • I am open to possibilities, I am just not sure how to use it, that's all :) – Karel Bílek May 23 '12 at 10:55
  • Looks like what @tenshi wrote in his answer is the way to go, forgot about that one. – evilcandybag May 23 '12 at 11:01
1

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).

Christian
  • 4,543
  • 1
  • 22
  • 31
0

In general try to use pure 'functions' or methods with immutable data structures that should help with thread safety.

AndreasScheinert
  • 1,918
  • 12
  • 18