1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using System;
- using System.Threading;
- using System.Collections.Generic;
- namespace EZXR.Glass.Core.Threading
- {
- public class SerailQueue
- {
- readonly object _locker = new object();
- Thread[] _workers;
- Queue<Action> _itemQ = new Queue<Action>();
- public SerailQueue(int workerCount)
- {
- _workers = new Thread[workerCount];
-
- for (int i = 0; i < workerCount; i++)
- (_workers[i] = new Thread(Consume)).Start();
- }
- public void Shutdown(bool waitForWorkers)
- {
-
- foreach (Thread worker in _workers)
- EnqueueItem(null);
-
- if (waitForWorkers)
- foreach (Thread worker in _workers)
- worker.Join();
- }
- public void EnqueueItem(Action item)
- {
- lock (_locker)
- {
- _itemQ.Enqueue(item);
- Monitor.Pulse(_locker);
- }
- }
- public bool isBusy()
- {
- lock (_locker)
- {
- return _itemQ.Count > 0;
- }
- }
- void Consume()
- {
- while (true)
- {
- Action item;
- lock (_locker)
- {
- while (_itemQ.Count == 0) Monitor.Wait(_locker);
- item = _itemQ.Dequeue();
- }
- if (item == null) return;
- item();
- }
- }
- }
- }
|