What is a process?
A process is in a running program, it has its own independent memory space, a set of system resources, each process of internal data and state are independent, for example, the window is open more than one Notepad at the same time, although they run the program code is the same, but the use of memory space is independent, non-interference.
What is a thread?
A thread is similar to a process, a piece of code that accomplishes a particular function, a single sequential flow control in a program, but unlike a process, multiple threads of the same class share a chunk of memory space and a set of system resources, and the thread itself usually has only the microprocessor's register data and a stack for the program to execute.
The difference between a process and a thread
Process: Each process has its own code and data space (process context), and the process switching overhead is high.
Threads: A lightweight process that shares code and data spaces with a class of threads, each with a separate run stack and program counter (PC), with minimal thread switching overhead.
Multi-process: In the operating system, you can run multiple task programs at the same time.
Multithreading: In the same application, there are multiple sequential streams executing simultaneously.
Two ways to create threads
Creating Threads with inherited thread classes
This method is relatively simple, mainly by inheriting the Java.lang.Thread class and overriding the thread class's run () method to complete the creation of the line. The thread class is a concrete class, not an abstract class, that encapsulates the behavior of a thread. To create a thread, the programmer must create a new class that is exported from the thread class. The thread class has two of the most important functions run () and start ().
Creating a thread by implementing the Runnable interface
The method generates a class that implements the Java.lang.Runnable interface. This interface only defines a method run (), so it must be implemented in a new class. But the Runnable interface does not support any threads, and we must also create an instance of the thread class by The constructor for the Thread class public thread (Runnable target);
Java's processes and threads