﻿
{"id":4348,"date":"2016-05-23T07:48:32","date_gmt":"2016-05-23T07:48:32","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=4348"},"modified":"2016-05-23T07:48:32","modified_gmt":"2016-05-23T07:48:32","slug":"cta-lock-anahtar-sozcugu","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/cta-lock-anahtar-sozcugu\/","title":{"rendered":"C#&#8217;ta Lock Anahtar S\u00f6zc\u00fc\u011f\u00fc"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>E\u011fer birden fazla Thread \u00fczerinde i\u015flem ger\u00e7ekle\u015ftiriyorsan\u0131z ve bu i\u015flemler k\u00fcmesinde belirli kod alanlar\u0131nda tek Thread olarak i\u015flev yapacaksan\u0131z Lock keyword\u00fcn\u00fc kullanabilirsiniz. Anlayaca\u011f\u0131n\u0131z elinizde \u00e7oklu Thread ak\u0131\u015f\u0131ndaki bir i\u015flev kesitini tek kanala indirgeyerek asl\u0131nda o i\u015flem bitmeden ba\u015fka kanallarda Process s\u00fcrecini durdurman\u0131z\u0131 garanti alt\u0131na alacakt\u0131r. Haliyle kodunuzun sadece o anda \u00e7al\u0131\u015ft\u0131rmak istedi\u011finiz yer d\u0131\u015f\u0131nda Thread&#8217;lar beklemeye al\u0131nacakt\u0131r.<\/p>\n<p>\u00d6rnek olarak a\u015fa\u011f\u0131daki durumu ele alal\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    class Program\r\n    {\r\n        static void X()\r\n        {\r\n            for (int i = 0; i &lt; 50; i++)\r\n            {\r\n                Console.WriteLine(i + &quot; - x |&quot;);\r\n            }\r\n        }\r\n        static void Y()\r\n        {\r\n            for (int i = 0; i &lt; 50; i++)\r\n            {\r\n                Console.WriteLine(i + &quot; - y |&quot;);\r\n            }\r\n        }\r\n        static void Main(string&#x5B;] args)\r\n        {\r\n            Thread t1 = new Thread(new ThreadStart(X));\r\n            Thread t2 = new Thread(new ThreadStart(Y));\r\n            t1.Start();\r\n            t2.Start();\r\n\r\n            Console.Read();\r\n        }\r\n    }\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funu incelerseniz e\u011fer X ve Y ad\u0131nda iki farkl\u0131 metod farkl\u0131 Thread&#8217;lar da ba\u015flat\u0131lmaktad\u0131r. Haliyle bu \u015fekilde bir \u00e7al\u0131\u015fmada a\u015fa\u011f\u0131dakine benzer sonu\u00e7lar elde edilmektedir.<\/p>\n<div id=\"attachment_4351\" style=\"width: 221px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.gencayyildiz.com\/blog\/cta-lock-anahtar-sozcugu\/cta-lock-anahtar-sozcugu\/\" rel=\"attachment wp-att-4351\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4351\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/05\/Cta-Lock-Anahtar-S\u00f6zc\u00fc\u011f\u00fc.jpg\" alt=\"C#&#039;ta Lock Anahtar S\u00f6zc\u00fc\u011f\u00fc\" width=\"211\" height=\"195\" class=\"size-full wp-image-4351\" \/><\/a><p id=\"caption-attachment-4351\" class=\"wp-caption-text\">C#&#8217;ta Lock Anahtar S\u00f6zc\u00fc\u011f\u00fc<\/p><\/div>\n<p>E\u011fer ki biz, farkl\u0131 Thread&#8217;lar da tetiklenmi\u015f bu iki metod i\u00e7erisindeki d\u00f6ng\u00fclerin birinin i\u015flevi bitmeden di\u011ferinin ba\u015flamamas\u0131n\u0131 istiyorsak i\u015fte tamda b\u00f6yle noktada Lock keyword\u00fcn\u00fc devreye sokaca\u011f\u0131z.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    class Program\r\n    {\r\n        static object Kontrol = new object();\r\n        static void X()\r\n        {\r\n            lock (Kontrol)\r\n            {\r\n                for (int i = 0; i &lt; 50; i++)\r\n                {\r\n                    Console.WriteLine(i + &quot; - x |&quot;);\r\n                }\r\n            }\r\n        }\r\n        static void Y()\r\n        {\r\n            lock (Kontrol)\r\n            {\r\n                for (int i = 0; i &lt; 50; i++)\r\n                {\r\n                    Console.WriteLine(i + &quot; - y |&quot;);\r\n                }\r\n            }\r\n        }\r\n        static void Main(string&#x5B;] args)\r\n        {\r\n            Thread t1 = new Thread(new ThreadStart(X));\r\n            Thread t2 = new Thread(new ThreadStart(Y));\r\n            t1.Start();\r\n            t2.Start();\r\n\r\n            Console.Read();\r\n        }\r\n    }\r\n<\/pre>\n<p>\u00c7al\u0131\u015fmam\u0131zda g\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi lock keyword\u00fc ile d\u00f6ng\u00fcler tek Thread garantisine al\u0131nm\u0131\u015ft\u0131r. Sonu\u00e7 olarak a\u015fa\u011f\u0131dakine benzer bir \u00e7\u0131kt\u0131 al\u0131nacakt\u0131r.<\/p>\n<div id=\"attachment_4355\" style=\"width: 112px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.gencayyildiz.com\/blog\/cta-lock-anahtar-sozcugu\/cta-lock-anahtar-sozcugu-2\/\" rel=\"attachment wp-att-4355\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4355\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/05\/Cta-Lock-Anahtar-S\u00f6zc\u00fc\u011f\u00fc-1.jpg\" alt=\"C#&#039;ta Lock Anahtar S\u00f6zc\u00fc\u011f\u00fc\" width=\"102\" height=\"245\" class=\"size-full wp-image-4355\" \/><\/a><p id=\"caption-attachment-4355\" class=\"wp-caption-text\">Bir d\u00f6ng\u00fc son bulunca di\u011feri ba\u015flamaktad\u0131r.<\/p><\/div>\n<p>lock keyword\u00fcn\u00fcn kullan\u0131m senaryolar\u0131n\u0131 \u00e7o\u011faltabiliriz. Lakin en \u00e7ok tercih edilen noktalardan birisi Singleton tasar\u0131m desenini uygulad\u0131\u011f\u0131m\u0131z s\u0131n\u0131flard\u0131r.<\/p>\n<p>A\u015fa\u011f\u0131daki kod blo\u011funu inceleyiniz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    class MyClass\r\n    {\r\n        MyClass() { }\r\n        static private MyClass nesne;\r\n\r\n        static public MyClass NesneVer\r\n        {\r\n            get\r\n            {\r\n                if (nesne == null)\r\n                {\r\n                    nesne = new MyClass();\r\n                    Console.WriteLine(nameof(MyClass) + &quot; nesnesi \u00fcretilmi\u015ftir.&quot;);\r\n                }\r\n                Console.WriteLine(&quot;Var olan &quot; + nameof(MyClass) + &quot; nesnesi g\u00f6nderilmi\u015ftir.&quot;);\r\n                return nesne;\r\n            }\r\n        }\r\n    }\r\n<\/pre>\n<p>MyClass isimli s\u0131n\u0131f\u0131m\u0131za Singleton Design Pattern uygulanarak ilgili s\u0131n\u0131ftan talepler tek bir \u00f6rne\u011fe ayarlanm\u0131\u015ft\u0131r.<\/p>\n<p>MyClass s\u0131n\u0131f\u0131ndan nesne talebimizi a\u015fa\u011f\u0131daki gibi yaparsak e\u011fer&#8230;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    class Program\r\n    {\r\n        delegate void ASenkronMetodHandler();\r\n        static void Main(string&#x5B;] args)\r\n        {\r\n            ASenkronMetodHandler metod = () =&gt;\r\n            {\r\n                MyClass Nesne = MyClass.NesneVer;\r\n            };\r\n\r\n            metod.BeginInvoke(new AsyncCallback((x) =&gt; { }), new object());\r\n            metod.BeginInvoke(new AsyncCallback((x) =&gt; { }), new object());\r\n            metod.BeginInvoke(new AsyncCallback((x) =&gt; { }), new object());\r\n            metod.BeginInvoke(new AsyncCallback((x) =&gt; { }), new object());\r\n            metod.BeginInvoke(new AsyncCallback((x) =&gt; { }), new object());\r\n\r\n            Console.Read();\r\n        }\r\n    }\r\n<\/pre>\n<p><a href=\"http:\/\/www.gencayyildiz.com\/blog\/cta-lock-anahtar-sozcugu\/cta-lock-anahtar-sozcugu-3\/\" rel=\"attachment wp-att-4357\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/05\/Cta-Lock-Anahtar-S\u00f6zc\u00fc\u011f\u00fc-2.jpg\" alt=\"C#&#039;ta Lock Anahtar S\u00f6zc\u00fc\u011f\u00fc\" width=\"347\" height=\"181\" class=\"aligncenter size-full wp-image-4357\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/05\/Cta-Lock-Anahtar-S\u00f6zc\u00fc\u011f\u00fc-2.jpg 347w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/05\/Cta-Lock-Anahtar-S\u00f6zc\u00fc\u011f\u00fc-2-300x156.jpg 300w\" sizes=\"auto, (max-width: 347px) 100vw, 347px\" \/><\/a><\/p>\n<p>Sonucuna benzer \u00e7\u0131kt\u0131lar elde edece\u011fiz.<\/p>\n<p>Eee! Hani MyClass&#8217;a Singleton tasar\u0131m\u0131n\u0131 uygulam\u0131\u015ft\u0131k. Burada iki adet nesne olu\u015fturulmu\u015f!<\/p>\n<p>Mesele burada iki adet nesnenin olu\u015fturulmas\u0131 de\u011fil, Singleton paternine ra\u011fmen birden fazla nesnenin olu\u015fturulabilme ihtimalidir. Bunun sebebi, Singleton tasar\u0131m\u0131n\u0131 uygulad\u0131\u011f\u0131m\u0131z s\u0131n\u0131fa yap\u0131lan talepler farkl\u0131 Threadlar \u00fczerinden ger\u00e7ekle\u015ftiriliyorsa e\u011fer &#8216;nesne == null&#8217; kontrol\u00fcne T zaman\u0131nda birden fazla(n adet) talep ula\u015fabilmekte(ihtimal) ve bu e\u015fzamanl\u0131 ula\u015fan taleplerin her biri &#8216;true&#8217; olarak if ko\u015fullar\u0131ndan ge\u00e7erek nesne \u00fcretim scopeuna eri\u015febilmektedirler. Daha do\u011fru bir ifadeyle eri\u015febilme ihtimaline sahiptirler.<\/p>\n<p>\u0130\u015fte bu y\u00fczden MyClass s\u0131n\u0131f\u0131na gelecek her talep neticesinde lock keyword\u00fc ile i\u015flemlerimizi tek Thread&#8217;a indirgemeli ve \u00e7al\u0131\u015fma mant\u0131\u011f\u0131m\u0131z\u0131 garanti alt\u0131na almal\u0131y\u0131z.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    class MyClass\r\n    {\r\n        MyClass() { }\r\n        static private MyClass nesne;\r\n        static object Kontrol = new object();\r\n        static public MyClass NesneVer\r\n        {\r\n            get\r\n            {\r\n                lock (Kontrol)\r\n                {\r\n                    if (nesne == null)\r\n                    {\r\n                        nesne = new MyClass();\r\n                        Console.WriteLine(nameof(MyClass) + &quot; nesnesi \u00fcretilmi\u015ftir.&quot;);\r\n                    }\r\n                    Console.WriteLine(&quot;Var olan &quot; + nameof(MyClass) + &quot; nesnesi g\u00f6nderilmi\u015ftir.&quot;);\r\n                }\r\n                return nesne;\r\n            }\r\n        }\r\n    }\r\n<\/pre>\n<p>G\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi MyClass&#8217;da lock keyword\u00fc ile nesne kontrol ve \u00fcretim s\u00fcrecinde ba\u015fka Thread&#8217;lar\u0131n \u00e7al\u0131\u015fmas\u0131n\u0131 durduruyoruz.<\/p>\n<p>Yap\u0131lan bu g\u00fcncelleme neticesinde a\u015fa\u011f\u0131daki sonucu \u00e7\u0131kt\u0131 olarak elde ediyoruz.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/cta-lock-anahtar-sozcugu\/cta-lock-anahtar-sozcugu-4\/\" rel=\"attachment wp-att-4362\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/05\/Cta-Lock-Anahtar-S\u00f6zc\u00fc\u011f\u00fc-3.jpg\" alt=\"C#&#039;ta Lock Anahtar S\u00f6zc\u00fc\u011f\u00fc\" width=\"337\" height=\"141\" class=\"aligncenter size-full wp-image-4362\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/05\/Cta-Lock-Anahtar-S\u00f6zc\u00fc\u011f\u00fc-3.jpg 337w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/05\/Cta-Lock-Anahtar-S\u00f6zc\u00fc\u011f\u00fc-3-300x126.jpg 300w\" sizes=\"auto, (max-width: 337px) 100vw, 337px\" \/><\/a><\/p>\n<p>Her bir yap\u0131 farkl\u0131 bir yap\u0131n\u0131n yahut organizasyonun olas\u0131 durumlar\u0131na hitap edebilecek tarzda geli\u015ftirilmektedir. \u0130\u015fte lock keyword\u00fc bu duruma \u00e7ok net bir \u00f6rnektir. Uygun zamanlarda kullanman\u0131n performans ve bak\u0131m a\u00e7\u0131s\u0131ndan maliyeti ne kadar azaltt\u0131\u011f\u0131 a\u015fikard\u0131r.<\/p>\n<p>Sonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar dilerim&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, E\u011fer birden fazla Thread \u00fczerinde i\u015flem ger\u00e7ekle\u015ftiriyorsan\u0131z ve bu i\u015flemler k\u00fcmesinde belirli kod alanlar\u0131nda tek Thread olarak i\u015flev yapacaksan\u0131z Lock keyword\u00fcn\u00fc kullanabilirsiniz. Anlayaca\u011f\u0131n\u0131z elinizde \u00e7oklu Thread ak\u0131\u015f\u0131ndaki bir i\u015flev kesitini tek kanala indirgeyerek&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":3223,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[1205,1206,1207,154,606,1208],"class_list":["post-4348","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-sharp-c","tag-lock","tag-lock-anahtar-sozcugu","tag-lock-keyword","tag-singleton","tag-singleton-design-pattern","tag-singleton-tasarim-deseni"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/4348","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=4348"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/4348\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/3223"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=4348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=4348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=4348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}