A18_深入了解多线程

/*
 * 测试线程的“死锁”,及解决方法
 * 
 * 
 * 
 * **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace A18_IndepthStudyMultitheading
{
    class TestClass
    {
        private int _GameState = 100;
        private object objLock = new object();


        public void ChangeState()
        {
            while (true)
            {
                lock(objLock) //通过lock关键字让多个线程顺序访问公共数据。
                {
                    ChangeMyState();
                }
                
            }
        }


        private void ChangeMyState()
        {

            ++_GameState;
            if (_GameState == 100) //
            {
                Console.WriteLine("_GameState == 100,本语句一般情况下不会输出");
            }
            _GameState = 100; //多线程在争抢资源时有可能会因为这条语句而得到_GameState的值为100,从而使上面的判断成立
        }
    }
}


/*
 * 进一步演示“死锁”示例
 * 
 * 
 * **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;


namespace A18_IndepthStudyMultitheading
{
    
    class TestClass2
    {
        private int _Num = 0;
        private object objLock = new object(); //设置一个“所”


        public void Add()
        {
            while (true)
            {
                lock (objLock)
                {
                    _Num++;
                    Thread.Sleep(1000);
                    Console.WriteLine(Thread.CurrentThread.Name + ": " + _Num); 
                }
            }
        }
    }
}


/*
 * 多线程的“死锁”与“同步”问题
 * 死锁:多个线程争抢公共资源造成的异常情况
 * 使用lock关键字让多个线程顺序访问公共资源,暨线程的“同步”
 * 
 * **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Threading;

namespace A18_IndepthStudyMultitheading
{
    class Demo1
    {
        public void Test1()
        {
            Console.WriteLine("测试与实验多线程的死锁问题1");
            TestClass testObj = new TestClass();

            Thread t1 = new Thread(testObj.ChangeState);
            t1.Start();

            Thread t2 = new Thread(testObj.ChangeState);
            t2.Start();

        }

        public void Test2()
        {
            Console.WriteLine("测试与实验多线程的死锁问题2");
            TestClass2 testObj = new TestClass2();

            Thread t1 = new Thread(testObj.Add);
            t1.Name = "TA";
            t1.Start();

            Thread t2 = new Thread(testObj.Add);
            t2.Name = "TB";
            t2.Start();

        }

        static void Main1(string[] args)
        {
            Demo1 obj = new Demo1();
            //obj.Test1();
            obj.Test2();
        }
    }
}


/*
 * 线程池 和 任务
 * 线城池中的线程都是后台线程
 * 线城池中的线程不能更改为前台线程,也不能更改优先级
 * 
 * 
 * **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Threading;


namespace A18_IndepthStudyMultitheading
{
    class Demo2
    {

        public void ThreaMethod1(object para)
        {
            Console.WriteLine("开始下载(当前线程编号):" + Thread.CurrentThread.ManagedThreadId);
            Thread.Sleep(3000);
            Console.WriteLine("下载完毕!!!");
        }


        public void ThreadMethod2()
        {
            Console.WriteLine("开始下载");
            Thread.Sleep(2000);
            Console.WriteLine("下载完毕!!");
        }

        //线城池的使用
        public void Test1()
        {
            //使用线程池
            ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
            ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
            ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
            ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
            ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
            ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
            ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
            ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
            ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
            ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
            ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
            ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
            ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
            ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);

            Console.ReadLine();
        }

        //任务的使用
        public void Test2()
        {
            Task t = new Task(ThreadMethod2);
            t.Start();
            Console.ReadLine();
        }

        //任务工厂的使用
        public void Test3()
        {
            TaskFactory ft = new TaskFactory();
            Task t = ft.StartNew(ThreadMethod2);
            Console.ReadLine();
        }


        static void Main(string[] args)
        {
            Demo2 obj = new Demo2();
            //obj.Test1();
            obj.Test2();
        }
    }
}












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值