Added advanced multithreading examples including async/await, tasks, race conditions, and cancellation handling
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
// using System;
|
||||
// using System.Threading.Tasks;
|
||||
//
|
||||
// namespace Multithreading
|
||||
// {
|
||||
// internal static class Program
|
||||
// {
|
||||
// public static void Main(string[] args)
|
||||
// {
|
||||
// Console.WriteLine("Program started");
|
||||
//
|
||||
// Task<int> randomNumberTask = Task.Run(() =>
|
||||
// {
|
||||
// Console.WriteLine($"Thread Id: {Thread.CurrentThread.ManagedThreadId}");
|
||||
// var random = new Random();
|
||||
// return random.Next();
|
||||
// });
|
||||
//
|
||||
// randomNumberTask.Wait(); // this will pause our main thread from running
|
||||
// int result = randomNumberTask.Result;
|
||||
// Console.WriteLine($"Random number: {result}");
|
||||
//
|
||||
// GetRandomNumberAsync().Wait();
|
||||
//
|
||||
// Console.WriteLine("Program ended");
|
||||
// }
|
||||
//
|
||||
// private static async Task<int> GetRandomNumberAsync()
|
||||
// {
|
||||
// Task<int> randomNumberTask = Task.Run(() =>
|
||||
// {
|
||||
// Console.WriteLine($"Thread Id: {Thread.CurrentThread.ManagedThreadId}");
|
||||
// var random = new Random();
|
||||
// return random.Next();
|
||||
// });
|
||||
//
|
||||
// int result = await randomNumberTask;
|
||||
//
|
||||
// return result;
|
||||
// }
|
||||
//
|
||||
// private static async Task SleepAsync(int seconds)
|
||||
// {
|
||||
// Console.WriteLine($"Thread Id before sleep: {Thread.CurrentThread.ManagedThreadId}");
|
||||
// await Task.Delay(TimeSpan.FromSeconds(seconds));
|
||||
// Console.WriteLine($"Thread Id before sleep: {Thread.CurrentThread.ManagedThreadId}");
|
||||
//
|
||||
// // GameObject[] prefabs;
|
||||
// // for (int i = 0; i < prefabs.Length; i++)
|
||||
// // {
|
||||
// // Instantiate(prefabs[i]);
|
||||
// // if (i % 10 == 0)
|
||||
// // {
|
||||
// // // update ui, e.g. progress bar
|
||||
// // await Task.Delay(100);
|
||||
// // }
|
||||
// // }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
Reference in New Issue
Block a user