小弟刚学C#。看到有个Thread类。看了MSDN也没怎么看明白,特地来问下:
这个类具体有哪些方法?常用的有哪些?都怎么用?

解决方案 »

  1.   

    多线程同步
    1、lock语句
    2、Mutex互斥对象
    3、
    using System;
    using System.Threading;
    class A
    {
        public int tickes = 100;
        public void Seller()
        {//ticket=1
            while (true )
            {
                lock (this)
                {
                    if (tickes == 0)
                        break;
                    Console.WriteLine("Seller{0}---{1}", Thread.CurrentThread.ManagedThreadId, tickes);
                    tickes--;
                    Thread.Sleep(100);
                }
            }
        }
    }
    class Test
    {
        public static void Main()
        {
            A a = new A();
            Thread t1 = new Thread(new ThreadStart(a.Seller));
            Thread t2 = new Thread(new ThreadStart(a.Seller));
            Thread t3 = new Thread(new ThreadStart(a.Seller));
            Thread t4 = new Thread(new ThreadStart(a.Seller));
            t1.Start();
            t2.Start();
            t3.Start();
            t4.Start();
            Console.Read();
        }
    }
    生产者与消费者同步算法:
    同步信号:=false;(没有生产数据)
    producter
    {
    p();
    if()//如果为真
    生产者等待消费者
    生产数据
    =true;
    通知消费者有数据产生
    v();
    }
    Customer
    {
    p();
    if(!)
    消费者等待生产者
    消费数据;
    =false;
    通知生产者
    v();
    }
    using System.Threading;
    using System;class ProducterCustomer
    {
        int x = 0;
        int sum = 0;
        bool  = false ;//没有数据;
        public void Producter()
        {
            for (int i = 1; i <= 100; i++)
            {
                Monitor.Enter(this);
                {
                    if()
                        Monitor.Wait(this);
                     = true;
                    x = i;
                    Console.WriteLine("producter--x={0}", x);
                    Monitor.Pulse(this);//通知消费者改变状态
                    //Thread.Sleep(5);
                }
                Monitor.Exit(this);
            }
        }
        public void Customer()
        {
            for (int i = 1; i <= 100; i++)
            {
                Monitor.Enter(this);
                {
                    if (!)
                        Monitor.Wait(this);
                     = false;
                    sum += x;
                    Console.WriteLine("Customer--sum={0}", sum);
                    Monitor.Pulse(this);
                    //通知生产者
                    //Thread.Sleep(5);
                }
                Monitor.Exit(this);
            }
        }
    }class Test
    {
        public static void Main()
        {
            ProducterCustomer pc = new ProducterCustomer();
            Thread producter = new Thread(new ThreadStart(pc.Producter));
            Thread customer = new Thread(new ThreadStart(pc.Customer));
            producter.Start();
            customer.Start();
            Console.Read();
        }
    }using System;class AsyncDemo
    {
        //异步方法
        public void Event1()
        {
            Console.WriteLine("Event1 Start");
            System.Threading.Thread.Sleep(4000);
            Console.WriteLine("Event1 End");
        }
        public void Event2()//同步
        {
            Console.WriteLine("Event2 start");
            for (int i = 0; i <= 200; i++)
            {
                Console.WriteLine(i);
            }
            Console.WriteLine("Event2 End");
        }
    }
    class Test
    {
        public delegate void MyDel();
        public static void Main()
        {
            long start = 0;
            long end = 0;
            start = DateTime.Now.Ticks;
            AsyncDemo asy = new AsyncDemo();
            asy.Event1();
            asy.Event2();
            //MyDel del = new MyDel(asy.Event1);
            //IAsyncResult ia = del.BeginInvoke(null, null);//步异调用Event1方法
            //asy.Event2();//启动同步方法
            //del.EndInvoke(ia);//结束异步调用
            end = DateTime.Now.Ticks;
            Console.WriteLine("时间刻度为{0}",end-start);
            Console.Read();
        }
    }
    using System;class AsyncDemo
    {
        //异步方法
        public void Event1()
        {
            Console.WriteLine("Event1 Start");
            System.Threading.Thread.Sleep(4000);
            Console.WriteLine("Event1 End");
        }
        public void Event2()//同步
        {
            Console.WriteLine("Event2 start");
            for (int i = 0; i <= 200; i++)
            {
                Console.WriteLine(i);
            }
            Console.WriteLine("Event2 End");
        }
    }
    class Test
    {
        public delegate void MyDel();
        public static void Main()
        {
            long start = 0;
            long end = 0;
            start = DateTime.Now.Ticks;
            AsyncDemo asy = new AsyncDemo();
            asy.Event1();
            asy.Event2();
            //MyDel del = new MyDel(asy.Event1);
            //IAsyncResult ia = del.BeginInvoke(null, null);//步异调用Event1方法
            //asy.Event2();//启动同步方法
            //del.EndInvoke(ia);//结束异步调用
            end = DateTime.Now.Ticks;
            Console.WriteLine("时间刻度为{0}",end-start);
            Console.Read();
        }

      

  2.   

    3.Monitor提供同步访问对象的机制。