Threads` improve performance. They move computations to a separate logical processor. They are handled in the .NET Framework with classes from the base class library. Multithreading, even with these classes, is difficult.`Example.` How can you use the Thread type in a C# program? This console program creates two instances of the Thread type and uses the ThreadStart class to specify their target methods A and B. `ThreadStart `threadstart`Next: `The threads are started and then they are joined. The two threads each run for a certain number of milliseconds.`Join `thread-join`Thread A: `This thread starts first. It terminates after 100 milliseconds, a tenth of a second.`Thread B: `This thread starts second. It ends after 1 second has elapsed. It prints B to the Console.`BackgroundWorker.` A good way to implement simple threading in Windows Forms programs is to use the BackgroundWorker class. This results in error-free and reliable threading. The class lacks some features you can implement by directly using threads. `BackgroundWorker `backgroundworker`ThreadPool.` With the ThreadPool, you can execute many different threads in parallel and have the system recycle them as soon as possible. This is an ideal way to start many short-lived jobs. The ThreadPool type provides built-in functionality. `ThreadPool `threadpool`ThreadPool.SetMinThreads `threadpool-setminthreads`Lock.` We show how to use the lock statement in the C# language. It also relates the Theory of Relativity to concurrent programs. The lock statement is a simplified syntax for Monitor method calls. `Lock `lock`Volatile: `The volatile modifier ensures a field is used in a thread-safe way. This modifier can eliminate the need for lock statements.`Volatile `volatile`Interlocked: `The Interlocked type provides a way to change or compare a value in an atomic way.`Interlocked `interlocked`Mutex.` Continuing on, the .NET Framework provides the Mutex type in the System.Threading namespace. This type is used to synchronize processes and threads within a single process. We use Mutex to create a single-instance program. `Mutex `mutex`Mutex can be used in other program contexts, but generally the lock-keyword and the async and await keywords are clearer.`Sleep.` By using the Sleep method, you can pause a thread and not have it incur any actual CPU usage. Instead, the thread is simply delayed for a certain amount of time, measured in milliseconds. `Thread.Sleep `sleep`SpinWait: `With Thread.SpinWait, your computer can wait too, doing computationally intensive loops. This is a long-running loop.`SpinWait `spinwait`Async, await.` The .NET Framework 4.5 added the async and await keywords. Async modifies a method. It indicates a method can be asynchronously run. The await keyword waits for an async method to return. It is used within an async method. `Async, Await `async`Async and await make some threading patterns easier to implement. But they require planning and a careful design.`Process.` A process is separate at the level of the operating system. It can contain multiple threads. We can use separate processes instead of separate threads. This can move some of the complexity to the operating system. `Process `process`Summary.` Multiple threads can be executed in one process. As an alternative to using multiple threads, you can use multiple processes. Please also check out the Process type. Threading introduces extra complexity into programs.

34524:DQDQbJA%yqq`yqq```?c'?c' 44; 44.Threading444 { 343{ 335Thread5 thread144Thread(4Thread4(A)); 335Thread5 thread244Thread(4Thread4(B)); 33thread1.4(); 33thread2.4(); 33thread1.Join(); 33thread2.Join(); 3} 344A() 3{ 33Thread.Sleep(100); 3345'A'5); 3} 344B() 3{ 33Thread.Sleep(1000); 3345'B'5); 3} } 5 5A B5

!dthreading constructs