Exemple #1
0
        public void ProduceTest_WhenMaxProduceWithConsume_ShouldReturnAllItems(int consumersCount)
        {
            var maxSize = 10;
            var actual  = new BlockingDictionary <int, string>(
                maxSize,
                new SortedDictionary <int, string>(
                    new Dictionary <int, string>(1000)));

            var produceThread  = new Thread(() => Produce(actual, Enumerable.Range(0, 1000).ToArray()));
            var consumeThreads = new List <Thread>();

            consumeThreads
            .AddRange(Enumerable.Range(0, consumersCount)
                      .Select(_ => new Thread(() => Consume(actual))));

            produceThread.Start();
            foreach (var thread in consumeThreads)
            {
                thread.Start();
            }

            produceThread.Join();
            actual.Close();
            foreach (var thread in consumeThreads)
            {
                thread.Join();
            }

            Assert.That(actual.Size(), Is.EqualTo(0));
        }
Exemple #2
0
        public void Start()
        {
            var readThread = new Thread(Read)
            {
                Name = "Read thread"
            };

            readThread.Start();

            var processingThreads = new List <Thread>();

            for (var i = 0; i < Environment.ProcessorCount; i++)
            {
                var processingThread = new Thread(Processing)
                {
                    Name = $"Processing {i}"
                };
                processingThreads.Add(processingThread);
                processingThread.Start();
            }

            var writeThread = new Thread(Write)
            {
                Name = "Write thread"
            };

            writeThread.Start();

            readThread.Join();
            _processingBuffer.CloseQueue();

            foreach (var processingThread in processingThreads)
            {
                processingThread.Join();
            }
            _writingBuffer.Close();

            writeThread.Join();

            if (_error != null)
            {
                throw _error;
            }
        }
Exemple #3
0
        public void Process(string inputFilePath, string outputFilePath)
        {
            using var inputDictionary = new BlockingDictionary <int, byte[]>(
                      _settings.InputBufferSize,
                      new SortedDictionary <int, byte[]>(new Dictionary <int, byte[]>(_settings.InputBufferSize)));
            using var outputDictionary = new BlockingDictionary <int, byte[]>(
                      _settings.OutputBufferSize,
                      new Dictionary <int, byte[]>(_settings.OutputBufferSize));
            var workerThreads = new List <Thread>();

            _logger.Write($"Start processing with parallelism level {_settings.ParallelismLevel}");

            using var binaryReader = _fileService.GetReader(inputFilePath);
            var readerThread = new Thread(() => _reader.Read(inputDictionary, binaryReader));

            workerThreads.AddRange(
                Enumerable.Range(0, _settings.ParallelismLevel)
                .Select(_ => new Thread(() => _processor.ProcessChunks(inputDictionary, outputDictionary))));

            using var binaryWriter = _fileService.GetWriter(outputFilePath);
            var writerThread = new Thread(() => _writer.Write(outputDictionary, binaryWriter));

            readerThread.Start();
            foreach (var thread in workerThreads)
            {
                thread.Start();
            }
            writerThread.Start();
            readerThread.Join();
            inputDictionary.Close();

            foreach (var thread in workerThreads)
            {
                thread.Join();
            }

            outputDictionary.Close();
            writerThread.Join();
        }