﻿
{"id":4749,"date":"2016-07-13T11:12:27","date_gmt":"2016-07-13T11:12:27","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=4749"},"modified":"2016-07-13T11:12:27","modified_gmt":"2016-07-13T11:12:27","slug":"c-ile-multi-thread-programlamada-system-threading-thread-sinifi","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/c-ile-multi-thread-programlamada-system-threading-thread-sinifi\/","title":{"rendered":"C# \u0130le Multi Thread Programlamada System.Threading.Thread S\u0131n\u0131f\u0131"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Multi Thread Programlama Yaz\u0131 Dizisinin art\u0131k programatik k\u0131sm\u0131na gelmi\u015f bulunmaktay\u0131z. .NET platformunda yap\u0131lacak olan t\u00fcm Multi Thread programlama i\u015flemlerini\u00a0<em><strong>System.Threading.Thread<\/strong><\/em> s\u0131n\u0131f\u0131 ile ger\u00e7ekle\u015ftirmekteyiz. Haliyle bu i\u00e7eri\u011fimizde bu s\u0131n\u0131f\u0131 a&#8217;dan z&#8217;ye inceleyece\u011fiz.<\/p>\n<h3><span style=\"color: #800080;\">Thread Yarat\u0131lmas\u0131<\/span><\/h3>\n<p>Thread s\u0131n\u0131f\u0131ndan \u00fcretilen her nesne ile aslen bir thread olu\u015fturmu\u015f oluyoruz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        private void Form1_Load(object sender, EventArgs e)\r\n        {\r\n            Thread t1 = new Thread(Fonksiyon);\r\n        }\r\n\r\n        void Fonksiyon() {\r\n            int Sayac = 0;\r\n            while (true)\r\n            {\r\n                Sayac++;\r\n                this.Text = Sayac.ToString();\r\n            }\r\n        }\r\n<\/pre>\n<p>G\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi Fonksiyon isimli metodu t1 referansl\u0131 bir Thread nesnesi arac\u0131l\u0131\u011f\u0131yla farkl\u0131 bir thread da \u00e7al\u0131\u015ft\u0131raca\u011f\u0131z.<\/p>\n<h4><span style=\"color: #0000ff;\">Start Metodu<\/span><\/h4>\n<p>Olu\u015fturdu\u011fumuz Thread&#8217;\u0131 \u00e7al\u0131\u015ft\u0131racak, bir ba\u015fka deyi\u015fle i\u015fleyecek metotdur.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n            Thread t1 = new Thread(Fonksiyon);\r\n            t1.Start();\r\n<\/pre>\n<h4><span style=\"color: #0000ff;\">Abort Metodu<\/span><\/h4>\n<p>\u00c7al\u0131\u015fan Thread&#8217;\u0131n programatik olarak durdurulmas\u0131 yani \u00e7izelgeleme d\u0131\u015f\u0131na \u00e7\u0131kart\u0131lmas\u0131 i\u00e7in kullan\u0131l\u0131r. Lakin \u00e7a\u011fr\u0131ld\u0131\u011f\u0131 anda hemen thread\u0131n \u00e7al\u0131\u015fmas\u0131n\u0131 durdurmaz. Bunun sebebi, Start metodu gibi asenkron bir \u015fekilde \u00e7al\u0131\u015fmas\u0131d\u0131r.<\/p>\n<p>Unutmay\u0131n ki, bir thread\u0131n ger\u00e7ek manada durdurulmas\u0131 i\u00e7in o thread\u0131n \u00fczerinde \u00e7al\u0131\u015ft\u0131r\u0131lan fonksiyonun <strong>Safe Point<\/strong> olarak nitelendirilen g\u00fcvenli bir noktaya ula\u015fmas\u0131 gerekmektedir. Bu nokta genellikle <strong>Garbage Collection<\/strong> mekanizmas\u0131n\u0131n devreye girdi\u011fi and\u0131r.<\/p>\n<p>Abort metodu ile durdurulmu\u015f bir Thread, Start metodu ile tekrardan ba\u015flat\u0131lamaz.<\/p>\n<h4><span style=\"color: #0000ff;\">Suspend ve Resume Metodu<\/span><\/h4>\n<p>Suspend metodu ile Thread ge\u00e7ici olarak durdurulabilir. Yani \u00e7izelgeleme d\u0131\u015f\u0131na \u00e7\u0131kart\u0131labilir. Suspend ile ge\u00e7ici olarak durdurulmu\u015f Thread Resume metodu ile kald\u0131\u011f\u0131 yerden devam ettirilebilir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        Thread t1;\r\n        private void Form1_Load(object sender, EventArgs e)\r\n        {\r\n            t1 = new Thread(Fonksiyon);\r\n            t1.Start();\r\n        }\r\n        void Fonksiyon()\r\n        {\r\n            int Sayac = 0;\r\n            while (true)\r\n            {\r\n                Sayac++;\r\n                this.Text = Sayac.ToString();\r\n            }\r\n        }\r\n        private void btnSuspend_Click(object sender, EventArgs e)\r\n        {\r\n            t1.Suspend();\r\n        }\r\n        private void btnResume_Click(object sender, EventArgs e)\r\n        {\r\n            t1.Resume();\r\n        }\r\n<\/pre>\n<p>Burada dikkatinizi \u00e7ekerim ki, Multi Thread yakla\u015f\u0131mlarda bu iki metodun kullan\u0131m\u0131ndan m\u00fcmk\u00fcn mertebe ka\u00e7\u0131nman\u0131z\u0131 tavsiye ederim. Bunun nedeni, Suspend ile durdurulmu\u015f bir thread\u0131n di\u011fer threadlar ile payla\u015f\u0131ml\u0131 olarak kulland\u0131\u011f\u0131 kaynaklarda \u00e7\u0131kard\u0131\u011f\u0131 <strong>Deadlock<\/strong> diye nitelendirdi\u011fimiz muhtemel sorunlard\u0131r.<\/p>\n<h5><span style=\"color: #666699;\">Deadlock Nedir?<\/span><\/h5>\n<p>Hemen yeri gelmi\u015fken Deadlock \u00fczerine biraz konu\u015fal\u0131m.<br \/>\nDeadlock asl\u0131nda processlerin hi\u00e7bir zaman ula\u015famayacaklar\u0131 bir birim yahut kayna\u011fa ihtiya\u00e7 duymalar\u0131 durumudur. Durum b\u00f6yle olunca processlerin ask\u0131da kalmalar\u0131 diye \u00f6zetleyebiliriz. Yani bir processi ask\u0131da b\u0131rakacak her i\u015flem olas\u0131 deadlock durumuyla kar\u015f\u0131la\u015fmam\u0131z\u0131 sa\u011flayabilir. \u00d6rne\u011fin; bir process bir di\u011fer processin i\u015fini bitirmesini beklerken ayn\u0131 \u015fekilde o processinde di\u011fer processin i\u015fini beklemesi durumudur. Yani \u00e7\u0131kmaza girmektir.<\/p>\n<p>Hani en k\u00f6t\u00fc 90&#8217;l\u0131lar hat\u0131rlarlar. Bilgisayarlar\u0131m\u0131z durmadan tak\u0131l\u0131rd\u0131 da ayak topu\u011fuyla monit\u00f6rden ac\u0131s\u0131n\u0131 \u00e7\u0131kar\u0131rd\u0131k. \u0130\u015fte asl\u0131nda o tak\u0131lma de\u011fil, kilitlenmeydi. O kilitlenme de deadlock&#8217;tan kaynaklanmaktayd\u0131. Deadlock&#8217;tan ka\u00e7\u0131\u015f i\u00e7in onlarca algoritma olmas\u0131na ra\u011fmen bir \u015fekilde yakalan\u0131l\u0131r ve en etkili \u00e7\u00f6z\u00fcm olarak reset tu\u015fu kullan\u0131l\u0131rd\u0131&#8230; \ud83d\ude42<\/p>\n<h4><span style=\"color: #0000ff;\">Sleep Metodu<\/span><\/h4>\n<p>Asl\u0131nda bizlere fazla yabanc\u0131 olmayan Sleep metodu, teknik olarak ald\u0131\u011f\u0131 milisaniye cinsinden de\u011fer kadar thread\u0131 durdurmam\u0131z\u0131 sa\u011flayan(\u00e7izelgeleme d\u0131\u015f\u0131na \u00e7\u0131kartan) static bir metotdur.<\/p>\n<p>Burada dikkat etmeniz gereken nokta, Sleep metoduna verilen s\u00fcre sonunda thread tekrar devreye giremeyebilir. Bunun nedeni, Win32 sistemlerinin Real Time(Ger\u00e7ek Zamanl\u0131) i\u015fletim sistemleri olmay\u0131\u015f\u0131d\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        void Fonksiyon()\r\n        {\r\n            int Sayac = 0;\r\n            while (true)\r\n            {\r\n                Thread.Sleep(1000);\r\n                \/\/Bir saniye boyunca bu thread \u00e7izelgeleme d\u0131\u015f\u0131na \u00e7\u0131kar\u0131lacakt\u0131r.\r\n                Sayac++;\r\n                this.Text = Sayac.ToString();\r\n            }\r\n        }\r\n<\/pre>\n<p>Ayriyetten Sleep metoduna -1 de\u011ferini verirseniz ilgili thread belirsiz s\u00fcreli\u011fine durdulacakt\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n                    Thread.Sleep(new TimeSpan(0, 1, 1));\r\n<\/pre>\n<p>Yukar\u0131daki gibi TimeSpan ile kullan\u0131rsak e\u011fer 0 saat, 1 dakika ve 1 saniye gibisinden zaman zarf\u0131 belirleyebilir, thread\u0131 daha net zamanlayabiliriz.<\/p>\n<p>Ayr\u0131ca Sleep metoduna verilen 0 de\u011feri, s\u00f6z konusu thread\u0131n \u00e7izelgeye girme hakk\u0131n\u0131 di\u011fer threadlara devretmesini sa\u011flar.<\/p>\n<h4><span style=\"color: #0000ff;\">Join Metodu<\/span><\/h4>\n<p>D\u00fc\u015f\u00fcn\u00fcn ki, bir thread\u0131 sonland\u0131rmak istiyorsunuz ama ilgili thread i\u00e7erisinde kullan\u0131lan di\u011fer threadlar\u0131n son bulmas\u0131 \u015fart\u0131yla&#8230;<\/p>\n<p>\u0130\u015fte Join metodu bu i\u015flevi g\u00f6rmektedir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        static void Main(string&#x5B;] args)\r\n        {\r\n            Thread t1 = new Thread(Fonksiyon);\r\n            t1.Start();\r\n\r\n            while (!t1.Join(5000))\r\n                Console.WriteLine(&quot;Denetleniyor&quot;);\r\n            Console.WriteLine(&quot;Denetlenme tamamland\u0131. Thread sonland\u0131.&quot;);\r\n\r\n            Console.Read();\r\n\r\n        }\r\n\r\n        static void Fonksiyon()\r\n        {\r\n            int Sayac = 0;\r\n            while (Sayac &lt; 100)\r\n            {\r\n                Sayac++;\r\n                Thread.Sleep(500);\r\n            }\r\n        }\r\n<\/pre>\n<p>Verilen s\u00fcre periyodunda thread\u0131 kontrol eden ve geriye durumla ilgili bool bir de\u011fer d\u00f6nd\u00fcren Join metodunu yukar\u0131daki gibi while d\u00f6ng\u00fcs\u00fc arac\u0131l\u0131\u011f\u0131yla kullanarak ilgili thread\u0131 takip edebilir ve i\u015flemlerimizi ger\u00e7ekle\u015ftirebiliriz.<\/p>\n<p><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/07\/C-\u0130le-Multi-Thread-Programlamada-System.Threading.Thread-S\u0131n\u0131f\u0131.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-4760\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/07\/C-\u0130le-Multi-Thread-Programlamada-System.Threading.Thread-S\u0131n\u0131f\u0131-300x201.png\" alt=\"C# \u0130le Multi Thread Programlamada System.Threading.Thread S\u0131n\u0131f\u0131\" width=\"300\" height=\"201\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/07\/C-\u0130le-Multi-Thread-Programlamada-System.Threading.Thread-S\u0131n\u0131f\u0131-300x201.png 300w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/07\/C-\u0130le-Multi-Thread-Programlamada-System.Threading.Thread-S\u0131n\u0131f\u0131.png 338w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h4><span style=\"color: #000080;\">ThreadState Propertysi<\/span><\/h4>\n<p>\u0130lgili thread\u0131n herhangi bir T an\u0131ndaki durumunu bizlere vermektedir.<\/p>\n<h4><span style=\"color: #000080;\">Priority Propertysi<\/span><\/h4>\n<p>Thread\u0131n \u00f6ncelik d\u00fczeyinin belirlenmesini sa\u011flar. Varsay\u0131lan olarak Normal de\u011ferine sahiptir.<\/p>\n<h4><span style=\"color: #000080;\">IsAlive Propertysi<\/span><\/h4>\n<p>Thread\u0131n herhangi bir T an\u0131nda aktiflik durumunu verir.<\/p>\n<p>Bu son sat\u0131rlada Multi Thread i\u015flemleri ger\u00e7ekle\u015ftirmemizi sa\u011flayan Thread s\u0131n\u0131f\u0131n\u0131 bitirmi\u015f bulunmaktay\u0131z. Sonraki yaz\u0131lar\u0131m\u0131zda seriye katk\u0131da bulunacak bir \u00e7ok konuyu ele alacak ve i\u00e7eriklerini net bir \u015fekilde olu\u015fturaca\u011f\u0131z&#8230;<\/p>\n<p>\u015eimdilik g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<\/p>\n<p>\u0130yi \u00e7al\u0131\u015fmalar&#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, Multi Thread Programlama Yaz\u0131 Dizisinin art\u0131k programatik k\u0131sm\u0131na gelmi\u015f bulunmaktay\u0131z. .NET platformunda yap\u0131lacak olan t\u00fcm Multi Thread programlama i\u015flemlerini\u00a0System.Threading.Thread s\u0131n\u0131f\u0131 ile ger\u00e7ekle\u015ftirmekteyiz. Haliyle bu i\u00e7eri\u011fimizde bu s\u0131n\u0131f\u0131 a&#8217;dan z&#8217;ye inceleyece\u011fiz. Thread Yarat\u0131lmas\u0131 Thread&#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":[1365,1364,1372,1373,1367,611,1383,1382,233,1377,1330,1381,1380,1376,1370,1371,1366,1375,1374,1363,1362,1369,1368,1358,1359,1360,1361,1379,1378],"class_list":["post-4749","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-sharp-c","tag-abort","tag-abort-metodu","tag-deadlock","tag-deadlock-nedir","tag-garbage-collection","tag-gercek-zamanli","tag-isalive","tag-isalive-propertysi","tag-join","tag-join-metodu","tag-multi-thread","tag-priority","tag-priority-propertysi","tag-real-time","tag-resume","tag-resume-metodu","tag-safe-point","tag-sleep","tag-sleep-metodu","tag-start","tag-start-metodu","tag-suspend","tag-suspend-metodu","tag-system-threading-thread","tag-system-threading-thread-sinifi","tag-thread-sinifi","tag-thread-yaratilmasi","tag-threadstate","tag-threadstate-propertysi"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/4749","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=4749"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/4749\/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=4749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=4749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=4749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}