当前位置: 首页>>代码示例>>C#>>正文


C# ObjectPool.PutObject方法代码示例

本文整理汇总了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();
        }
开发者ID:HawkingChan,项目名称:TaskAsyncProject,代码行数:30,代码来源:Program.cs

示例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();
        }
开发者ID:danielmarbach,项目名称:RabbitMqNext,代码行数:19,代码来源:ObjectPoolTestCase.cs

示例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();
        }
开发者ID:kunalgujar,项目名称:GameOfLife,代码行数:56,代码来源:MainForm.cs

示例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();
        }
开发者ID:danielmarbach,项目名称:RabbitMqNext,代码行数:11,代码来源:ObjectPoolTestCase.cs

示例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);
        }
开发者ID:debop,项目名称:NFramework,代码行数:15,代码来源:ObjectPoolTestCase.cs

示例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");
        }
开发者ID:danielmarbach,项目名称:RabbitMqNext,代码行数:37,代码来源:Program.cs

示例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);
            }
        }
开发者ID:kalinikoleg,项目名称:Helper,代码行数:80,代码来源:Program.cs


注:本文中的ObjectPool.PutObject方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。