Thread-- thread

 

The following statement is from sources in daily education Eleven teachers

1 process - thread - multi-threaded, synchronous and asynchronous
2 Asynchronous delegate start calling
more than three threads Features: no card main thread, fast, disorderly
4 asynchronous callback and state parameters
5 asynchronous wait three ways
6 asynchronous return value

and more thread .Net development is very important together,
but many developers for many years, almost no multi-threading / very afraid / to know why write the code does not consider the scene multithreaded

process: the concept of a computer program running on the server occupy all computing the sum of resources comprehensive
virtual,
threads: computer concept, process operation in response to the smallest unit, the network also includes CPU memory hard disk IO
virtual concept, more invisible
a process contains multiple threads; threads belonging to a a process, the process of destruction of the thread has gone
handle: in fact, is a long number, is the operating system identifies the application
multithreading: computer concept, a process has several threads running simultaneously

C # inside multithreaded:
the thread class is the C # language thread a package object

Why can multithreading it?
1 CPU plurality of cores may work in parallel,
4 core 8 threads, which here refers to the analog core
2 CPU fragment, lS processing capability divided into 1000 parts, the operating system in response to the scheduling of different tasks
from the macroscopic point of view feeling is concurrent execution of multiple tasks
from the microscopic point of view, only one physical cpu same time as a service task

parallelism: call between the multi-core parallel
concurrent: concurrent CPU fragment

synchronous asynchronous:
Synchronization method: initiating calls, continue after the completion of the next line; very consistent with the development of thinking and orderly execution;
sincere invite people to dinner, invited Nick, Nick to be busy for a while, after waiting for Nick to complete, and then went out to dinner
asynchronous methods: initiate calls, without waiting for completion of direct access to the next line, start a new thread to complete the calculation method of the
kind of asked people what to eat, to invite death five dead five to be busy for a while, you are busy I went to dinner, and you busy yourself go eat


1 thread: thread waits, callback, foreground thread / background thread
2 threadpool: thread pool used, provided the thread pool, the ManualResetEvent
. 3 extended packages thread & threadpool callback / wait

. 1 the Task: WaitAll the WaitAny Delay
2 TaskFactory: ContinueWhenAny ContinueWhenAll
. 3 parallel arithmetic parallel .Invoke / the For / the Foreach

1 Duo exception handling and thread to cancel
more than two threads temporary variable
3 thread safe and lock lock
4 the await / the async

 

 

/// multithreaded 1.0
/// Thread: C # a package of thread object
/// Thread many ways very strong, but also too strong, and there is no limit

 

{
The ThreadStart Method = () =>
{
the Thread.Sleep (5000);
this.DoSomethingLong ( "btnThread_Click");
the Thread.Sleep (5000);
};
the Thread Thread new new = the Thread (Method);
Thread.Start (); / / open thread, the content execution request
//thread.Suspend();// pause
//thread.Resume();// resume really should not want, not necessarily immediately suspend suspended; thread to operate too complicated
/ /thread.Abort ();
//// thread is a computer resource, the program want to stop the thread, only notifying an operating system (thread throwing an exception),
//// there will be a delay / not be able to really stop
//Thread.ResetAbort ();
// wait. 1
// the while (thread.ThreadState = ThreadState.Stopped!)
// {
// the Thread.Sleep (200 is); // current thread rest 200ms
//}
// 2 the Join waiting
thread //thread.Join();// run this code, waiting for the completion thread of
//thread.Join(1000);// wait up to 1000ms

//Console.WriteLine ( "This is the only operation after completion of the thread execution ...");

= ThreadPriority.Highest //thread.Priority;
//// highest priority: precedence, but even that does not mean the completion of priority extreme cases, there is an accident, can not perform this thread to control the order
thread.IsBackground = false; // default is false foreground thread, the process is closed, the thread needs to be calculated only after the exit
//thread.IsBackground = true; // close the process, thread exit
}

1      // based on a callback thread package
 2          // Callback: promoter threads of execution does not block the action A-- --A child thread executed after performing operation B 
. 3          ///  <Summary> 
. 4          ///  
. 5          ///  </ Summary> 
. 6          ///  <param name = "threadStart"> operation of the multi-threaded execution </ param> 
. 7          ///  <param name = "actionCallback"> after the completion of the thread, the action callback </ param> 
. 8          Private  void ThreadWithCallBack (the threadStart threadStart, the Action actionCallback)
 . 9          {
 10              // the Thread Thread new new = the Thread (threadStart);
 . 11              // Thread.Start ();
 12 is              //Thread.join (); // wrong, because the method is blocked
 13 is              // actionCallback.Invoke (); 
14  
15              the ThreadStart Method = new new the ThreadStart (() =>
 16              {
 . 17                  threadStart.Invoke ();
 18 is                  actionCallback.Invoke ();
 . 19              });
 20 is              new new the Thread (Method) .Start ();
 21 is          }
 22 is  
23 is          ///  <Summary> 
24          /// . 1 asynchronous, non-blocking
 25          /// 2 can obtain the final calculation result
 26          ///  
27          ///Or both blocked, but also the results? impossible!
28          ///  </ Summary> 
29          ///  <typeParam name = "T"> </ typeParam> 
30          ///  <param name = "FUNC"> </ param> 
31 is          ///  <Returns> </ Returns > 
32          Private Func <T> ThreadWithReturn <T> (Func <T> FUNC)
 33 is          {
 34 is              T = T default (T);
 35              the threadStart threadStart = new new the threadStart (() =>
 36              {
 37 [                  T = func.Invoke () ;
 38              });
new Thread(threadStart);
40             thread.Start();
41 
42             return new Func<T>(() =>
43             {
44                 thread.Join();
45                 //thread.ThreadState
46                 return t;
47             });
48         }
49 
50 
51         #endregion

 

Thread pool own little understood

 

 

/// If an object creation and destruction costs relatively high, but this object also can be used repeatedly, it needs a pool
/// save more of these objects, the time needed to obtain from the pool inside; not used up destruction, back into the pond; (Flyweight)
/// conserve resources improve performance; in addition, can control the total amount, to prevent abuse;
///
/// ThreadPool threads are background threads

1     #region   ThreadPool thread pool (conceptual understanding)
 2  
3          / * 
4           * 
 5           the Thread no control on the number of threads
 6  
7           thread pool
 8           If an object creation and destruction costs relatively high, but this object also can be used repeatedly, it is necessary a pond holding a plurality of such objects, the time needed to acquire used up without destroying the pool from the inside, back into the pond (Flyweight)
 9           save resources to enhance performance, also can control the total amount, to prevent abuse
 10  
. 11            ThreadPool is a global is a global singleton used if you set the maximum number of threads, then the whole process will have only a few threads
 12             
13              Task --Parrallel - ThreadPool Async / are all used in the await thread but the number of threads number of threads statement is not new thread but will occupy the ThreadPool thread pool
 14  
15  
16           * / 
17          Private  static  voidThreadPoolWithBack ()
 18 is          {
 . 19  
20 is              / * 
21 is               the QueueUserWorkItem (the WaitCallback)
 22 is               the WaitCallback // no return type is a delegate
 23               * / 
24  
25              // When the switch is closed 
26 is  
27              / * 
28               the ManualResetEvent the ManualResetEvent MRE = new new (to false); image switch off a false true open
 29               
30               reset it to (); // switch off mre.Set (); // open the switch
 31 is  
32               mre.WaitOne (); if the data transfer gate is closed the door, then it can not be transmitted. the code can not continue later execution
 33               * / 
34 is  
35              { 
 36                 MRE = the ManualResetEvent new new the ManualResetEvent ( to false );
 37 [                  the ThreadPool.QueueUserWorkItem ((X) => {
 38 is                      Console.WriteLine ($ " thread Id: {Thread.CurrentThread.ManagedThreadId.ToString ( " 00 " )} " );
 39                      //   mre.Set (); // switch to open 
40                      reset it to ();     // switch off 
41 is                  });
 42 is                  Console.WriteLine ( " delegate method has the thread pool clock " );
 43                 mre.WaitOne ();
 44                  // If you do not mre.set () opens the following code will not be implemented because there is no mre open 
45                  Console.WriteLine ( " delegate methods in the thread pool has been executed completely " );
 46 is              }
 47  
48              {
 49                  ThreadPool.SetMaxThreads ( . 8 , . 8 ); // set the maximum number of threads of workerThreads8, completionPortThreads8 
50                   
51 is                  the ManualResetEvent MRE = new new the ManualResetEvent ( to false );
 52 is                  the ThreadPool.QueueUserWorkItem ((X) => {
 53 is                     Console.WriteLine ($ " thread Id: {Thread.CurrentThread.ManagedThreadId.ToString ( " 00 " )} " );
 54 is                      //   mre.Set (); // switch to open 
55                      reset it to ();     // a off switch 
56 is                  });
 57 is                  Console.WriteLine ( " delegate method has the thread pool clock " );
 58                  mre.WaitOne ();
 59                  // If not the mre.set () to open the following code is not performed because there is no mre open 
60                  Console.WriteLine ( " delegate methods in the thread pool has been executed completely .");
61              }
 62          }
 63          #endregion

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/YZM97/p/11846233.html