zoukankan      html  css  js  c++  java
  • Part 89 to 91 Talking about pass the parameters in thread

    Part 89   ParameterizedThreadStart delegate

    Use ParameterizedThreadStart delegate to pass data to the thread function

    class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("input a target number:");
                object target = Console.ReadLine();
                ParameterizedThreadStart pt = new ParameterizedThreadStart(Number.Print);//really need to write that?No,you can just pass the function into Thread class.
                Thread t = new Thread(pt);//new Thread(Number.Print);
                t.Start(target);
                Console.ReadLine();
            }
        }
    
        public class Number
        {
            public static void Print(object target)
            {
                int number = 0;
                if (int.TryParse(target.ToString(), out number))
                {
                    for (int i = 0; i < number; i++)
                    {
                        Console.WriteLine(i);
                    }
                }
            }
        }
    View Code

    How we are not explictly creating an instance of ParameterizedThreadStart delegate.Then how is it working?

    It's working because, the compiler implicitly converts new Thread(Number.Print); to new Thread(new ParameterizedThreadStart(Number.Print));

    When to use ParameterizedThreadStart over ThreadStart delegate?

    Use ParameterizedThreadStart delegate if you have some data to pass to the Thread function, otherwise just use ThreadStart delegate.

    Please note: Using parameterizedThreadStart delegate and Thread.Start(Object) method to pass data to the Thread function is not type safe as they operate on object datatype and any type of data can be passed.

    If you try to change the data type of the target parameter of Print() function from object to int, a compiler error will be raised as the signature of Print() function does not match with the signature of ParameterizedThreadStart delegate.

    Part 90   Passing data to the Thread function in a type safe manner(in a manner..在某种程度上)

    To pass data to the Thread function in a type safe manner, encapsulate the thread function and the data it needs in a helper class and use the ThreadStart delegate to execute the thread function.

    class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("input a target number:");
                int target = Convert.ToInt32(Console.ReadLine());
                Number number = new Number(target);
                Thread t = new Thread(number.Print);
                t.Start();
                Console.ReadLine();
            }
        }
    
        public class Number
        {
            int _target;
            public Number(int target)
            {
                this._target = target;
            }
            public void Print()
            {
                int number = 0;
                for (int i = 0; i < _target; i++)
                {
                    Console.WriteLine(i);
                }
            }
        }
    View Code

    Part 91   Retrieving data from Thread function using callback method

    Callback method to get data from thread

    Main thread retrieves(检索) the target number from the user.

    Main thread creates a child thread and pass the target number to the child thread.

    The child thread computes the sum of numbers and then returns the sum to the Main thread using callback function.

    The callback method prints the sum of numbers.

    Step 1: Create a callback delegate. The actual callback method signature should match with the signature of this delegate.

    public delegate void SumOfNumberCallback(int sumOfNumber);

    Step 2:Create a helper class to compute the sum of numbers and to call the callback method.

    public class Number
        {
            int _target;
            SumOfNumberCallback _callBackMethod;
            public Number(int target, SumOfNumberCallback callBackMethod)
            {
                this._target = target;
                this._callBackMethod = callBackMethod;
            }
            public void PrintSumOfNumber()
            {
                int sum = 0;
                for (int i = 1; i <= _target; i++)
                {
                    sum = sum + i;
                }
                if(_callBackMethod!=null)
                {
                    _callBackMethod(sum);
                }
            }
        }
    View Code

    Step 3:This class consumes the Number class create in Step 2

    class Program
        {       
            public static void Main(string[] args)
            {
                Console.WriteLine("input a target number:");
                int target = Convert.ToInt32(Console.ReadLine());
                SumOfNumberCallback callBack = new SumOfNumberCallback(PrintSum);
                Number number = new Number(target,callBack);
                Thread t = new Thread(number.PrintSumOfNumber);
                t.Start();
                Console.ReadLine();
            }
    
            public static void PrintSum(int sum)
            {
                Console.WriteLine("sum of number =" + sum);
            }      
        }
    View Code
  • 相关阅读:
    linux_ext4恢复超级块.txt
    bayaim_Centos7.6_mysql源码5.7-多my.cnf_20190424.txt
    bayaim_Centos7.6_mysql源码5.7-multi_20190424.txt
    zhy2_rehat6_mysql04
    zhy2_rehat6_mysql03
    Neo4j安装,入门到深入了解完整教程
    程序员千万不要裸辞,对你没有任何好处
    减薪是变相的裁员所以我还是走吧
    python正则表达式匹配中文日期时间
    python用正则表达式匹配字符串里的日期
  • 原文地址:https://www.cnblogs.com/gester/p/4870443.html
Copyright © 2011-2022 走看看