码迷,mamicode.com
首页 > 编程语言 > 详细

多线程

时间:2018-05-31 16:43:48      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:sha   console   arp   bubuko   csharp   oid   分享   pre   sleep   

线程安全问题都是由全局变量及静态变量引起的。
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();//对象锁的对象  

技术分享图片

 

多线程

标签:sha   console   arp   bubuko   csharp   oid   分享   pre   sleep   

原文地址:https://www.cnblogs.com/liuqiyun/p/9117393.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!