static void Main(string[] args) { FixedThreadPool fixedThreadPool = new FixedThreadPool(10); for (int i = 0; i < 6; i++) { thread hh = new thread("h"); fixedThreadPool.Execute(hh, Priority.High); } for (int i = 0; i < 6; i++) { thread nn = new thread("n"); fixedThreadPool.Execute(nn, Priority.Normal); } for (int i = 0; i < 6; i++) { thread hh = new thread("h"); fixedThreadPool.Execute(hh, Priority.High); } for (int i = 0; i < 6; i++) { thread ll = new thread("l"); fixedThreadPool.Execute(ll, Priority.Low); } fixedThreadPool.Stop(); Console.ReadKey(); }
static void Main(string[] args) { var threadPool = new FixedThreadPool(5); Console.WriteLine("Started"); var random = new Random(); var tasks = new KeyValuePair <Priority, ITask> [100]; for (var i = 0; i < tasks.Length; i++) { var priority = (Priority)random.Next(0, 3); tasks[i] = new KeyValuePair <Priority, ITask>(priority, new Task(i)); Console.WriteLine("Created task {0} with priority {1}", i, priority); } Parallel.ForEach(tasks, new ParallelOptions { MaxDegreeOfParallelism = 100 }, task => { threadPool.Execute(task.Value, task.Key); }); threadPool.Stop(); Console.WriteLine("Stopped"); Console.ReadLine(); }
// ReSharper disable InconsistentNaming static void Main(string[] args) // ReSharper restore InconsistentNaming { var threadPool = new FixedThreadPool(2); Thread.Sleep(5000); threadPool.Execute(new SleepyTask("Task 0 (HIGH)", 5), Priority.High); threadPool.Execute(new SleepyTask("Task 1 (HIGH)", 5), Priority.High); threadPool.Execute(new SleepyTask("Task 2 (HIGH)", 5), Priority.High); threadPool.Execute(new SleepyTask("Task 3 (LOW)", 3), Priority.Low); threadPool.Execute(new SleepyTask("Task 4 (LOW)", 3), Priority.Low); threadPool.Execute(new SleepyTask("Task 5 (NORMAL)", 3), Priority.Normal); threadPool.Execute(new SleepyTask("Task 6 (NORMAL)", 3), Priority.Normal); threadPool.Execute(new SleepyTask("Task 7 (NORMAL)", 3), Priority.Normal); threadPool.Execute(new SleepyTask("Task 8 (NORMAL)", 3), Priority.Normal); threadPool.Execute(new SleepyTask("Task 9 (HIGH)", 3), Priority.High); threadPool.Execute(new SleepyTask("Task 10 (HIGH)", 3), Priority.High); threadPool.Execute(new SleepyTask("Task 11 (HIGH)", 3), Priority.High); threadPool.Execute(new SleepyTask("Task 12 (HIGH)", 3), Priority.High); threadPool.Execute(new SleepyTask("Task 13 (LOW)", 3), Priority.Low); threadPool.Stop(); }