Multi-threaded Java study notes - first met multithreading

Processes and threads:

   What is the process: the process is the basic unit of allocation of resources (CPU, memory, etc.), it is an example of when the program is executed. The system will create programs that run when a process, and allocate resources to it, and then put the process into the process ready queue, process scheduler will select it when it allocates CPU time, the program began to really run.

What is the thread: the thread is the smallest unit when the program is executed, it is a process flow of execution, is the basic unit of CPU scheduling and dispatch, a process can be made up of many threads, all of the resources shared among threads process, each thread It has its own stack and local variables. Thread scheduling executed by the CPU independent, in a multi-CPU environment allows multiple threads to run simultaneously. Multithreading may be implemented similarly concurrent operations, each request is assigned a thread to handle.

The difference between threads and processes:

   The process is the smallest unit of resource allocation, the thread is the smallest unit of program execution.

   Process has its own separate address space, each started a process, the system will allocate address space for it, set up a data table to maintain the code segment, stack and data segments, this operation is very expensive. The thread is a shared process data, using the same address space, so the CPU changes than the process takes a much smaller thread, while creating a thread overhead is much smaller than the process.

   More convenient communication between threads, the thread in the same process share global variables, static variables and other data, and communication between processes carried out in a manner (IPC) communication. But how to handle the synchronization and mutual exclusion is the difficulty of writing multithreaded programs.

   But the program more robust multi-process, multi-threaded program as long as there is a thread dies, the whole process is dead, too, and a process dies the other will not have impact on the process, because the process has its own separate address space.

Concurrent and Parallel:

   For example

   The order of execution: in order to perform multiple tasks. Play games, feet two things, some students are processing the order, so after the game to feet, the last thing done before we do the next thing.

   Parallel: some students may edge feet while playing the game, two things affect each other, at the same time to do it.

   Concurrent: There are students name the name of the game is to own odor, can not stand up then went feet, washing feet and then come back to play the game.

 

Guess you like

Origin www.cnblogs.com/ldh666/p/10993713.html
Recommended