Java multithreading (1). Understand the concepts and relationships between processes and Multithreading

Source: Internet
Author: User

1. What is a process?

In a simple understanding, in a multitasking system, each program that runs independently is a process, or every program that is currently running is a process. Most of the operating systems we use today are multitasking systems, such as Windows, Linux, Mac OS X, and UNIX. Because a single CPU can only execute one program at a time, this is an iron law. But in the system, how can a single CPU execute multiple programs at the same time? The actual situation is that the operating system is responsible for scheduling and allocating and managing CPU resources. Although a single CPU can only do one thing at a time, however, it switches to execute multiple programs at a very small interval. However, with a weak eye, it is impossible to notice that the CPU is switching back and forth to execute multiple programs, so it gives people the feeling of executing multiple programs at the same time. If we open two notepad programs A and B at the same time, this is two different processes. The document edited by a will not affect B. Because each process has an independent code and data storage space and operates data in its own space, it does not affect each other.


2. What is a thread?


A process can contain one or more threads. A thread is an execution thread in the program. In a single thread, a thread is automatically generated when the program starts. This thread is called the main thread. The main function is run on this thread, and then the main function is executed in sequence according to the calling sequence of the program code. In this case, when the main function calls the sub-function, the main function must wait for the sub-function to return before it can continue to be executed, and the effect of alternating execution of two sections of Code cannot be achieved. If you want to execute multiple pieces of code alternately in a program, you need to generate multiple threads and specify the program code to run on each thread. This is multithreading. There are two methods to create multithreading in Java: Inherit the java. Lang. Thread class and implement the runnable interface, and call the start method of the thread class to start the thread.


3. Process and thread scenario analysis and understanding


1> the core of the computer is the CPU, which undertakes all the computing tasks. Like a factory, it is always running. Provide dredging and processing services for each component in the factory.



2> If the power of the plant is limited, only one workshop can be used at a time. That is to say, when a workshop is started, other workshops must be shut down. It means that a CPU can only execute one task (process) at a time ).




3> the process is like a workshop in a factory. At any time, only one workshop is in production and other workshops are in shutdown status. It means that the CPU can only run a single process at any time, and other processes are not active.




4> there can be many workers in a workshop who work together to complete a task. For example, in a mobile phone production workshop, Zhang San is responsible for the installation and debugging of the Main Board, Li Si is responsible for the testing and processing of the display screen, and Wang Wu is responsible for the assembly of mobile phone parts.A thread is like a worker in the workshop. A process contains multiple threads, each of which is responsible for completing their own tasks.




5> the space in the workshop is shared by the workers. For example, many rooms in the workshop (such as the processing room, production room, and assembly room), each room in the workshop, employees can move around and enter the site at will. This indicates that the memory space of a process is shared and can be used by all threads in the process.




6> the size of each room in the workshop is different. Some rooms can only accommodate one person at most, such as the restroom. When there are people in the room, you cannot enter the room any more. You need to wait for the people in the room to come in. That is to say, when a thread is using a shared memory, other threads must wait for it to end before other threads can use this memory.




7> a simple way to stop others from entering the toilet is to put a lock on the outside. The first person enters the toilet and locks the door to the door. Then, the person sees the lock in the toilet and queues at the door. When the lock is opened, the person goes in again. This is mutex, which prevents multiple threads from simultaneously reading and writing data in a memory area. Use the synchronized keyword in Java to achieve mutual exclusion between multiple threads.




8> some rooms can accommodate n people at the same time, such as kitchens. If the number of people is greater than N, the extra people can only wait outside and wait for others to come in. This is like some shared memory areas, which are only accessible to a fixed number of threads.




At this time, the solution is to plug-in N locks in the door, and the person who enters the lock will take a lock. When it comes out, the lock will be hung back to the original place. The person who arrived later found that the key was overhead and he knew that he had waited in the queue outside the door. This is called"Semaphores (semaphore)"To ensure that multiple threads do not conflict with each other.


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.