Last active
August 29, 2015 14:04
-
-
Save onlytiancai/d8574e1839867cbdbc9c to your computer and use it in GitHub Desktop.
BufferBase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.Threading; | |
namespace BufferBaseLib | |
{ | |
public interface IBufferState | |
{ | |
void Commit(); | |
} | |
public interface IBuffer | |
{ | |
IBufferState CaptureState(); | |
} | |
public abstract class BufferBase<T> : IBuffer | |
{ | |
private readonly object _gate = new object(); | |
private List<T> _items; | |
public class BufferState : IBufferState | |
{ | |
private BufferBase<T> _buffer; | |
private IList<T> _items; | |
public BufferState(BufferBase<T> buffer, IList<T> items) | |
{ | |
this._buffer = buffer; | |
this._items = items; | |
} | |
public void Commit() | |
{ | |
if (this._items.Count > 0) | |
{ | |
this._buffer.Flush(this._items); | |
} | |
} | |
} | |
public void Add(T item) | |
{ | |
lock (_gate) | |
{ | |
if (_items == null) | |
{ | |
_items = new List<T>(); | |
} | |
_items.Add(item); | |
} | |
} | |
public IBufferState CaptureState() | |
{ | |
List<T> items; | |
lock (_gate) | |
{ | |
items = _items; | |
_items = null; | |
} | |
return new BufferState(this, items); | |
} | |
protected abstract void Flush(IList<T> items); | |
} | |
public class LogBuffer : BufferBase<string> | |
{ | |
protected override void Flush(IList<string> items) | |
{ | |
foreach (string item in items) | |
{ | |
Console.WriteLine("LogBuffer Flush:{0}", item); | |
} | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
LogBuffer logBuff = new LogBuffer(); | |
new Thread(() => | |
{ | |
while (true) | |
{ | |
logBuff.Add(string.Format("add thread 1: {0}", DateTime.Now)); | |
Thread.Sleep(100); | |
} | |
}).Start(); | |
new Thread(() => | |
{ | |
while (true) | |
{ | |
logBuff.Add(string.Format("add thread 2: {0}", DateTime.Now)); | |
Thread.Sleep(100); | |
} | |
}).Start(); | |
new Thread(() => | |
{ | |
while (true) | |
{ | |
logBuff.CaptureState().Commit(); | |
Thread.Sleep(1000); | |
} | |
}).Start(); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
简化了一下