JAVA Multithreading and Concurrent Learning notes (iv)

Source: Internet
Author: User

1. Multi-process

The most straightforward way to implement concurrency is to use processes at the operating system level, which is a self-contained program that runs within its own address space. A multitasking operating system enables multiple processes to run concurrently by switching the CPU from one process to another periodically. Although it is possible for a CPU to run only one process at a time, the CPU can be rotated between multiple processes, and the CPU is switching very fast, making it impossible for us to perceive the process of switching, as if more than one process is executing simultaneously.

Almost all operating systems support the concept of processes, and all running tasks typically correspond to one process. When a program goes into memory run, it becomes a process. Process is a program in the running process, and has a certain independent function, the process is a system for resource allocation and scheduling of an independent unit. In general, the process consists of the following 3 characteristics:

    • Independence: The process is a separate entity in the system, it can have its own independent resources, each process has its own private address space. A user process does not have direct access to the address space of another process without the process itself permitting.
    • Dynamic: The difference between a process and a program is that a program is just a static set of instructions, and a process is a collection of instructions that are active in the system. The concept of time is added to the process, and the process has its own life cycle and various states, which are not available in the middle of the program.
    • Concurrency: Multiple processes can execute concurrently on a single processor, with no interaction between multiple processes.

2 Multi-Threading

2.1 Multi-Threading Overview

Multi-Threading extends the concept of multi-process for concurrency. Enables simultaneous processing of multiple tasks concurrently in the same process. Threads (thread) are also known as lightweight processes (lightweight process). A thread is the execution unit of a process, just as a process is in the operating system, and a thread is a separate, concurrent execution flow in a program. When the process is initialized, the main thread is created. For most applications, there is usually only one main thread required, but it is also possible to create multiple sequential execution flows within that process, which are threads and each thread is independent of each other.

A thread is part of a process, a process can have multiple threads, and a thread must have a parent process. A thread can have its own stack, its own program counter, and its own local variables, but does not own system resources, and it shares all of the resources owned by the process with other threads of the parent process. Programming is easier because multiple threads share all the resources in the parent process, but you must be more careful and we must ensure that threads do not interfere with other threads in the same process.

2.2 Multi-threading mechanism

The threading model facilitates programming, simplifying the processing of multiple operations that are intertwined at the same time in a single program. When a thread is used, the CPU will take turns allocating its elapsed time to each task. Each task feels like it has been occupying the CPU, but in fact CPU time is divided into fragments assigned to all tasks. One of the great benefits of threading is that you can get out of this hierarchy, where the code doesn't have to know whether it's running on a machine with one or more CPUs. So, using the threading mechanism is a way to build a transparent, extensible program, and if the program is running too slowly, adding a CPU to the machine can easily speed up the program. Multitasking and multithreading are often the most logical way to use multiprocessor systems.

2.3 Multi-Thread scheduling

Threads can accomplish certain tasks by sharing shared variables and some environments in the parent process with other threads, collaborating with each other to complete the tasks that the process needs to accomplish. The thread is run independently, it does not know if there are other threads in the process, the execution of the thread is preemptive, that is, the currently running thread may be suspended at any time so that another thread can run.

One thread can create and revoke another thread, which can be executed concurrently between multiple threads in the same process. From a logical point of view, multithreading exists in an application where multiple execution parts can be executed simultaneously, but the operating system does not need to treat multiple threads as multiple independent applications, scheduling and managing multithreading, and allocating resources. The scheduling and management of threads is done by the process itself.

Inductive mining It can be said that the operating system can perform multiple tasks at the same time, each task is a process, and a process can perform multiple tasks at the same time, each task being a thread. In short, after a program runs at least one process, a process can contain multiple threads, but at least one thread is included.

2.4 Advantages of multi-threading

Threads are independent, concurrent execution flows in a program, and are less isolated than separated processes

They share memory, file handles, and other states that each process should have. Because the thread partition scale is smaller than the process, the concurrency of multi-thread is high. The process has a separate memory unit during execution, and multiple threads share memory, which greatly improves the efficiency of the program's operation.

Threads have higher performance than processes because threads in the same process have commonalities----multiple threads share the same process virtual space. Thread-shared environments include process code snippets, process public data, and so on. With these shared data, it is easy for threads to communicate with each other

When the operating system creates a process, it must allocate a separate memory space for the process and allocate a large number of related resources, but creating a thread is much simpler, so using multithreading to achieve concurrency is much higher than concurrency with multiple-process implementations

    • Summing up, using multithreaded programming has several advantages:
    • Memory is not shared between processes, but it is easy to share memory between threads
    • The system needs to reallocate system resources for the process when it is created, but it is much less expensive to create threads, so multi-tasking is more efficient than multi-process using multithreading
    • The Java language has built-in support for multithreading, rather than simply being a scheduler for the underlying operating system, simplifying multithreaded programming in Java

In practical applications, multithreading is very useful, a browser must be able to download multiple images at the same time, a Web server must be able to respond to multiple user requests at the same time, the Java virtual machine itself in the background provides a super thread for garbage collection; graphical user interface (GUI) Apps also need to start a separate thread to collect user interface events from the host environment ... In short, multithreading in the actual programming of the application is very extensive.

JAVA Multithreading and Concurrent Learning notes (iv)

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.