本文整理汇总了C#中ObjectPool.PutObject方法的典型用法代码示例。如果您正苦于以下问题:C# ObjectPool.PutObject方法的具体用法?C# ObjectPool.PutObject怎么用?C# ObjectPool.PutObject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObjectPool
的用法示例。
在下文中一共展示了ObjectPool.PutObject方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
private static void Main(string[] args)
{
CancellationTokenSource cts = new CancellationTokenSource();
Task task = new Task(() =>
{
if (Console.ReadKey().KeyChar == 'c' || Console.ReadKey().KeyChar == 'C')
{
cts.Cancel();
}
});
task.Start();
ObjectPool<MyClass> pool = new ObjectPool<MyClass>(() => new MyClass());
Parallel.For(0, 1000000, (i, loopstate) =>
{
MyClass mc = pool.GetObject();
Console.CursorLeft = 0;
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();
}
示例2: GetPut
public void GetPut()
{
var pool = new ObjectPool<DisposedTracking>(() => new DisposedTracking());
var item1 = pool.GetObject();
item1.Use();
var item2 = pool.GetObject();
item2.Use();
pool.PutObject(item2);
pool.PutObject(item1);
var item3 = pool.GetObject();
item3.Used.Should().BeTrue();
var item4 = pool.GetObject();
item4.Used.Should().BeTrue();
}
示例3: btnRun_Click
/// <summary>Run a game.</summary>
private void btnRun_Click(object sender, EventArgs e)
{
// If no game is currently running, run one
if (_cancellation == null)
{
// Clear the current image, get the size of the board to use, initialize cancellation,
// and prepare the form for game running.
pictureBox1.Image = null;
int width = pictureBox1.Width, height = pictureBox1.Height;
_cancellation = new CancellationTokenSource();
var token = _cancellation.Token;
lblDensity.Visible = false;
tbDensity.Visible = false;
btnRun.Text = "Stop";
double initialDensity = tbDensity.Value / 1000.0;
// Initialize the object pool and the game board
var pool = new ObjectPool<Bitmap>(() => new Bitmap(width, height));
_game = new GameBoard(width, height, initialDensity, pool, cmbRuleset.SelectedItem.ToString()) { RunParallel = chkParallel.Checked };
// Run the game on a background thread
Task.Factory.StartNew(() =>
{
// Run until cancellation is requested
var sw = new Stopwatch();
while (!token.IsCancellationRequested)
{
// Move to the next board, timing how long it takes
sw.Restart();
Bitmap bmp = _game.MoveNext();
var framesPerSecond = 1 / sw.Elapsed.TotalSeconds;
// Update the UI with the new board image
BeginInvoke((Action)(() =>
{
lblFramesPerSecond.Text = String.Format("Frames / Sec: {0:F2}", framesPerSecond);
var old = (Bitmap)pictureBox1.Image;
pictureBox1.Image = bmp;
if (old != null) pool.PutObject(old);
}));
}
// When the game is done, reset the board.
}, token).ContinueWith(_ =>
{
_cancellation = null;
btnRun.Text = "Start";
lblDensity.Visible = true;
tbDensity.Visible = true;
}, TaskScheduler.FromCurrentSynchronizationContext());
}
// If a game is currently running, cancel it
else _cancellation.Cancel();
}
示例4: PutRecyclesObj
public void PutRecyclesObj()
{
var pool = new ObjectPool<DisposedTracking>(() => new DisposedTracking());
var item1 = pool.GetObject();
item1.Use();
pool.PutObject(item1);
item1.Disposed.Should().BeTrue();
}
示例5: ObjectPoolTest
public void ObjectPoolTest() {
var pool = new ObjectPool<int>(() => Rnd.Next(1, 99999));
Parallel.For(0, TestCount, i => pool.PutObject(Rnd.Next(1, 99999)));
Parallel.For(0, TestCount,
i => {
var item = pool.GetObject();
if(IsDebugEnabled)
log.Debug("item=" + item);
});
Assert.AreEqual(0, pool.Count);
}
示例6: Main
static void Main(string[] args)
{
Console.WriteLine("Is Server GC: " + GCSettings.IsServerGC);
GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
Console.WriteLine("Compaction mode: " + GCSettings.LargeObjectHeapCompactionMode);
Console.WriteLine("Latency mode: " + GCSettings.LatencyMode);
GCSettings.LatencyMode = GCLatencyMode.SustainedLowLatency;
Console.WriteLine("New Latency mode: " + GCSettings.LatencyMode);
var pool = new ObjectPool<FakeItem>(() => new FakeItem(), 1000, true);
var watch = Stopwatch.StartNew();
const int Iterations = 10000000;
Action c = () =>
{
for (int i = 0; i < Iterations; i++)
{
var obj = pool.GetObject();
// Thread.SpinWait(rnd.Next(10) + 1);
Thread.SpinWait(4);
pool.PutObject(obj);
}
};
var t1 = Task.Factory.StartNew(c);
var t2 = Task.Factory.StartNew(c);
var t3 = Task.Factory.StartNew(c);
var t4 = Task.Factory.StartNew(c);
var t5 = Task.Factory.StartNew(c);
var t6 = Task.Factory.StartNew(c);
Task.WaitAll(t1, t2, t3, t4, t5, t6);
Console.WriteLine("Completed in " + watch.Elapsed.TotalMilliseconds + "ms");
}
示例7: Main
private static void Main(string[] args)
{
ObjectPool<MyClass> pool = new ObjectPool<MyClass>(() => new MyClass());
Stopwatch watchParallel = new Stopwatch();
watchParallel.Start();
Console.WriteLine("start......");
//Create a high demand for MyClass objects.
Parallel.For(0, 100, (i, loopState) =>
{
MyClass mc = pool.GetObject();
// This is the bottleneck in our application. All threads in this loop
// must serialize their access to the static Console class.
Console.WriteLine("pool 1 :{0:####.##} -> {1} Created: {2}; Added to pool: {3}", mc.GetValue(i), mc.GetValue(0), pool.CreatedObjectsCount, pool.AddedToPoolCount);
pool.PutObject(mc);
});
Console.WriteLine("stop......");
watchParallel.Stop();
Console.WriteLine("Created: {0}; Added to pool: {1}, removed: {2}", pool.CreatedObjectsCount, pool.AddedToPoolCount, pool.RemovedObjectsFromBagCount);
int count = GC.CollectionCount(0);
Console.WriteLine("Сборок мусора " + count + ", время выполнения: " + watchParallel.Elapsed);
Console.WriteLine("Press the Enter key to exit.");
Console.ReadLine();
GC.Collect();
{
Console.WriteLine("start......");
count = GC.CollectionCount(0) - 1;
Stopwatch watchOnlyThread = new Stopwatch();
watchOnlyThread.Start();
for (int i = 0; i < 1000; i++)
{
MyClass mc = pool.GetObject();
// This is the bottleneck in our application. All threads in this loop
// must serialize their access to the static Console class.
Console.WriteLine("pool 2 :{0:####.##} -> {0} -> {1} Created: {2}; Added to pool: {3}", mc.GetValue(i), mc.GetValue(0), pool.CreatedObjectsCount, pool.AddedToPoolCount);
pool.PutObject(mc);
}
watchOnlyThread.Stop();
count = GC.CollectionCount(0) - count;
Console.WriteLine("Сборок мусора " + count + ", время выполнения в одном потоке: " + watchOnlyThread.Elapsed);
Console.WriteLine("pool 1 : Created: {0}; Added to pool: {1}, removed: {2}", pool.CreatedObjectsCount, pool.AddedToPoolCount, pool.RemovedObjectsFromBagCount);
Console.ReadKey();
}
{
Console.ReadKey();
count = GC.CollectionCount(0) - 1;
Stopwatch watchOnlyThread = new Stopwatch();
watchOnlyThread.Start();
for (int i = 0; i < 100; i++)
{
MyClass mc = new MyClass();
// This is the bottleneck in our application. All threads in this loop
// must serialize their access to the static Console class.
Console.WriteLine("{0:####.##} -> {1}", mc.GetValue(i), mc.GetValue(0));
}
watchOnlyThread.Stop();
count = GC.CollectionCount(0) - count;
Console.WriteLine("Сборок мусора " + count + ", время выполнения в одном потоке: " + watchOnlyThread.Elapsed);
}
}