Added Multithreading project with examples and solution configuration
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
// using System;
|
||||
// using System.Threading;
|
||||
//
|
||||
// namespace Multithreading
|
||||
// {
|
||||
// internal static class Program
|
||||
// {
|
||||
// public static void Main(string[] args)
|
||||
// {
|
||||
// Console.WriteLine("Program started");
|
||||
//
|
||||
// // Console.WriteLine($"ThreadId: {Thread.CurrentThread.ManagedThreadId}");
|
||||
// //
|
||||
// // Console.WriteLine("Before sleep");
|
||||
// // Thread.Sleep(5000);
|
||||
// // Console.WriteLine("After sleep");
|
||||
//
|
||||
// // Thread thread = new Thread(() =>
|
||||
// // {
|
||||
// // Console.WriteLine("Thread started");
|
||||
// // // Thread.Sleep(5000);
|
||||
// //
|
||||
// // for (int i = 0; i < 5; i++)
|
||||
// // {
|
||||
// // Console.WriteLine($"Thread ({Thread.CurrentThread.ManagedThreadId}): {i}");
|
||||
// // Thread.Sleep(500);
|
||||
// // }
|
||||
// //
|
||||
// // Console.WriteLine("Thread stopped");
|
||||
// // });
|
||||
// // thread.IsBackground = true;
|
||||
// // thread.Start();
|
||||
// //
|
||||
// // Thread[] threads = new Thread[10];
|
||||
// // for (int i = 0; i < threads.Length; i++)
|
||||
// // {
|
||||
// // threads[i] = new Thread(PrintAndSleep);
|
||||
// // threads[i].IsBackground = true;
|
||||
// // threads[i].Start();
|
||||
// // }
|
||||
// //
|
||||
// // for (int i = 0; i < 5; i++)
|
||||
// // {
|
||||
// // Console.WriteLine($"Main thread: {i}");
|
||||
// // Thread.Sleep(250);
|
||||
// // }
|
||||
// //
|
||||
// // Console.WriteLine($"Thread ({thread.ManagedThreadId}) current state before join: {thread.ThreadState}");
|
||||
// // thread.Join();
|
||||
// // Console.WriteLine($"Thread ({thread.ManagedThreadId}) current state after join: {thread.ThreadState}");
|
||||
// //
|
||||
// // // thread.Start(); // CAUTION: an already started thread can't be started one more time
|
||||
// //
|
||||
// // while (threads[0].ThreadState != ThreadState.Stopped)
|
||||
// // {
|
||||
// // Console.WriteLine($"Thread ({threads[0].ManagedThreadId}): {threads[0].ThreadState}");
|
||||
// // threads[0].Join(50);
|
||||
// //
|
||||
// // // update your ui
|
||||
// // }
|
||||
// //
|
||||
// // for (int i = 0; i < 10; i++)
|
||||
// // {
|
||||
// // Console.WriteLine($"Thread ({threads[i].ManagedThreadId}) current state before join: {threads[i].ThreadState}");
|
||||
// // threads[i].Join(1000);
|
||||
// // Console.WriteLine($"Thread ({threads[i].ManagedThreadId}) current state after join: {threads[i].ThreadState}");
|
||||
// // }
|
||||
// //
|
||||
// // for (int i = 0; i < threads.Length; i++)
|
||||
// // {
|
||||
// // threads[i] = new Thread(PrintAndSleep);
|
||||
// // threads[i].Start();
|
||||
// // }
|
||||
//
|
||||
// Console.WriteLine("----------- THREADPOOL -----------");
|
||||
//
|
||||
// Console.WriteLine($"SetMaxThreads to 1: {ThreadPool.SetMaxThreads(1, 1)}");
|
||||
// Console.WriteLine($"SetMaxThreads to 20: {ThreadPool.SetMaxThreads(20, 20)}");
|
||||
//
|
||||
//
|
||||
// for (int i = 0; i < 10; i++)
|
||||
// {
|
||||
// ThreadPool.QueueUserWorkItem(_ => PrintAndSleep());
|
||||
// }
|
||||
//
|
||||
// Thread.Sleep(4900);
|
||||
//
|
||||
// for (int i = 0; i < 10; i++)
|
||||
// {
|
||||
// ThreadPool.QueueUserWorkItem(_ => PrintAndSleep());
|
||||
// }
|
||||
//
|
||||
// Console.WriteLine("Program ended");
|
||||
// }
|
||||
//
|
||||
// private static void PrintAndSleep()
|
||||
// {
|
||||
// Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId} started");
|
||||
//
|
||||
// for (int i = 0; i < 10; i++)
|
||||
// {
|
||||
// Console.WriteLine($"Thread ({Thread.CurrentThread.ManagedThreadId}): {i}");
|
||||
// Thread.Sleep(500);
|
||||
// }
|
||||
//
|
||||
// Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId} stopped");}
|
||||
// }
|
||||
// }
|
||||
Reference in New Issue
Block a user