|
| 1 | +namespace snippets; |
| 2 | + |
| 3 | +public class SchedulingThreads |
| 4 | +{ |
| 5 | + public void RunMultipleThreadsOnDifferentPriorities() |
| 6 | + { |
| 7 | + var threadsList = new List<Thread>(9); |
| 8 | + |
| 9 | + // Initialize 9 threads. 5 with Highest priority, and the first 4 from Lowest to Normal range. |
| 10 | + for (int i = 0; i < 9; i++) |
| 11 | + { |
| 12 | + var thread = new Thread(() => { new ThreadWithCallback(Callback).Process(); }); |
| 13 | + |
| 14 | + if (i > 3) |
| 15 | + thread.Priority = ThreadPriority.Highest; |
| 16 | + else |
| 17 | + thread.Priority = (ThreadPriority)i; |
| 18 | + |
| 19 | + threadsList.Add(thread); |
| 20 | + } |
| 21 | + |
| 22 | + threadsList.ForEach(thread => thread.Start()); |
| 23 | + } |
| 24 | + |
| 25 | + public void Callback(ThreadPriority threadPriority) |
| 26 | + { |
| 27 | + Console.WriteLine($"Callback in {threadPriority} priority. \t\t ThreadId: {Thread.CurrentThread.ManagedThreadId}."); |
| 28 | + } |
| 29 | + |
| 30 | + public class ThreadWithCallback |
| 31 | + { |
| 32 | + public ThreadWithCallback(Action<ThreadPriority> callback) |
| 33 | + { |
| 34 | + this.callback = callback; |
| 35 | + } |
| 36 | + |
| 37 | + public Action<ThreadPriority> callback; |
| 38 | + |
| 39 | + public void Process() |
| 40 | + { |
| 41 | + Console.WriteLine($"Entered process in {Thread.CurrentThread.Priority} priority. \t\t ThreadId: {Thread.CurrentThread.ManagedThreadId}."); |
| 42 | + Thread.Sleep(1000); |
| 43 | + Console.WriteLine($"Finished process in {Thread.CurrentThread.Priority} priority. \t\t ThreadId: {Thread.CurrentThread.ManagedThreadId}."); |
| 44 | + |
| 45 | + if (callback != null) |
| 46 | + { |
| 47 | + callback(Thread.CurrentThread.Priority); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // The example displays the output like the following: |
| 53 | + // Entered process in Highest priority. ThreadId: 9. |
| 54 | + // Entered process in Highest priority. ThreadId: 12. |
| 55 | + // Entered process in Normal priority. ThreadId: 6. |
| 56 | + // Entered process in BelowNormal priority. ThreadId: 5. |
| 57 | + // Entered process in Lowest priority. ThreadId: 4. |
| 58 | + // Entered process in AboveNormal priority. ThreadId: 7. |
| 59 | + // Entered process in Highest priority. ThreadId: 11. |
| 60 | + // Entered process in Highest priority. ThreadId: 10. |
| 61 | + // Entered process in Highest priority. ThreadId: 8. |
| 62 | + // Finished process in Highest priority. ThreadId: 9. |
| 63 | + // Finished process in Highest priority. ThreadId: 12. |
| 64 | + // Finished process in Highest priority. ThreadId: 8. |
| 65 | + // Finished process in Highest priority. ThreadId: 10. |
| 66 | + // Callback in Highest priority. ThreadId: 10. |
| 67 | + // Finished process in AboveNormal priority. ThreadId: 7. |
| 68 | + // Callback in AboveNormal priority. ThreadId: 7. |
| 69 | + // Finished process in Lowest priority. ThreadId: 4. |
| 70 | + // Callback in Lowest priority. ThreadId: 4. |
| 71 | + // Finished process in Normal priority. ThreadId: 6. |
| 72 | + // Callback in Highest priority. ThreadId: 9. |
| 73 | + // Callback in Highest priority. ThreadId: 8. |
| 74 | + // Callback in Highest priority. ThreadId: 12. |
| 75 | + // Finished process in Highest priority. ThreadId: 11. |
| 76 | + // Callback in Highest priority. ThreadId: 11. |
| 77 | + // Callback in Normal priority. ThreadId: 6. |
| 78 | + // Finished process in BelowNormal priority. ThreadId: 5. |
| 79 | + // Callback in BelowNormal priority. ThreadId: 5. |
| 80 | +} |
0 commit comments