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
  • 相关阅读:
    剑指offer4:重建二叉树(后序遍历)
    剑指offer3:从尾到头打印链表每个节点的值
    剑指offer2:C++实现的替换空格(字符中的空格替换为“%20”)
    tp5系统变量输出(可以用来传递搜索的参数)
    Ajax实现文件上传的临时垃圾文件回收策略
    php获取当天的开始时间和结束时间
    Think PHP递归获取所有的子分类的ID (删除当前及子分类)
    tp查找某字段,排除某字段,不用一次写那么多
    git-查看历史版本及回滚版本
    dedecms目录结构,非常全
  • 原文地址:https://www.cnblogs.com/gester/p/4870443.html
Copyright © 2011-2022 走看看