I have a number of worker actors and a list of tasks (a linked list) which is sent to the workers by a master actor. Each element of the linked list has a Boolean flag that specifies whether the element is taken by a worker yet or not. If not, a worker set the flag to chosen and start working on that element. When the actor finish that element will check the list elements for the next not chosen element. Workers will continue until no element remain in the list to work on. My question is the best way to set the flag to show the element is chosen. I am thinking of using an AtomicBoolean for the flag to check and set it atomically. But I don't know how to use AtomicBoolean in scala. Please advise
You can import Java's AtomicBoolean and use it as usual:
scala> import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicBoolean
scala> val ab = new AtomicBoolean(false)
ab: java.util.concurrent.atomic.AtomicBoolean = false
I would rethink the design, though - actors usually don't need to synchronize over any mechanism other than their queues/inboxes. How about: