C # Learning Notes----threads, tasks, and synchronizations

Source: Internet
Author: User

Thread

The thread has a priority, the location counter of the program that is actually being processed, a stack that stores its local variables

Each thread has its own stack, but the memory of the program code and the heap are shared by all threads of a process

Asynchronous delegate

An easy way to create a thread is to define a delegate and invoke it asynchronously

The delegate class created by the poll provides the BeginInvoke () method, and the BeginInvoke () method always has two additional arguments for the AsyncCallback and object types

Another way to wait for the result of an asynchronous delegate is to use a wait handle associated with IAsyncResult

The third way to wait for the result of the delegate is the asynchronous callback

Thread class

Use the thread class to create and control threads

The constructor overload of the thread class is a delegate parameter that accepts ThreadStart and Parameterizedthreadstart types

ThreadStart delegate defines a parameterless method with a return type of void

After you have created the thread object, you can start it with the start () method

There are two ways to pass data to a thread. One way is to use the thread constructor with the Parameterizedthreadstart delegate parameter

Another way is to create a custom class that defines the thread's method as an instance method so that the data of the instance can be initialized, and then the thread is started

By default, threads created with the thread class are foreground threads.

Thread in thread pool is always a background thread

Set the IsBackground property to determine whether it is a foreground thread or a background thread

Set priority property, thread precedence

Thread.threadstate attribute Thread State

Thread.Sleep (), Thread.Abort (), Thread.Join ()

Thread pool

Managed by the ThreadPool class

All threads in the thread pool are background threads. If all foreground threads of the process are ended, all background threads will stop. You cannot change the thread of a pool into a foreground thread.

You cannot set a priority or name for a thread that is in the pool

For COM objects, all threads that are pooled are multithreaded apartment (multithreaded Apartment,mta) threads. Many COM objects require a single-threaded apartment (single-threaded Apartment,mta) thread

The thread into the pool can only be used for tasks that are short in time. If the thread is running all the time, you should use the thread class to create a thread

Task

. NET 4 contains the new namespace System.Threading.Tasks

To start a task, you can use the constructor and start () method of the TaskFactory class or task class

Parallel class

In. NET 4, another new abstraction thread is the parallel class.

Canceling schemas

. NET 4 includes a new cancellation schema that allows long-running tasks to be canceled in a standard manner.

Threading issues

A race condition occurs if two or more threads access the same object or access a shared state that is not synchronized

In a deadlock, at least two threads are suspended and wait for the object to be unlocked

Synchronous

C # provides its own keyword for synchronization of multiple threads: the lock statement.

WaitHandle is an abstract base class that is used to wait for a signal to be set.

Timer class

The Timer class in the System.Threading namespace provides the core functionality. In a constructor, you can pass a delegate that should be called at a specified interval of time.
The Timer class in the System.Timers namespace is a component because it derives from the component base class.

Therefore, you can drag and drop it from the Toolbox onto the phone interface of the server application. This Timer class uses System.Threading.Timer, but provides a time-based mechanism rather than a delegate-based mechanism.

Event-based Asynchronous Pattern

The BackgroundWorker class is an implementation scheme for asynchronous event patterns.

C # Learning Notes----threads, tasks, and synchronizations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.