Concepts of processes and threads

Source: Internet
Author: User
Tags instance method thread class

A: What is a process

A process is the basic unit of allocation of resources (CPU, memory, and so on), which is an instance of program execution.

When the program runs, the system creates a process, assigns IT resources, and then puts the process into the process-ready queue.

When the process scheduler selects it, it allocates CPU time and the program starts to actually run.

Two: What is a thread

A thread is the smallest unit of execution of a program, which is an execution flow of a process, and is the basic unit of CPU dispatch and dispatch.

A process can consist of a number of threads that share all the resources of a process, each with its own stack and local variables.

Threads are scheduled to be executed independently by the CPU, allowing multiple threads to run concurrently in a multi-CPU environment. Similarly, multithreading can also be implemented concurrently, with each request being assigned a thread to process.

Three: What are the differences and pros and cons of threads and processes?

1): The process is the smallest unit of resource allocation, and the thread is the smallest unit of program execution.

2): The process has its own independent address space, each time a process is started, the system will assign it an address space, set up a data table to maintain the code snippets, stack segments and data segments, this is very expensive operation.

The thread is the data in the shared process, using the same address space, so the CPU switching one thread is much less expensive than the process, and creating a thread is much less expensive than the process.

3): Communication between threads is more convenient, the same process threads share global variables, static variables and other data.

Communication between processes needs to be conducted in a communication way (IPC). However, how to deal with the synchronization and mutual exclusion is the difficulty of writing multi-threaded program.

4): But the multi-process program is more robust, multithreaded programs as long as one thread dies, the whole process is dead,

A process that dies does not affect another process because the process has its own independent address space.

Four: Multi-threaded five states

New-----> Ready-----> Run------> Blocked-------> Dead

Five: How to realize multithreading

1): Inherit thread class to create threads.

The thread class essentially implements an instance of the Runnable interface, representing an instance of a thread. The only way to start a thread is through the start () instance method of the thread class.

2): Implements the Runnable interface creation thread.

3): If your class already extends another class, you cannot directly extends the Thread, at which point you can implement a runnable interface.

4): Implement the callable interface to create thread threads through the Futuretask wrapper

Six: Multi-process and multi-threaded differences, such as

  

  Seven: When to use multi-process, when to use multi-threading

1): The priority threads that need to be created frequently for destruction.

2): Priority usage threads that require a lot of computation.

3): Strongly related processing with threads, weakly related processing with processes.

What is strong correlation and weak correlation?

Example:

The general server needs to do the following tasks: messaging, message processing.

"Messaging" and "message processing" are weak-related tasks, and "message processing" can be divided into "message decoding",

"Business Processing", these two tasks are relatively much more relevant.

So "messaging" and "message processing" can be divided into process design, "message decoding", "Business processing" can be divided into threading design.

Of course, this classification is not static, but also can be adjusted according to the actual situation.

4): Threads that may want to be extended to multi-machine distribution with processes, multicore distribution.

   

Concepts of processes and threads

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.