Preliminary understanding of processes and multithreading and deadlocks, and multi-thread understanding of processes

Source: Internet
Author: User

Preliminary understanding of processes and multithreading and deadlocks, and multi-thread understanding of processes

Concept:ProcessIt is an execution activity on a computer, a dynamic execution process of a program, and a unit of resource allocation by the operating system. When a program is run, a process is started.ProcessAttackers can access processes on local or remote computers and start and stop processes in the Local System (System. Diagnostics)

ThreadIt is the basic unit for the operating System to allocate the processor time. It is a situation where multiple operating systems seem to occur at the same time (System. Threading)

Advantages of multithreading: 1. Background operations can also be performed when the user interface responds. 2. tasks are executed by priority. 3. web servers or databases are connected over the network.

Multithreading disadvantages: 1. Increasing the system load may lead to lower performance. 2. It is difficult to control multi-threaded code, which may lead to many bugs.

MultithreadingA thread is a single sequential control flow in a program. A thread is an entity in a process. A process can have multiple threads, and a thread must have one parent thread. A thread does not have system resources and only has some data structures required for running. It shares all resources owned by the process with other threads of the parent thread. Threads can be created and abolished to implement concurrent program execution.

Deadlock: A deadlock occurs when two or more threads that use shared resources occupy shared resources and wait for the resources occupied by other threads, as a result, two or more threads are waiting for the other party to release resources and the execution is stopped.

Instance:

Startup thread:

The construction method uses the ThreadStart proxy as the parameter. The method pointed to by ThreadStart is executed in the thread. The thread is started through the Start method.
Thread t = new Thread (new ThreadStart (MyClass. AStaticMethod ));
T. Start ();
Attributes and parameters of a thread:

Thread. Name gets or sets the Thread Name; Thread. Priority gets or sets the Thread Priority; whether the Thread. IsBackground Thread can run in the background
Management thread:
Thread. Sleep blocks the current Thread. Sleep (3000); // blocks for 3 seconds

Suspend and Resume are not commonly used, which may lead to severe problems in applications such as deadlocks.

Thread. Join Wait for other threads to stop t. Join (); // Wait for the thread to exit
Thread. WaitHandle wait for one or more events WaitHandle. WaitAll (waitEvents );

Thread. ThreadState attribute -- Thread status

Thread interruption and termination:

Thread. Interrupt method interrupts the Thread in the execution state; Thread. Abort method ends the Thread

  

 

Related Article

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.