0

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

A.G
  • 37
  • 3

1 Answers1

1

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:

  1. The master actor distributes work units among worker actors
  2. worker actors process the units an send ready bits to accumulator actor
  3. accumulator actor gathers the ready results and outputs a list.
Michael Bar-Sinai
  • 2,729
  • 20
  • 27
  • Thanks Michael. The list is heterogeneous and some of the elements take much more time to do than the others. I cannot evenly distribute it between workers at first. I am thinking of sending one element to each worker. Once a worker done with that element informs the master. Then master sends the next element to that free worker? – A.G Nov 09 '16 at 20:15
  • 2
    @A.G, that comment is really an entirely different question about how to distribute tasks across workers. You may get a better response posting it as a separate question – The Archetypal Paul Nov 09 '16 at 21:09