C # threading issues

First, the multi-thread calls with arguments

#region multithreaded with a parameter of 

            the Thread MyThread = new new the Thread ( new new ParameterizedThreadStart (the Calculate)); 

            mythread.IsBackground = to true ;       

            mythread.Start ( 500 ); 

            #endregion 

        Private  void the Calculate ( Object Max)               // with a parameter delegate function 

        { 

            int max = ( int ) max; 

            The Stopwatch Stopwatch = Stopwatch.StartNew (); 

            for ( int I = 0 ; I <max; I ++)

            {

                Thread.Sleep(5);

            }

            stopwatch.Stop();

            long lSearchTime = stopwatch.ElapsedMilliseconds;

            MessageBox.Show(lSearchTime.ToString() + "毫秒");

        }
View Code

One way: define a class, the parameter to be passed as a property of the class, then the parameter value is assigned to class attributes, class will be communicated as a parameter, the following code example by two parameters, the same plurality of parameters, as follows :

class MyClass 

    { 

        public  int Max { GET ; SET ;} 

        public  int Min { GET ; SET ;} 

    } 

            #region first embodiment: multiple threads of execution with a plurality of parameters 

            MyClass Model = new new MyClass (); 

            model.Max = 500 ; 

            model.Min = 0 ; 

            the Thread mythread1 = new new the Thread ( new new ParameterizedThreadStart (CalculateTwo)); 

            mythread1.IsBackground =to true ;       

            mythread1.Start (Model); 

            #endregion 

        Private  void CalculateTwo ( Object Myclass)               // delegate function with a plurality of parameters 

        { 

            MyClass Model = (MyClass) Myclass; 

            The Stopwatch Stopwatch = Stopwatch.StartNew (); 

            for ( int I = model.Min; I <model.Max; I ++ ) 

            { 

                the Thread.Sleep ( . 5 ); 

            } 

            stopwatch.Stop (); 

            Long lSearchTime = stopwatch.ElapsedMilliseconds;

            MessageBox.Show(lSearchTime.ToString() + "毫秒");

        }
View Code

Second way: the lambda expression way , simple and convenient,

code show as below:

#region second approach: multi-threaded execution with multiple parameters of 

            the Thread mythread2 = new new the Thread (() => CalculateThree ( 500 , 0 )); 

            mythread2.IsBackground = to true ;         // set to a background thread, after the program shut down process also closed, if not set true, the program is closed, the thread is still in memory, not close 

            mythread2.Start (); 

            #endregion 

        Private  void CalculateThree ( int Max, int Min)               // delegate function with a plurality of parameters 

        { 

            the Stopwatch Stopwatch = Stopwatch.StartNew (); 

            for ( int I = Min; I <Max; I ++)

            {

                Thread.Sleep(5);

            }

            stopwatch.Stop();

            long lSearchTime = stopwatch.ElapsedMilliseconds;

            MessageBox.Show(lSearchTime.ToString() + "毫秒");

        }
View Code

 

Thread-safety issues Second, the static variables and static methods

  Internal static method creates local parameter is thread-safe , with different thread calls a static method, they will not share internal parameters to create a static method, the code example as follows

public static void Test()
{
    int i = 0;
    Console.WriteLine(i);
    i++;
}

In the above code, the variable i is not shared between the different threads, each thread calls the different method, the output is 1 ,.

Static variables are shared among different threads , we believe we all know, when a static method of static variables operate, which relates to the thread safety problems:

private static int i = 0;
public static void Test()
{
    Console.WriteLine(i);
    i++;
}

When this code is operated by a different thread, there is a thread safety problem, so it should be locked

private static int i = 0;
public static void Test()
{
    lock(this)
    {   
        Console.WriteLine(i);
        i++;
    }
}

 

Guess you like

Origin www.cnblogs.com/peterYong/p/10886441.html