首页 > 编程语言 > 详细

C#线程死锁

时间:2015-03-10 17:22:44      阅读:254      评论:0      收藏:0      [点我收藏+]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;


namespace 线程进程学习
{
    class Program
    {
        static void Main(string[] args)
        {
            var one = new object();
            var two = new object();
            new Task(new test(one, two).lock1).Start();
            new Task(new test(one, two).lock2).Start();
            Console.ReadKey();
        }
        public class test
        {
            public test(object temp1, object temp2)
            {
                this.temp1 = temp1;
                this.temp2 = temp2;
            }
            public object temp1;
            public object temp2;
            public static int temp3 = 1;
            public void lock1()
            {
                while (true)
                {
                    lock (temp1)
                    {
                        lock (temp2)
                        {
                            temp3++;
                            Console.WriteLine(temp3);
                        }
                    }
                }
            }
            public void lock2()
            {
                while (true)
                {
                    lock (temp2)
                    {
                        lock (temp1)
                        {
                            temp3++;
                            Console.WriteLine(temp3);
                        }
                    }
                }
            }
        }
    }
}

C#线程死锁

原文:http://blog.csdn.net/linukey/article/details/44176491

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!