国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C#線程安全、事件、元組的使用

2019-11-08 02:41:33
字體:
來源:轉載
供稿:網友
using System;using System.Collections.Generic;using System.Linq;using System.Reflection;using System.Text;using System.Threading.Tasks;using System.Threading;using System.Collections;using System.Collections.Concurrent;namespace Consoleapplication7{    class PRogram    {        static int count = 0;        static void Main(string[] args)        {            //線程安全            ConcurrentQueue<Teacher> queue = new ConcurrentQueue<Teacher>();            var j = 0;            Task.Factory.StartNew(() =>            {                while (true)                {                    queue.Enqueue(new Teacher { Name = "李雪-A-" + j++ });                    Thread.Sleep(100);                }            });            var m = 0;            Task.Factory.StartNew(() =>            {                while (true)                {                    queue.Enqueue(new Teacher { Name = "李雪-B-" + m++ });                    Thread.Sleep(100);                }            });            Task.Factory.StartNew(() =>            {                while (true)                {                    Teacher p_teacher;                    queue.TryDequeue(out p_teacher);                    if (p_teacher != null)                        Console.WriteLine(p_teacher.Name);                    Thread.Sleep(TimeSpan.FromSeconds(1));                }            });            Parallel.ForEach(new int[] { 1, 2, 3 }, (p) =>            {                count++;                Console.WriteLine("處理結果:" + count);                System.Threading.Thread.Sleep(TimeSpan.FromSeconds(2));            });            Console.WriteLine(count);            Console.WriteLine(Assembly.GetEntryAssembly().CodeBase);            Console.WriteLine(typeof(Teacher).FullName);            Teacher teacher = (Teacher)Assembly.LoadFile(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ConsoleApplication7.exe")).CreateInstance(typeof(Teacher).FullName);            Student student = new Student();            student.Listen(teacher);            teacher.Say("同學生們好!");            Tuple<string, int, double> studentScore = Tuple.Create("肖", 1, 100.1);            Console.WriteLine(studentScore.Item1);            Console.ReadKey();        }    }    public class Student    {        private Teacher teacher;        public void Listen(Teacher teacher)        {            this.teacher = teacher;            this.teacher.SayEvent += Teacher_SayEvent;        }        private void Teacher_SayEvent(object sender, string e)        {            Console.WriteLine("老師我聽到您說:" + e);        }    }    public class Teacher    {        public string Name { get; set; }        public event EventHandler<string> SayEvent;        public void Say(string msg)        {            Console.WriteLine("老師說:" + msg);            SayEvent(this, msg);        }    }}
上一篇:poj 3461

下一篇:hdu 2600排序

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 麻江县| 密云县| 宁蒗| 彩票| 奈曼旗| 乐亭县| 垦利县| 葫芦岛市| 湖北省| 类乌齐县| 甘洛县| 闽侯县| 湖南省| 富宁县| 泰顺县| 永修县| 密山市| 建水县| 吴川市| 仪征市| 黄平县| 肃宁县| 龙山县| 巴林右旗| 隆回县| 濉溪县| 双鸭山市| 夏邑县| 碌曲县| 武汉市| 铜山县| 红安县| 顺昌县| 六安市| 通州区| 长丰县| 富源县| 浙江省| 高邮市| 正蓝旗| 鄂尔多斯市|