Multithreading theory Notes

Source: Internet
Author: User
Tags terminates

1. Multithreading: In a single process, run different threads and do different work.

2. The difference between a thread and a process is that the child process and the parent process have different code and data spaces, while multiple threads share the data space, and each thread has its own execution stack and program counter for its execution context. Multithreading is mainly to save CPU time, play use, depending on the situation. The running of a thread requires the use of the computer's memory resources and CPU.

3. Message Queuing: The order of execution between the blocks in the coordination and control program.

4. Some threading models:

(1), single-threaded model: a process has only one thread, the disadvantage is that it takes a long time to complete a small task;

(2) block threading Model: Also known as single-threaded multi-block model/sta, a process has multiple blocks, and each block is unique to a thread, the program specifies the execution time of each block, and then through the message queue to serialize the control of these blocks run; The advantage of this model over a single-threaded model is that You can respond to tasks requested by multiple users at the same time, not just individual user requests. But its performance is not very good, because it uses a serialized threading model, the task is executed one by one.

(3) Multi-threaded block model: Also known as the free-threaded block model/mat; Each process has only one block, and each block has multiple threads; These applications are generally more complex because programmers must provide thread synchronization to ensure that threads do not concurrently request the same resources and thus lead to a competitive situation. It is necessary to provide a locking mechanism here. However, this may cause the system to deadlock.

5. Process and Thread concepts: both process and thread are concepts of the operating system.

(1) A process is an instance of an application that consists of a private virtual address space, code, data, and other system resources, and the resources created during the process are destroyed as the process terminates, and the system resources used are freed or closed when the process terminates.

(2) A thread is an execution unit within a process. After the system has created a process, the main execution thread that executes the process is actually started, and the main execution thread provides the program's startup point to the Windows system in the form of a function address, such as the main or WinMain function. The primary execution thread terminates, and the process terminates with it.

6. Process pool: For short-running tasks, especially those that often need to start, the process pool is a very good choice. A process pool can run multiple tasks at the same time, and you can also use a framework class. For situations in which resource shortages require synchronization, it can limit access to resources for only one thread at a time. This can be thought of as a lock mechanism implemented for threads.

7. Basic steps:

(1) Create thread:

Creates an instance of a new thread object. The thread's constructor accepts a parameter:

Thread dummythread = new Thread (new ThreadStart (dummyfunction));

(2) Execution thread:

Use the Start method in the threading namespace to run the thread:

Dummythread.start ();

(3) Combined thread:

There are often situations where multiple threads need to be combined, that is, when a thread needs the end of another thread to complete its task. Suppose Dummythread must wait for Dummyprioritythread to complete his task, just do this:

Dummyprioritythread.join ();

(4) Suspend thread:

Causes a thread to pause for a given second

Dummyprioritythread.sleep (<time in second>);

(5) Abort thread:

If you need to abort a thread, you can use the following code:

Dummyprioritythread.abort ();

8. There are two ways to implement multithreading:

(1) Inherit the thread class,

(2) Implement Runable interface.


Multithreading theory Notes

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.