コード例 #1
0
ファイル: FibonacciActor.cs プロジェクト: Andreeii/ADA-LABS
        private void ProcessFibonacci(FibonacciMessage message)
        {
            var fibonacciCalculator = new FibonacciCalculator();

            for (int i = 0; i < message.NumbersToProcess; i++)
            {
                var numberIndex = message.StartIndex + i;
                var result      = fibonacciCalculator.Calculate(message.FibonacciNumbers[numberIndex], message.ProcessingType);
                SynchronizationContext.AddNumberAndCheckIfIsDone(result);
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: Ernest96/ADA-LABS
        private static void ProcessFibonacciWithActors(long[] fibonacciNumbers, int numberOfActors, ProcessingType processingType)
        {
            var actorSystem = ActorSystem.Create($"Fibonacci-Actors-{numberOfActors}");
            var actors      = new List <IActorRef>(numberOfActors);
            var messages    = new List <FibonacciMessage>(numberOfActors);

            for (int i = 0; i < numberOfActors; i++)
            {
                int taskIndex = i;
                var actor     = actorSystem.ActorOf(Props.Create <FibonacciActor>());

                int numbersPerTask   = fibonacciNumbers.Length / numberOfActors; // ideal case, even tasks and even numbers count
                int numbersToProcess = 0;

                if (taskIndex == numberOfActors - 1) // if is last actor
                {
                    numbersToProcess = fibonacciNumbers.Length - (numbersPerTask * (numberOfActors - 1));
                }
                else
                {
                    numbersToProcess = numbersPerTask;
                }

                var message = new FibonacciMessage()
                {
                    FibonacciNumbers = fibonacciNumbers,
                    NumbersToProcess = numbersToProcess,
                    StartIndex       = taskIndex * numbersPerTask,
                    ProcessingType   = processingType
                };

                actors.Add(actor);
                messages.Add(message);
            }

            SynchronizationContext.InitAndStartWatch(fibonacciNumbers.Length);

            for (int i = 0; i < numberOfActors; i++)
            {
                actors[i].Tell(messages[i]);
            }
        }