Created
December 27, 2020 11:31
-
-
Save ierhalim/90588e97650231a2ea1870143a3ad9ec to your computer and use it in GitHub Desktop.
Y2O2 Paralel programlama
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Threading.Tasks; | |
| namespace DataSharing | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| int number = 0; | |
| var tasks = new List<Task>(); | |
| object locker = new object(); | |
| for (int i = 0; i < 15; i++) | |
| { | |
| tasks.Add(Task.Factory.StartNew(() => | |
| { | |
| for (int j = 0; j < 20; j++) | |
| { | |
| lock (locker) | |
| { | |
| number++; | |
| } | |
| } | |
| })); | |
| tasks.Add(Task.Factory.StartNew(() => | |
| { | |
| for (int j = 0; j < 20; j++) | |
| { | |
| lock (locker) | |
| { | |
| number--; | |
| } | |
| } | |
| })); | |
| } | |
| Task.WaitAll(tasks.ToArray()); | |
| Console.WriteLine(number); | |
| Console.ReadKey(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment