public ProducerWorker(ConcurrentBuffer buffer, int wordCount, int sleepCount)
 {
     _buffer         = buffer;
     this.wordCount  = wordCount;
     this.sleepCount = sleepCount;
     Stop            = false;
 }
Beispiel #2
0
        public HttpStatusCode Post(Settings newSettings)
        {
            if (_worker._settings == null)
            {
                _worker._settings = new Settings();
            }

            if (newSettings == null)
            {
                return(HttpStatusCode.BadRequest);
            }

            if (_worker._settings.Equals(newSettings))
            {
                return(HttpStatusCode.OK);
            }
            //else

            lock (_worker.locker)
            {
                if (_worker._buffer == null)
                {
                    _worker._buffer = new ConcurrentBuffer(newSettings.BufferSize);
                }

                if (_worker._buffer.Size != newSettings.BufferSize)
                {
                    var buffer = new ConcurrentBuffer(newSettings.BufferSize);

                    //Grab all items in old buffer and place in new buffer.
                    _worker._buffer.switching = true;
                    string[] items = _worker._buffer.ToArray();
                    foreach (var item in items)
                    {
                        buffer.TryAdd(item);
                    }
                    _worker._buffer = buffer;
                    Parallel.ForEach(_worker._threadManager.GetRunningThreads(), (tuple) => { tuple.Item2._buffer = _worker._buffer; });
                }

                if (_worker._settings.ProducerWordCount != newSettings.ProducerWordCount)
                {
                    Parallel.ForEach(_worker._threadManager.GetRunningThreads(), (tuple) => { tuple.Item2.wordCount = newSettings.ProducerWordCount; });
                }

                if (_worker._settings.ProducerSleepNum != newSettings.ProducerSleepNum)
                {
                    Parallel.ForEach(_worker._threadManager.GetRunningThreads(), (tuple) => { tuple.Item2.sleepCount = newSettings.ProducerSleepNum; });
                }

                while (newSettings.NumOfProducers > _worker._threadManager.CurrentCount)
                {
                    ProducerWorker producer = new ProducerWorker(_worker._buffer, newSettings.ProducerWordCount,
                                                                 newSettings.ProducerSleepNum);
                    Thread thread = new Thread(producer.Produce)
                    {
                        Name         = "Producer " + Guid.NewGuid().ToString(),
                        IsBackground = false
                    };
                    thread.SetApartmentState(ApartmentState.MTA);

                    _worker._threadManager.AddThread(thread, producer);
                    thread.Start();
                }

                while (newSettings.NumOfProducers < _worker._threadManager.CurrentCount)
                {
                    var tuple = _worker._threadManager.GetRunningThreads()[0];
                    _worker._threadManager.KillThread();
                }
                _worker._settings = newSettings;
            }
            return(HttpStatusCode.OK);
        }
 public Producer(ConcurrentBuffer buffer)
 {
     _buffer = buffer;
 }