• 多线程一


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Threading;
    
    namespace ThreadTest
    {
        class ThreadTest
        {
            public object locker = new object();
            int count = 100;
            public void Sell()
            {
                //Console.WriteLine("当前进入的线程:" + System.Threading.Thread.CurrentThread.Name);
                while (count > 0)
                {
                    lock (locker)//不同线程执行这个代码时只要确保是同一个对象就可以了,如: this.GetType()  /  static变量 / 相同实例变量
                    {
                        count--;
                        Console.WriteLine(System.Threading.Thread.CurrentThread.Name + ",剩{0}张~!", count);
                    }
                }
            }
    
            #region 1.0测试 实例方法里的锁
            /// <summary>
            /// 1.0测试 实例方法里的锁
            /// </summary>
            /// <param name="oLoker"></param>
            public void TestSellMany()
            {
                ThreadTest tt = new ThreadTest();
                Thread t1 = new Thread(tt.Sell);
                t1.Name = "t1";
                t1.IsBackground = true;
    
                Thread t2 = new Thread(tt.Sell);
                t2.Name = "t2";
                t2.IsBackground = true;
    
                t1.Start();
                t2.Start();
            } 
            #endregion
            //----------------------------------------------------------------
    
            static int countStatic = 100;
            public static object lockerStatic = new object();
            public static void SellStatic(object oLoker)
            {
                while (countStatic > 0)
                {
                    lock (oLoker)//不同线程执行这个代码时只要确保是同一个对象就可以了,如: this.GetType()  /  static变量 / 相同实例变量
                    {
                        countStatic--;
                        Console.WriteLine(System.Threading.Thread.CurrentThread.Name + ",剩{0}张~!", countStatic);
                    }
                }
            }
    
            #region 2.0 测试 静态方法里的锁
            /// <summary>
            /// 测试 静态方法里的锁
            /// </summary>
            public void TestSellManyStatic()
            {
                ThreadTest tt = new ThreadTest();
                Thread t1 = new Thread(SellStatic);
                t1.Name = "t1";
                t1.IsBackground = true;
    
                Thread t2 = new Thread(SellStatic);
                t2.Name = "t2";
                t2.IsBackground = true;
    
                object lok = new object();
                t1.Start(lok);
                t2.Start(lok);
            } 
            #endregion
            //----------------------------------------------------------------
            public void SellByMonitor()
            {
                while (countStatic > 0)
                {
                    bool isGetLoker = false;
                    try
                    {
                        Monitor.Enter(locker, ref isGetLoker);//尝试获取锁,如果获取成功,isGetLoker=true,否则=false
                        countStatic--;
                        Console.WriteLine(System.Threading.Thread.CurrentThread.Name + ",剩{0}张~!", countStatic);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        if (isGetLoker)//如果有锁,就解锁;否则,没有锁,就不存在 解锁!
                        {
                            Monitor.Exit(locker);
                        }
                    }
                }
            }
            #region 3.0 测试 Monitor
            /// <summary>
            /// 3.0 测试 Monitor
            /// </summary>
            public static void TestMonitor()
            {
                ThreadTest tt = new ThreadTest();
                Thread t1 = new Thread(tt.SellByMonitor);
                t1.Name = "t1";
                t1.IsBackground = true;
    
                Thread t2 = new Thread(tt.SellByMonitor);
                t2.Name = "t2";
                t2.IsBackground = true;
    
                t1.Start();
                t2.Start();
            } 
            #endregion
    
            int forNum = 0;
            public void PrintNum(int num)
            {
                Console.WriteLine(System.Threading.Thread.CurrentThread.Name+",哈哈哈~~~");
                Console.WriteLine(num);
            }
    
            //同步方法 特性
            [MethodImpl(MethodImplOptions.Synchronized)]
            public void MethodLock()
            {
                for (; forNum < 100; forNum++)
                {
                    PrintNum(forNum);
                }
            }
    
            #region 4.0 测试同步方法
            //测试同步方法
            public static void TestMothodLock()
            {
                ThreadTest tt = new ThreadTest();
                Thread t1 = new Thread(tt.MethodLock);
                t1.Name = "t1";
                t1.IsBackground = true;
    
                Thread t2 = new Thread(tt.MethodLock);
                t2.Name = "t2";
                t2.IsBackground = true;
    
                t1.Start();
                t2.Start();
            } 
            #endregion
            //-------------------------------------------------------------------------
            static object locker1 = new object();
            static object locker2 = new object();
    
            #region 5.0测试死锁
            //5.0测试死锁
            public static void TestDeadLock()
            {
                new Thread(() =>
                {
                    Console.WriteLine("线程1开始执行");
                    lock (locker1)   //获取锁locker1
                    {
                        Console.WriteLine("线程1获取锁1");
                        Thread.Sleep(1000);
                        lock (locker2) //尝试获取locker2
                        {
                            Console.WriteLine("线程1获取锁2");
                        }
                    }
                }).Start();
    
                new Thread(() =>
                {
                    Console.WriteLine("线程2开始执行");
                    lock (locker2) //获取锁locker2
                    {
                        Console.WriteLine("线程2获取锁2");
                        Thread.Sleep(1000);
                        lock (locker1) //尝试获取locker1
                        {
                            Console.WriteLine("线程2获取锁1");
                        }
                    }
                }).Start();
            } 
            #endregion
            //-------------------------------------------------------------------------
            #region 6.0 测试 线程通信
            /// <summary>
            /// 6.0 测试 线程通信
            /// </summary>
            public static void TestProCus()
            {
                Dog d = new Dog();
                Productor p = new Productor(d);
                Customer c = new Customer(d);
    
                Thread trhPro = new Thread(p.ProductDog);
                trhPro.IsBackground = true;
                trhPro.Name = "t1";
    
                Thread trhCus = new Thread(c.ShowDogInfo);
                trhCus.IsBackground = true;
                trhCus.Name = "t2";
    
                trhPro.Start();
                trhCus.Start();
                Console.ReadLine();
                trhPro.Abort();
                trhCus.Abort();
            } 
            #endregion
            //-------------------------------------------------------------------------
    
            public static void Mother()
            {
                Console.WriteLine("买菜~~~");
                Console.WriteLine("切菜~~~");
                Console.WriteLine("做菜~~~");
                Console.WriteLine("做饭~~~判断是否有米,发现木有米勒,就叫儿子去买米......");
                Thread thrSon = new Thread(Son);
                thrSon.IsBackground = true;
                thrSon.Start();
                thrSon.Join();//强制阻断其他线程执行,先执行当前线程thrSon
                Console.WriteLine("儿子买米回来了~~~");
                Console.WriteLine("淘米~~~");
                Console.WriteLine("煮饭~~~");
                Console.WriteLine("吃饭~~~");
            }
    
            public static void Son()
            {
                Console.WriteLine("妈妈叫我去买米~~~");
                Console.WriteLine("买米哦~~~");
                for (int i = 0; i < 5; i++) 
                {
                    System.Threading.Thread.Sleep(500);
                    Console.WriteLine("{0}秒钟......",i);
                }
                Console.WriteLine("买好米勒,回家~~~!");
            }
    
            public static void TestJoin()
            {
                Thread thrMother = new Thread(Mother);
                thrMother.IsBackground = true;
                thrMother.Start();
            }
    
            public static void Main(string[] ar)
            {
                //TicketSeller t1 = new TicketSeller("1台");
                //TicketSeller t2 = new TicketSeller("2台");
                TestJoin();
                Console.ReadLine();
            }
    
        }
    
        /// <summary>
        /// 产品
        /// </summary>
        public class Dog
        {
            public static object lockCommunicate = new object();
    
            public string name;
            public int age;
        }
    
        /// <summary>
        /// 生产者
        /// </summary>
        public class Productor
        {
            Dog myDog;
            public Productor(Dog d)
            {
                myDog = d;
            }
    
            bool type = true;
            public void ProductDog()
            {
                while (true)
                {
                    lock (Dog.lockCommunicate)
                    {
                        if (type)
                        {
                            myDog.name = "Ruiky";
                            myDog.age = 2;
                        }
                        else
                        {
                            myDog.name = "瑞奇";
                            myDog.age = 1;
                        }
                        type = !type;
                        Monitor.Pulse(Dog.lockCommunicate);
                        Monitor.Wait(Dog.lockCommunicate);
                    }
                }
            }
        }
    
        public class Customer
        {
            Dog myDog;
            public Customer(Dog d)
            {
                myDog = d;
            }
    
            public void ShowDogInfo()
            {
                while (true)
                {
                    lock (Dog.lockCommunicate)
                    {
                        Console.WriteLine("我是{0},年龄{1}", myDog.name, myDog.age);
                        Monitor.Pulse(Dog.lockCommunicate);
                        Monitor.PulseAll(Dog.lockCommunicate);
                        Monitor.Wait(Dog.lockCommunicate);
                    }
                }
            }
        }
    
        public class TicketSeller
        {
            public static int count = 100;
            public static object locker = new object();
    
            public TicketSeller(string taiName)
            {
                Thread t2 = new Thread(Sell);
                t2.Name = taiName;// "2号台";
                t2.IsBackground = true;
                t2.Start();
            }
    
            public void Sell()
            {
                while (count > 0)
                {
                    lock (this)
                    {
                        TicketSeller.count--;
                        Console.WriteLine(System.Threading.Thread.CurrentThread.Name + ",剩{0}张~!", count);
                    }
                }
            }
    
            public void Sell2()
            {
                lock (locker)
                {
     
                }
            }
        }
    }
  • 相关阅读:
    Fiddler 的几个用法
    jQuery中ajax的使用和缓存问题解决 $getjson 与$get都会被IE缓存
    http与https的区别以及https的加密原理
    asp.net mvc通过预处理实现数据过滤和数据篡改。
    C# winform的WebBrowser非常规编程(强烈推荐)
    DES加密 java与.net可以相互加密解密的方法
    日常开发中常见的HTTP协议的状态码
    前端性能监控
    在手机的浏览器上通过连接打开App
    工作五年总结一二三流公司的常见特性
  • 原文地址:https://www.cnblogs.com/sumg/p/4106735.html
Copyright © 2020-2023  润新知