多线程编程小试

it2022-05-09  28

Test1 MyThread  1using System; 2using System.Collections.Generic; 3using System.Text; 4 5namespace multithreadedprograming 6{ 7    public class MyThread 8    { 9        public static void Thread1()10        {11            for (int i = 0; i < 10; i++)12            {13                Console.WriteLine("Thread1 {0}", i);14            }15        }1617        public static void Thread2()18        {19            for (int i = 0; i < 10; i++)20            {21                Console.WriteLine("Thread2 {0}", i);22            }23        }24    }25} Main  1using System; 2using System.Collections.Generic; 3using System.Text; 4 5using System.Threading; 6 7namespace multithreadedprograming 8{ 9    class Program10    {11        static void Main(string[] args)12        {13            Console.WriteLine("Before start thread");1415            Thread tid1 = new Thread(new ThreadStart(MyThread.Thread1));16            Thread tid2 = new Thread(new ThreadStart(MyThread.Thread2));1718            tid1.Start();19            tid2.Start();2021            Console.ReadKey();22        }23    }24} test2 MyThread  1using System; 2using System.Collections.Generic; 3using System.Text; 4 5using System.Threading; 6 7namespace multithreadedprograming 8{ 9    public class MyThread10    {11        public void Thread1()12        {13            for (int i = 0; i < 10; i++)14            {1516                int iHour = 0;17                int iMin = 0;18                int iSec = 1;1920                Console.WriteLine("Hello world " + i);21                Thread.Sleep(new TimeSpan(iHour, iMin, iSec));22            }23        }24    }25} Test3 MyThread  1using System; 2using System.Collections.Generic; 3using System.Text; 4 5using System.Threading; 6using System.Security; 7 8namespace multithreadedprograming 9{10    public class MyThread11    {12        public void Thread1()13        {14            for (int i = 0; i < 10; i++)15            {1617                Thread thr = Thread.CurrentThread;18                Console.WriteLine(thr.Name + "=" + i);1920                try21                {22                    Thread.Sleep(1);23                }24                catch (ArgumentException ae)25                {26                    Console.WriteLine(ae.ToString());27                }28                catch (ThreadInterruptedException tie)29                {30                    Console.WriteLine(tie.ToString());31                }32                catch (SecurityException se)33                {34                    Console.WriteLine(se.ToString());35                }36            }37        }38    }39} Main  1using System; 2using System.Collections.Generic; 3using System.Text; 4 5using System.Threading; 6 7namespace multithreadedprograming 8{ 9    class Program10    {11        static void Main(string[] args)12        {13         Console.WriteLine("Before start thread");1415                MyThread thr1 = new MyThread();16                MyThread thr2 = new MyThread();1718                Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );19                Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );2021                tid1.Name = "Thread 1";22                tid2.Name = "Thread 2";2324                try {25                        tid1.Start();26                        tid2.Start();27                }28                catch (ThreadStateException te) {29                        Console.WriteLine(te.ToString() );30                }3132                tid1.Join();//加入当前线程,直到当前线程结束才也结束。33                //tid2.Join();//不加入当前线程3435                //Thread.Sleep(10);3637                Console.WriteLine("End of Main");3839            Console.ReadKey();40        }41    }42} Test4 ManualResetEvent MyThread  1using System; 2using System.Collections.Generic; 3using System.Text; 4 5using System.Threading; 6using System.Security; 7 8namespace multithreadedprograming 9{10    public class MyThread11    {12        ManualResetEvent mre = new ManualResetEvent(false);1314        public ManualResetEvent MRE15        {16            get17            {18                return mre;19            }20        }2122        public void Thread1()23        {24            mre.WaitOne();2526            Console.WriteLine("Receive a envent");2728            for (int i = 0; i < 10; i++)29            {3031                Thread thr = Thread.CurrentThread;32                Console.WriteLine(thr.Name + "=" + i);3334                try35                {36                    Thread.Sleep(1);37                }38                catch (ArgumentException ae)39                {40                    Console.WriteLine(ae.ToString());41                }42                catch (ThreadInterruptedException tie)43                {44                    Console.WriteLine(tie.ToString());45                }46                catch (SecurityException se)47                {48                    Console.WriteLine(se.ToString());49                }50            }51        }52    }53} Main  1using System; 2using System.Collections.Generic; 3using System.Text; 4 5using System.Threading; 6 7namespace multithreadedprograming 8{ 9    class Program10    {11        static void Main(string[] args)12        {13            Console.WriteLine("Before start thread");1415            MyThread thr1 = new MyThread();1617            Thread tid1 = new Thread(new ThreadStart(thr1.Thread1));1819            tid1.Name = "Thread 1";2021            try22            {23                tid1.Start();24            }25            catch (ThreadStateException te)26            {27                Console.WriteLine(te.ToString());28            }29            Console.WriteLine("Main Thread is live? {0}", Thread.CurrentThread.IsAlive);3031            Console.WriteLine("End of Main");3233            thr1.MRE.Set();3435            Console.ReadKey();36        }37    }38}

转载于:https://www.cnblogs.com/nanshouyong326/archive/2007/04/19/719581.html

相关资源:数据结构—成绩单生成器

最新回复(0)