One of the Java multi-thread Development Series: One of the multi-thread and multi-thread Series

Source: Internet
Author: User

One of the Java multi-thread Development Series: One of the multi-thread and multi-thread Series

Basic knowledge of programming languages: After basic concepts such as branch, selection, loop, and object orientation, we need to learn a certain degree about java advanced programming, it is inevitable to access multi-threaded development.

Due to the large overall system of multi-threaded development, I will write a series of articles to summarize the concept, usage, thread status, synchronization, and thread pool of multi-threaded development. I hope to share my hopes with you.

 

In the first part, that is, this section, we will first introduce what is multi-threaded programs, threads and processes, and why multithreading is required.

(1) What is a multithreaded program?

Multithreading is a very professional concept. In fact, it is very simple. We often come into contact with multithreading in our daily life.

For example

(1) In the factory, the workers work hard and the division of labor is clear. Some workers prepare production materials (concrete, steel bars, and bricks), some workers mine the foundation, and some workers take care of the equipment and provide logistic support (a bit like a daemon process, ). In general, they divide one thing into several jobs, then assign different people to do it, and finally solve the problem well. (A worker counts a thread)

(2) A student completes homework written at night, including language, number, and other assignments. After half of the students write their Chinese homework, they won't do any questions. First, they finish their maths homework, then they finish their English homework, and finally wait for the parents to come back to complete their Chinese homework. (A job counts as a thread)

The main reason is multithreading in these two cases,

One is that there are more than one worker (multi-processor) on the top, and the work between workers has little impact, so it does not stop another job because of one worker's hard work.

The other is the form of homework below (single processor, only one person is doing the job). Although we have multiple jobs that need to be completed (executed) this evening ), however, we can only do one thing at a time. When we encounter a certain situation (such as not doing, or in a bad mood), we switch to another subject at will and start other jobs, for some reason (for example, someone can have tutoring), we can switch back and continue to do the unfinished tasks until all the tasks are solved. Here we need to differentiate: the example of homework looks a bit like a single-thread mode example. In fact, it is not true that a single thread can only complete tasks in a certain sequence, for example, Chinese> Foreign Language> mathematics. When you encounter difficulties (not doing it), you cannot switch randomly for some reason, but you can only wait. Multithreading can be dynamically converted according to the environment, such as the order between them, such as Chinese-> Foreign Language-> mathematics-> Chinese-> Foreign Language-> mathematics.

In this case, multiple threads (including fast switching in a short period of time to form the feeling of doing multiple tasks at a time). The following describes the process and thread, from the computer perspective, we will talk about the concept of multithreading in programming.

(2) What are threads and processes?

Process: For an OS, if a program starts running in the memory, we can say that the running program is a process. This process has its own functions and is an independent unit for OS scheduling and resource allocation.

Process has three features

(1) independence: a process is an entity that exists independently in the system and has its own resources. Without its own permission, other user processes cannot access it.

(2) dynamic: A process has a lifecycle. It is a set of commands that are active in the OS. The program is just a set of static commands.

(3) concurrency: InOnly one processorMultiple processes can also be concurrently executed without affecting each other. (Processes rotate and execute each other as if at the same time)

ThreadA thread is a process execution unit, or a lightweight process. Just as the process in the OS is independent, each thread in the process is also independent. Each thread has its own stack and other resources. They are not affected. All system resources of a process (the owner of these threads) are shared by all the threads it contains.

The thread is also independent. It does not know the existence of other threads in the current process, but whether the current thread is executed is similar to the process and preemptible. The current process gets the execution right of the OS, and a thread in the process gets the execution right of the current executable (other threads and processes are suspended ).

Multithreading is an independent task that can be executed concurrently in a process. Each task is a thread.

Their relationship:

A single OS can have several processes (but at least one process is required ).

Each process has several threads (at least one thread ).

(3) Why multithreading?

The most important reason is: improve work efficiency, do not wait in idle time, each thread preemptible work.Imagine if we encounter any problems, is it possible to keep waiting for the performance to be high, or to quickly switch to other jobs, I think in most cases, it is a quick preemption. (Sometimes it may happen that you have just switched and the waiting thing has just ended .... For example, when we query a certain data, the data may be very large. It takes a long time for us to load the data on the page, at this time, if a single thread is waiting for a long time, our program will be suspended (imagine a scenario with No Response). After a long time, the page will be activated and become available. During this period, we can't do anything (to the Program). At this time, the scenario is very inefficient and unfriendly.

If we have good multi-thread support, we can also switch programs at will when loading data and drawing images without affecting other operations, thus improving performance. At the same time, because multiple threads share all the other resources of the parent process, the threads can communicate data (data sharing and communication) well and the newly opened threads (new tasks ), the operating system does not allocate a large amount of resources to the process (compared to creating a new process for a new task ).

The following are some advantages:

1. It is inconvenient to share memory resources between threads. (As mentioned earlier, threads are independent of each other. Except for not allowing a process, other processes are not allowed to directly access its address)

2. the overhead of the system to create a new thread is much smaller than that of the new process, which can greatly reduce the cost of Multi-concurrency.

3. During coding, we can manage various threads through the class library and code, rather than directly directing the processes currently executed through the operating system. This greatly facilitates the execution of concurrent tasks.

 

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.