At work last week (Java Web development), I modified a tool class that took a long time to support multithreading. At the same time, the client sends an Ajax request at intervals to obtain the processing progress information of the tool class. The following is a summary of the work. The process and thread are to be further studied. The summary is as follows:
1. Process
1.1 process concept
Definition of operating system principles: a process is an execution process of a data set of a program with independent functions that can be executed concurrently. It is also the basic unit for the operating system to allocate and schedule resources.
A process is an ongoing program. an instance of a running program in the computer can be allocated to an entity executed by the processor. It is displayed by execution in a single order, the Activity Unit described by the current status and a group of related system resources. For example, when running an EXE file in windows, the system allocates resources such as CPU and memory to open up a new process for the executable EXE file, that is to say, only after the program is executed will the system open up processes for it. When the program is not executed, it is just an executable file stored on the disk.
1.2 Process status and transition
A process sometimes occupies a processor for execution during its entire lifecycle from creation to cancellation,
Although it can run but cannot be divided into processors, sometimes there are idle processors, but unable to execute because of waiting for an event, all this shows that the process and the program are different, it is active and stateful.
(1) three-state model conversion
In general, at least three different process states should be defined according to the process's different execution conditions:
Running status: the process occupies the processor and is running.
Ready (ready) status: the process has running conditions, waiting for the system to allocate a processor for running.
Blocking status: it is also called the blocking or sleep state. It means that the process is waiting for an event to complete because it does not have running conditions.
Process Conversion Diagram:
Process tri-state model and Transformation
(2) five-state model Conversion Diagram
Creating a process is generally divided into two steps: first, creating a PCB (Process Control Block) for a new process and entering necessary management information; second, transfer the process to the ready status and insert it into the ready queue.
Termination state: terminating a process is also divided into two steps: first, the operating system performs post-processing on it, and then clears the PCB.
PS: PCB concept.
PCB is used to describe and control the running of processes. The system defines a data structure for each process-Process Control Block PCB (processcontrolblock ), it is part of a process entity (program segment, related data segment, and PCB) and is the most important record-type data structure in the operating system. The PCB records all information required by the operating system to describe the current situation of the process and control the running of the process.
Process five-state model and Transformation
(3) Process status with the pending Function System and Its Conversion
Due to the continuous creation of processes, system resources, especially memory resources, cannot meet the running requirements of processes; or some processes need to be suspended due to system faults or other reasons, some processes must be suspended (suspend), switched to the disk image area, and some resources occupied by it must be released, so that they are not involved in low-level scheduling for the moment to smooth the system operation load.
Processes and conversions with the pending Function System
2 threads
2.1 relationship between threads and processes
Processes are the basic unit for the system to allocate resources, the virtual address space required to store the image, the primary storage space required for execution, and other peripheral device resources and files required to complete the task. Processes are also the basic unit of processor scheduling.
The thread has many characteristics of traditional processes, so it becomes lightweight integration or process meta, and can use threads as scheduling and allocation. In a process, there are usually one or more threads. These threads do not own resources, but share the resources of the process. In a process, a process is the basic unit of resource ownership, while a thread is the basic unit of scheduling and scheduling. multiple threads share the resources of the process.
The role of 2.2 threads
Improve the speed of concurrent execution of programs in the system, and further improve the throughput of the system. The introduction of threads can reduce the time-space overhead of the Program During Concurrent execution, so that the system has better concurrency.
3. threads in Java
3.1 two ways to create a thread in Java
The first method is to implement the runnable interface, and the second method is to inherit the Thread class. The two methods are described as follows:
(1) Implement the runnable interface (this method is recommended)
Let's take a look at a specific example,
Public class test implements runnable {/** how to start the thread */public static void main (string [] ARGs) {test = new test (); thread t = new thread (TEST); // The method for instantiating a thread object. Use the start method t. start (); // call the start method of the thread to start the thread. After the thread starts, the run method of the test class}/** is automatically called to implement the run method of the runnable interface */Public void run () {// what to do and what to process in the run method //...}}
(2) inherit the Thread class
Public class test extends thread {/** how to start the thread */public static void main (string [] ARGs) {test = new test (); thread t = new thread (TEST); // instantiate a thread object and use the start method t. start (); // call the start method of the thread to start the thread. After the thread starts, the run method of the test class}/** is automatically called to implement the run method of the runnable interface */Public void run () {// what to do and what to process in the run method //...}}
Note that the run method of the thread class must be rewritten to inherit the Thread class.
Others