public void Dispatch(IJob job) { var resetEvent = CreateAndTrackResetEvent(); var worker = new ThreadPoolWorker(job, resetEvent); ThreadPool.QueueUserWorkItem(new WaitCallback(worker.ThreadPoolCallback)); }
/// <summary> /// Run a Job /// </summary> /// <param name="job">job to run</param> /// <returns>true</returns> public bool Dispatch(ThreadStart job) { if (!IsRunning || job == null) { return(false); } ThreadPoolWorker thread = null; bool spawn = false; // Look for an idle thread thread = _idleQue.Dequeue(); if (thread == null) { // queue the job int count = _jobsQue.Enqueue(job); lock (_lock) { //int count = _jobsQue.Count; if (count > _maxQueued) { _maxQueued = count; } } spawn = count > _spawnOrShrinkAt; if (spawn) { NewThread(); } } else { // assign the job to the selected thread thread.Dispatch(job); } return(true); }
protected void NewThread() { lock (_threadsLock) { if (_threads.Count < _maxThreads) { ThreadPoolWorker thread = new ThreadPoolWorker(this); _threads.Add(thread); thread.Name = thread.Id + "@" + _name + "-" + _id++; thread.Start(); } else if (!_warned) { _warned = true; Log.Debug("Max threads for {0}", this); } } }
/// <summary> /// Creates a new worker with the given <paramref name="workerName"/> /// </summary> public IWorker CreateWorker(string workerName) { if (workerName == null) { throw new ArgumentNullException(nameof(workerName)); } var owningBus = _busGetter(); var worker = new ThreadPoolWorker( workerName, _transport, _rebusLoggerFactory, _pipelineInvoker, _parallelOperationsManager, owningBus, _options, _backoffStrategy, _busDisposalCancellationToken ); return(worker); }
public bool Equals([AllowNull] ThreadPoolEventStatistics other) { return(Type == other.Type && ThreadPoolWorker.Equals(other.ThreadPoolWorker) && ThreadPoolAdjustment.Equals(other.ThreadPoolAdjustment)); }
protected void NewThread() { lock (_threadsLock) { if (_threads.Count<_maxThreads) { ThreadPoolWorker thread = new ThreadPoolWorker(this); _threads.Add(thread); thread.Name = thread.Id + "@" + _name + "-" + _id++; thread.Start(); } else if (!_warned) { _warned=true; Log.Debug("Max threads for {0}",this); } } }