コード例 #1
0
        public static void AddNumberAndCheckIfIsDone(long number)
        {
            ConcurrentStore.AddNumberToStore(number);
            Interlocked.Increment(ref _numbersProcessed);

            if (_numbersProcessed >= _numbersToProcess)
            {
                _stopWatch.Stop();
                Console.WriteLine($"{_stopWatch.ElapsedMilliseconds} milliseconds");

                var result = ConcurrentStore.GetSortedNumbersFromStore();
                FileUtility.WriteNumbersToFile(OUTPUT_FILE, result);

                ConcurrentStore.ClearStore();
                _numbersProcessed = 0;
            }
        }
コード例 #2
0
        private static void CalculateFibonacci(long[] fibonacciNumbers, int numberOfTasks, int taskIndex, ProcessingType processingType)
        {
            var fibonacciCalculator = new FibonacciCalculator();
            int numbersPerTask      = fibonacciNumbers.Length / numberOfTasks; // ideal case, even tasks and even numbers count
            int numbersToProcess    = 0;

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

            for (int i = 0; i < numbersToProcess; i++)
            {
                var numberIndex = taskIndex * numbersPerTask + i;
                var result      = fibonacciCalculator.Calculate(fibonacciNumbers[numberIndex], processingType);
                ConcurrentStore.AddNumberToStore(result);
            }
        }