The difference between programs, processes, and threads.
Program: is a collection of instructions. The program cannot execute independently, only if it is loaded into memory and the system allocates resources to it.
Process: An executing program is called a process.
A process is a separate unit of system-assigned resources, each of which is a specific address space for a comrade.
A program is a static text description of a process. A process is a dynamic activity that a program executes sequentially within a system.
Thread: Is the "single continuous flow of control" of the process.
Threads are the basic unit of CPU scheduling and allocation, and are the basic units that can run independently, and are also known as lightweight processes, that are smaller than the process.
Threads cannot exist independently, must be attached to a process, and a process can include multiple threads in parallel.
Attached: Java creates threads in two ways, in fact it is essentially the same, because the thread class itself implements the Runnable interface.
public class Mydefinedthread extends thread{
Override the Run () method
@Override
public void Run () {
Write the task that the thread needs to perform in the run () method;
}
public static void Main (string[] args) {
Mydefinedthread MDT = new Mydefinedthread ();
Mdt.start ();
}
}
public class Myrunnable implements Runnable {
@Override
public void Run () {
Write the task that the thread needs to perform in the run () method;
}
public static void Main (string[] args) {
Myrunnable Mr = New Myrunnable ();
Mr is not a thread object, but is passed as a parameter to the constructor of the thread; "th" is a thread object
Thread th = new Thread (MR);
Th.start ();
}
}
The difference between programs, processes, and threads in Java.