Advanced use of delegation

  1. multicast a delegate contains more than one method
    static void Main(string[] args)
            {
                Student stu1 = new Student() { ID=1,PenColor=ConsoleColor.Yellow};
                Student stu2 = new Student { ID = 2, PenColor = ConsoleColor.Green };
                Student stu3 = new Student { ID = 3, PenColor = ConsoleColor.Red };
    
                Action action1 = new Action(stu1.DoHomeWork);
                Action action2 = new Action(stu2.DoHomeWork);
                Action action3 = new Action(stu3.DoHomeWork);
                action1 += action2;
                action1 += action3;
                action1();//Multicast delegation, according to the order of encapsulation methods
                //action1();//Unicast delegation
                //action2();//Unicast delegation
                //action3();//Unicast delegation
                Console.ReadLine();
            }
        }
        class Student
        {
            public int ID { get; set; }
            public ConsoleColor PenColor { get; set; }
            public void DoHomeWork()
            { 
            for(int i=0;i<5;i++)
            {
                Console.ForegroundColor = this.PenColor;
                Console.WriteLine("Student{0} doing homework {1} hours",this.ID,i);
                Thread.Sleep(1000);
            }
            }
        }

    2. Implicit asynchronous call

  • The language difference between synchronous and asynchronous. In English, asynchronous is done by two people at the same time. Synchronous is done by me on your basis.
  • Comparison between synchronous call and asynchronous call
    • Every program is a process
    • Each process can have one or more thread s
    • Synchronous call in a thread
    • The underlying mechanism of asynchronous call is multithreading (when multithreading accesses resources at the same time, pay attention to the conflict between threads competing for resources, and lock the threads at this time)
    • Serial = synchronous = single thread, parallel = asynchronous = multi thread
 class Program
    {
        static void Main(string[] args)
        {
            Student stu1 = new Student() { ID=1,PenColor=ConsoleColor.Yellow};
            Student stu2 = new Student { ID = 2, PenColor = ConsoleColor.Green };
            Student stu3 = new Student { ID = 3, PenColor = ConsoleColor.Red };

            //stu1.DoHomeWork();//Direct call, synchronous
            //stu2.DoHomeWork();
            //stu3.DoHomeWork();

            Action action1 = new Action(stu1.DoHomeWork);//Indirect call, synchronous
            Action action2 = new Action(stu2.DoHomeWork);
            Action action3 = new Action(stu3.DoHomeWork);
            //action1 += action2;
            //action1 += action3;
            //action1();//Multicast delegation, according to the order of encapsulation methods//Indirect call, synchronous


            //action1();//Unicast delegation//Indirect call, synchronous
            //action2();//Unicast delegation
            //action3();//Unicast delegation


            //Asynchronous call to delegate
            //action1.BeginInvoke(null,null);//The implicit indirect asynchronous call will generate a branch method. The first parameter of the two parameters is the callback method. Only when the sub thread finishes executing, what needs to be executed?
            //action2.BeginInvoke(null, null);//
            //action3.BeginInvoke(null, null);



         //General multithreading
            Thread thread1 = new Thread(new ThreadStart(stu1.DoHomeWork));//Explicit direct asynchronous call,
            Thread thread2 = new Thread(new ThreadStart(stu2.DoHomeWork));
            Thread thread3 = new Thread(new ThreadStart(stu3.DoHomeWork));

            thread1.Start();
            thread2.Start();
            thread3.Start();

            //Advanced multithreading with delegation
            Task task1 = new Task(new Action(stu1.DoHomeWork));//Explicit asynchronous call
            Task task2 = new Task(new Action(stu2.DoHomeWork));
            Task task3 = new Task(new Action(stu3.DoHomeWork));

            task1.Start();
            task2.Start();
            task3.Start();


            for (int i = 0; i < 10;i++ )
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("Main thread{0}",i);
                Thread.Sleep(1000);
            }
                Console.ReadLine();
        }
    }
    class Student
    {
        public int ID { get; set; }
        public ConsoleColor PenColor { get; set; }
        public void DoHomeWork()
        { 
        for(int i=0;i<5;i++)
        {
            Console.ForegroundColor = this.PenColor;
            Console.WriteLine("Student{0} doing homework {1} hours",this.ID,i);
            Thread.Sleep(1000);
            
        }
        }
    }

 

Interfaces should be used in time to replace the use of delegation

  • java completely uses the interface to replace the function of delegation. java does not have the entity corresponding to the delegation in C ා.
  • Refactoring code generally means putting the code in a more appropriate place

Keywords: PHP Java

Added by scorpioy on Mon, 21 Oct 2019 00:39:58 +0300