using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
CancellationTokenSource cts = new CancellationTokenSource();
// Create an opportunity for the user to cancel.
Task.Run(() =>
{
if (Console.ReadKey().KeyChar == 'c' || Console.ReadKey().KeyChar == 'C')
cts.Cancel();
});
ObjectPool<MyClass> pool = new ObjectPool<MyClass>(() => new MyClass());
// Create a high demand for MyClass objects.
Parallel.For(0, 500, (i, loopState) =>
{
MyClass mc = pool.GetObject();
mc.GetValue(i);
Thread.Sleep(500);
// Console.CursorLeft = 0;
// This is the bottleneck in our application. All threads in this loop
// must serialize their access to the static Console class.
// Console.WriteLine("{0:####.####}", mc.GetValue(i));
pool.PutObject(mc);
if (cts.Token.IsCancellationRequested)
loopState.Stop();
});
Console.WriteLine("Press the Enter key to exit.");
Console.ReadLine();
cts.Dispose();
}
}
public class ObjectPool<T>
{
private ConcurrentQueue<T> _objects;
private Func<T> _objectGenerator;
private static int COUNT = 0;
private int _size;
public ObjectPool(Func<T> objectGenerator,int size = 5)
{
if (objectGenerator == null) throw new ArgumentNullException("objectGenerator");
this._size = size;
_objects = new ConcurrentQueue<T>();
_objectGenerator = objectGenerator;
}
public T GetObject()
{
T item = default(T);
if (_objects.TryDequeue(out item)){
Console.WriteLine("Here is a available resource");
return item;
}else
{
if (COUNT <= _size)
{
Console.WriteLine("Here is more capacity, create a new resource");
COUNT++;
return _objectGenerator();
}
return GetObjectAsync().Result;
}
}
private async Task<T> GetObjectAsync() {
await Task.Run(() =>
{
int i = 0;
while (_objects.Count == 0) {
i++;
Task.Delay(100);
}
Console.WriteLine("Pool is fullified, cannot insert new object, wait for release after " +i+" times tries");
return;
});
Console.WriteLine("Here is a released resource");
return GetObject();
}
public void PutObject(T item)
{
T temp;
if (_objects.Count > 5)
{
_objects.TryDequeue(out temp);
}
_objects.Enqueue(item);
}
}
class MyClass
{
public int[] Nums { get; set; }
public double GetValue(long i)
{
return Math.Sqrt(Nums[i]);
}
public MyClass()
{
Nums = new int[1000000];
Random rand = new Random();
for (int i = 0; i < Nums.Length; i++)
Nums[i] = rand.Next();
}
}
}
对象池
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...