zoukankan      html  css  js  c++  java
  • 多线程

    线程安全问题都是由全局变量及静态变量引起的。
    class Program { static void Main(string[] args) { ThreadTest TK = new ThreadTest(); TK.Start(); Console.ReadKey(); } class ThreadTest { private Thread thread_1; private Thread thread_2; private List<int> tickets; private object objLock = new object();//对象锁的对象 /// <summary> /// 初始化线程实例 /// </summary> public ThreadTest() { thread_1 = new Thread(Run); thread_1.Name = "thread_1"; thread_2 = new Thread(Run); thread_2.Name = "thread_2"; } /// <summary> /// 新增票启动线程 /// </summary> public void Start() { tickets = new List<int>(100); for (int i = 1; i <= 100; i++) { tickets.Add(i); } thread_1.Start(); thread_2.Start(); } public void Run() { while (tickets.Count > 0) { //获取第一张票 int ticket = tickets[0]; Console.WriteLine("{0} :{1} ", Thread.CurrentThread.Name, ticket.ToString()); //移除 tickets.RemoveAt(0); //暂停单位毫秒 Thread.Sleep(1); } } } }

      

    修改

    public void Run()
                {
                    while (tickets.Count > 0)
                    {
                        lock (objLock)
                        {
                            if (tickets.Count > 0)
                            {
                                //获取第一张票
                                int ticket = tickets[0];
                                Console.WriteLine("{0} :{1} ",
                                    Thread.CurrentThread.Name, ticket.ToString());
                                //移除
                                tickets.RemoveAt(0);
                                //暂停单位毫秒
                                Thread.Sleep(1);
                            }
                        }
                    }
                }
    

      

    2.

    class ThreadTest2
            {
                public static List<int> tickets;
    
                private object objLock = new object();//对象锁的对象
                
                /// <summary>
                /// 初始化票
                /// </summary>
                static ThreadTest2()
                {
                    tickets = new List<int>(100);
                    for (int i = 1; i <= 100; i++)
                    {
                        tickets.Add(i);
                    }
                }
                
                public void Run()
                {
                    while (tickets.Count > 0)
                    {
                        lock (objLock)
                        {
                            if (tickets.Count > 0)
                            {
                                //获取第一张票
                                int ticket = tickets[0];
                                Console.WriteLine("{0} :{1} ",
                                    Thread.CurrentThread.Name, ticket.ToString());
                                //移除
                                tickets.RemoveAt(0);
                                //暂停单位毫秒
                                Thread.Sleep(1);
                            }
                        }
                    }
                }
            }
    

      

    static void Main(string[] args)
            {
                //ThreadTest TK = new ThreadTest();
                //TK.Start();
    
                Thread thread_1 = new Thread(test);
                thread_1.Name = "thread_1";
                Thread thread_2 = new Thread(test);
                thread_2.Name = "thread_2";
    
                thread_1.Start();
                thread_2.Start();
    
                Console.ReadKey();
            }
    
    public static void test()
            {
                ThreadTest2 threadTest21 = new ThreadTest2();
                threadTest21.Run();
            }
    

      

      

    修改:

    private static object objLock = new object();//对象锁的对象  

  • 相关阅读:
    js记录
    快速最好响应式布局(CSS3)
    CSS3终极动画制作属性animation
    虚拟机中试用windows 8(视频)
    平板电脑上完美体验Windows 8 (视频)
    面对电磁泄漏您的电脑还安全吗?--计算机设备信息泄漏揭秘
    基于Linux平台Softimage XSI 演示
    为您的Office文档加把锁-ADRMS的安装
    VNC Server模拟攻击实战
    暴力破解FTP服务器技术探讨与防范措施
  • 原文地址:https://www.cnblogs.com/liuqiyun/p/9117393.html
Copyright © 2011-2022 走看看