国产探花免费观看_亚洲丰满少妇自慰呻吟_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排序

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 酒泉市| 祁东县| 馆陶县| 牙克石市| 邓州市| 峡江县| 紫阳县| 洮南市| 平潭县| 罗平县| 滨州市| 桂阳县| 陆河县| 平武县| 邯郸市| 安化县| 柘城县| 来安县| 景宁| 临夏市| 永平县| 巫山县| 田阳县| 辽阳县| 皋兰县| 渑池县| 武胜县| 邯郸市| 阿巴嘎旗| 甘谷县| 通海县| 赤城县| 星子县| 白水县| 西青区| 彰化市| 崇礼县| 辽宁省| 河源市| 甘孜县| 徐水县|