Created
October 20, 2017 15:43
-
-
Save Kikimora/3b922b8b9ddd41542cb66ed7ec65a3b4 to your computer and use it in GitHub Desktop.
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.Threading; | |
using System.Linq; | |
using System.Collections.Generic; | |
public class Program | |
{ | |
private static int i = -1; | |
private static List<int> a = new List<int>() {1, 5, 6, 8}; | |
private static List<int> b = new List<int>() {3, 4, 1, 9}; | |
private static List<int> result = new List<int>(Enumerable.Repeat(0, a.Count)); | |
public static void Main(string[] args) | |
{ | |
var threads = Enumerable.Range(0, 3).Select(MakeThread).ToList(); | |
foreach (var t in threads) | |
{ | |
Console.WriteLine("Starting thread"); | |
t.Start(); | |
} | |
foreach (var t in threads) | |
{ | |
t.Join(); | |
} | |
foreach (var a in result) | |
{ | |
Console.Write(a + " "); | |
} | |
} | |
private static Thread MakeThread(int z) | |
{ | |
Console.WriteLine("Making thread"); | |
return new Thread(()=>{ | |
while(i < a.Count - 1) | |
{ | |
var idx = Interlocked.Increment(ref i); | |
result[idx] = a[idx] + b.Sum(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment