Is the Akka onReceive method running at the same time?

Here is my scenario:

I have a master actor who receives messages from several child actors. These messages contain data for aggregation. In this aggregation logic, do I need to take care of synchronization issues if I use a common data structure to collect aggregation?

else if(arg0 instanceof ReducedMsg){

                           ReducedMsg reduced = (ReducedMsg)arg0;
        counter.decrementAndGet();

        synchronized(finalResult){

            finalResult.add((KeyValue<K, V>) reduced.getReduced());

            if(counter.get() == 0){
                                    if(checkAndReduce(finalResult)){

                    finalResult.clear();
                }
                else{
                    stop();
                    latch.countDown();
                }

            }

        }



    }

So, as you can see, I have finalResult for which each message will be aggregated, and after the processing logic the collection should also be cleared.

In fact, what I'm trying to implement is a recursive (associative) reduction of mapreduce. So I need to keep the synchronized block, which I assume? Or is it by accident that Akka executes onReceive one thread at a time?

. , , . , - , dwelve .

+5
1

onReceive() . , .

, counter , , int/long AtomicInteger/AtomicLong. finalResult , , , .

, CountDownLatch . Akka . , ( ) .

: http://doc.akka.io/docs/akka/2.0.2/general/jmm.html#Actors_and_the_Java_Memory_Model

+15
source

All Articles