C#线程安全、事件、元组的使用

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);

        }


    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值