using System; using System.Collections.Concurrent; using System.Threading; using System.Threading.Tasks; namespace ObjectPoolExample { public class ObjectPool<T> { private ConcurrentBag<T> _objects; private Func<T> _objectGenerator; public ObjectPool(Func<T> objectGenerator) { if (objectGenerator == null) throw new ArgumentNullException("objectGenerator"); _objects = new ConcurrentBag<T>(); _objectGenerator = objectGenerator; } public T GetObject() { T item; if (_objects.TryTake(out item)) return item; return _objectGenerator(); } public void PutObject(T item) { _objects.Add(item); } } 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, 1000, (i, loopState) => { MyClass mc = pool.GetObject(); 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(); } } // A toy class that requires some resources to create. // You can experiment here to measure the performance of the // object pool vs. ordinary instantiation. class MyClass { public int[] Nums {get; set;} public double GetValue(long i) { return Math.Sqrt(Nums[i]); } public MyClass() { Nums = new int[1000]; for (int i = 0; i < Nums.Length; i++) } } }
Standard input is empty
32774 30189 35649 42693 21091 39960 35039 30722 20000 10618 30262 31300 17742 17465 45273 24467 37087 43684 39343 28512 37492 45147 25098 16517 31890 7362 36953 14947 26550 18617 35731 41226 19312 8583 41157 12251 19522 33066 27427 27622 13806 46330 35527 27159 19025 32949 29852 37060 25245 14471 11890 34177 34073 16409 35579 25185 21077 31592 28386 19764 15210 31691 15458 7309 31361 36929 24632 15528 27543 43584 14748 16796 34000 28016 24947 37506 27858 45164 45369 45594 36202 46255 41636 22259 14325 10423 23207 10183 30908 32552 43050 39123 16958 19016 23058 36521 43675 34767 43607 33849 21884 25495 24795 29753 46254 44797 3466 19393 42360 44796 44785 23493 32973 29571 35036 15469 37659 43485 44673 29579 28725 22428 37859 42971 6799 28886 46282 28185 15914 37895 40579 41410 11758 30209 39999 25701 39048 29126 22441 18600 9830 12747 21373 33108 34661 36075 21057 39505 35799 17099 45421 4071 36886 32041 17905 46080 36935 30262 28966 36124 44857 36947 11083 18930 18973 17056 20339 13214 37710 23811 29360 23872 37247 40617 27898 25742 6800 18363 25398 29871 19775 24112 40747 45917 38993 40374 18821 35484 24331 40299 37998 24676 3053 36689 21937 29648 6298 9923 27132 30174 29795 16394 11773 26732 39722 38642 27869 43108 15759 42490 45575 32047 16454 45762 30231 37826 43038 12727 31390 29620 45653 36084 41594 44107 35645 15909 23676 6425 34183 45241 24960 45774 41815 43383 2141 11058 21043 30752 23033 5225 29213 25366 31792 30563 41163 20418 18390 7912 20788 34611 34267 45002 35505 44791 31206 41631 26195 18456 39894 36397 30539 27119 26265 18684 34386 46145 37762 23155 45712 28136 31432 30110 42177 30952 4895 37957 17068 28214 15845 29218 45414 22315 41912 46022 29539 27300 28891 43471 30137 20468 39795 10109 38751 36786 35244 10862 45837 25723 43579 41383 33740 44153 10976 42285 14491 16359 15692 17394 44523 40995 37971 45140 9463 27163 37548 20849 33560 26820 40299 20908 35982 41033 42136 40874 36818 29762 39401 20633 43167 45315 38564 42156 32317 26192 43160 10626 19429 46001 43681 24596 22339 23067 45331 37083 33535 41152 3555 27638 45726 28417 33751 37172 34426 38236 20442 40027 13193 24439 20795 38362 29372 44362 24222 45567 13907 42569 31516 39383 31977 36013 7563 34025 30964 31923 45668 27615 42146 20007 23368 42539 29549 28081 21962 32494 30237 10267 24331 41591 16375 16143 45676 43905 41032 20889 43471 26048 45748 46109 34377 38209 26461 24680 43460 28177 27392 46141 20566 12887 39037 42459 44422 43832 7156 27756 24465 8611 38545 40967 34089 9381 34931 37562 35817 27603 36852 16643 40268 22857 35733 37511 28586 42400 36959 35123 33307 16249 32361 42847 17480 30542 42778 20638 14351 24756 20850 12847 43318 30220 44557 42609 36610 20361 31048 33072 15485 37905 38389 33569 39791 21756 22526 45255 32198 10807 31394 13251 23565 31066 42527 18289 34270 42395 37051 15140 20139 28182 34712 25884 18089 34541 23410 26613 26166 40109 8174 20064 28875 16500 31525 42662 30624 45473 15892 44494 20625 43925 39200 9442 42128 16097 41919 25321 41115 34486 27107 26233 39913 15227 28350 28278 19754 45528 44121 36924 39692 7070 24146 38592 16824 46030 46216 39296 35413 39304 39249 44174 24973 37484 33403 32292 8252 39791 42239 40043 34370 31106 22889 36889 9948 41612 39348 39852 11580 35611 44831 45326 25024 23791 45920 6259 34521 30287 38395 30661 35550 10326 39173 12068 42382 30512 8034 13710 35547 43686 27048 30893 16990 18526 13493 24655 40909 37010 37288 26438 23886 23405 37551 46189 41018 40417 36402 7592 37999 32812 44705 36032 10447 29052 18431 32783 39756 19448 41376 36269 40829 36882 18017 19316 23164 32487 42021 16822 19595 38740 22651 44379 40919 27843 19224 26758 29052 39352 24712 12755 41873 2627 29859 13674 45359 39759 29073 39901 1872 35424 34275 35991 42488 8655 34343 36848 14939 32202 39961 25041 44892 32282 42675 45723 23738 5391 42088 38489 21783 42086 15188 5655 43761 39361 41347 40744 21650 11732 21522 33216 14497 13178 16718 35730 18055 36949 12815 39994 41752 34048 22200 44211 45817 46164 17172 26669 18255 30797 42083 45037 44237 38876 33431 19488 32003 33805 39087 15998 22645 40827 29817 9247 23953 22216 20418 42914 27616 23335 42007 36685 44859 13110 43424 38245 35754 20581 8958 43948 35418 31597 36426 40838 22676 24131 35971 29092 34692 45246 15888 36478 44553 45340 32965 8739 16439 20182 37159 40070 41403 24510 45783 44002 36487 20620 4003 42249 42109 42455 35340 24507 32597 41759 33897 40766 18439 36996 41459 46248 36704 41449 25261 29377 17091 20831 38837 45870 35109 42536 34532 29513 34500 24942 14738 25567 32401 39220 18344 9434 23797 40698 34938 39352 28253 42418 16374 15529 30112 39652 43403 32388 28913 37274 16077 46300 45936 35115 40547 29956 32625 7710 23502 45949 24698 19440 39469 20988 39072 22256 24740 30811 32129 40371 39656 42875 22302 35172 8047 25304 12878 35597 34602 42519 45529 11976 5920 44140 19069 14867 26289 44320 45184 39832 34236 22513 33227 35452 30636 20502 15540 44442 29360 20283 30987 30571 4095 36792 44320 23826 40574 29547 34181 31683 27046 44099 31774 29865 6195 21600 18516 41916 8767 36133 44178 28201 32484 39914 21395 32646 27097 37256 17276 42331 19969 18391 20128 28034 29115 33740 11254 28790 46169 44084 26874 44334 28212 32215 29243 37079 40555 14563 34907 21726 8803 25140 43569 41278 24641 12841 45021 23899 29932 29974 44507 16071 23970 39037 24597 19831 39238 14956 6654 32365 18437 40761 42105 16760 24152 46192 46334 4170 27166 35387 40630 41267 45612 35116 45935 21540 37296 37676 20803 21491 44528 37697 12954 13076 11494 23738 31728 40216 33475 24919 12864 44827 44505 42580 38438 38193 9422 27352 44238 28953 44196 26284 6702 38846 44000 28007 41575 23231 17842 10926 27253 26970 41618 16617 37351 12143 18825 46325 42916 33925 30126 33431 43308 41295 22744 37376 43241 16353 43626 25521 19370 25354 40125 41737 25614 35943 37096 28396 19557 38668 31345 21610 36042 31642 45008 39820 16655 41286 20443 36469 33232 9499 35077 39604 22851 13020 44429 41405 43752 24238 29133 13370 26130 35482 Press the Enter key to exit.