Skip to content

Instantly share code, notes, and snippets.

@ierhalim
Last active December 27, 2020 17:41
Show Gist options
  • Select an option

  • Save ierhalim/e628668153b452550e3b9bedaed59262 to your computer and use it in GitHub Desktop.

Select an option

Save ierhalim/e628668153b452550e3b9bedaed59262 to your computer and use it in GitHub Desktop.
Y4O1 Paralel programlama
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ConcurentCollections
{
class Program
{
static void Main(string[] args)
{
var list = new List<int>();
var tasks = new List<Task>();
for (int i = 1; i < 5; i++)
{
// Taskler başlayana kadar döngü biteceği için tüm threadlerde i nin değeri 4 olacaktır, bunu engellemek adına i yi her scope için farklı bir değere atıyoruz.
int copyOfi = i;
tasks.Add(Task.Run(() =>
{
Console.WriteLine("Task {0} Add {1}", Task.CurrentId, copyOfi);
list.Add(copyOfi);
int result = list.Last();
Console.WriteLine("Task {0} Last record {1}", Task.CurrentId, result);
}));
}
Task.WaitAll(tasks.ToArray());
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment