I. Process: a dynamic execution of a program that corresponds to a complete process of loading from code, execution to execution, is a dynamic entity that has its own life
Cycle. It is created because it is scheduled to run, is waiting for a resource or event, and is undone due to completion of the task. Reflects a program in
All dynamic processes that run on a certain set of data. A process is uniquely identified by a Process control block (PCB). While the process occupies the appropriate resources (such as a package
Including CPU usage, rotation time, and some other device's permissions). is a system for resource allocation and scheduling of an independent unit.
The main differences between programs and processes are:
Whether the state has a resource whether it has a unique identity and whether it has concurrency
Process Dynamic There are there are
Program Static No No No
The basic state of the process:
1. Ready status
When the process has been allocated all the necessary resources except the CPU, it can be executed as soon as the CPU is acquired, and the state of the process is called the ready state. A process that is in a ready state in a system can have multiple, usually lined up in a queue, known as a ready queue.
2. Execution status
The process has obtained the CPU and its program is executing. In a single processor system, only one process is in the execution state, and in the multiprocessor system, multiple processes are in the execution state.
3. Blocking status
While the executing process is temporarily unable to continue execution due to an event, it abandons the processor and is suspended, that is, the execution of the program is blocked, which is called the blocking state, sometimes called the waiting state or the blocking state.
Conversion diagram between three processes:
Threads: can be understood as a number of execution threads for a process, each of which corresponds to its own independent life cycle. A thread is an entity of a process that is CPU-dispatched and dispatched
Basic unit, which is a smaller unit that can run independently than a process. One thread can create and revoke another thread, multiple threads in the same process
can be executed concurrently.
Here is a detailed explanation of the concepts of processes and threads in Java.
Threads in Java go through 4 processes
1) Create
There are two common ways to create a Java thread:
Inherit the thread class and implement the Runnable interface in two ways.
2) execution
After a thread is created, it only occupies memory resources, and the thread does not yet have it in the JVM-managed thread, which must call the Start method to notify the JVM, so the JVM
You'll know that another thread has queued up. If the current thread is CPU-bound, the current thread will continue to execute.
3) Interrupt
A.JVM switches the use of the CPU from the current thread to another thread, leaving this thread out of the CPU's usage rights and in a broken state.
B. The thread calls the sleep method during execution, leaving the current thread in hibernation.
C. The thread calls the wait method during execution
D. A thread performs an action while it is using CPU resources in such a blocking state.
4) Death
The thread of death is not capable of execution. There are two causes of thread death:
A. Death caused by the end of the thread's normal operation, that is, the Run method is executed.
B. The thread was prematurely forced to terminate.
Second: the creation and use of threads
Java uses the thread class to represent threads, and all thread objects must be instances of thread or its subclasses, each of which performs a certain task, in effect executing a program flow (code that executes sequentially)
Scenario One: Inheriting the thread class to create a threading class
steps: 1. Define a subclass of the thread class and override the Run method of the class, which represents the task that the thread needs to complete
2. Create an instance of the thread class, that is, the threading object is created
3. Call the thread's Start method to start the thread
Conclusion: When creating a thread class using subclasses of the inherited child thread class, multiple threads cannot share instance variables of the thread class (for example, I above)
Scenario Two: Implementing the Runnable interface
1: Define the implementation class for the Runnable interface and override its run method, which is also the execution of the thread !
2: Create an instance of the Runnable implementation class, and create a thread object as the target of the thread, the thread object is the real threading object !
3: Call the Start method to start the thread
Conclusion: Adopt The Ruunable interface creates multiple threads that can share instance variables of the thread class, because in this way the Runnable object created by the program is only the target of the thread, and multiple threads can share a target, so multiple threads can share an instance variable
through Runnable implementing multithreading is actually wrapping the run as a thread's execution, but currently Java cannot wrap any method into a thread-executing body
Programme III:UseCallable and future Create threads
from Java5 Start, Java provides the callable interface, the callable interface provides a call () method that can be used as the thread execution body, looks like runnable, but the call () method is more powerful--call () method can have a return value, The call () method can throw an exception
JAVA5 provides a future interface to represent the return value of the call () method of the callable interface, and provides a Futuretask implementation class for the future interface that implements the class future interface, The Runnable interface is also implemented-it can be used as the target of thread.
Implementation steps:
1: Create an implementation class for the callable interface, and implement the Call method, which will become the thread execution body, and the call method has a return value in creating the implementation class of the callable interface!
2: Use the Futruetask class to wrap the callable object, which Futruetask encapsulates the return value of the call method of the class callable
3: Create and start a new thread using the Futruetask object as the target of thread !
4: Use The Get method of Futruetask to get the return value after execution ends
Conclusion: The advantage of taking Runnable and callable is that the thread class just implements the Runnable or callable interface, and can inherit other classes; In this way, multiple threads can share a target object, Therefore, it is very suitable for multiple same threads to handle the same resource, thus separating the CPU, code and data, and the model with clear parameters, which embodies the object-facing programming idea. The disadvantage is a slightly higher degree of programming complexity.
A detailed explanation of processes and threads in Java