JAVA (multithreading)

1.1 The difference between process and thread
Process is the basic unit of operating system resource allocation, and thread is the basic scheduling unit of CPU.
After a program runs, there is a process.
A process can contain multiple threads, but at least one thread is required, otherwise this thread is meaningless.
The address of the data segment cannot be shared between processes, but it can be shared between threads of the same process.
1.2 Composition of threads
Any thread has basic components:
CPU time slice: The operating system will allocate execution time for each thread.
Running data:
Heap space: Store objects that threads need to use, and multiple threads can share objects in the heap.
Stack space: store local variables that threads need to use, and each thread has an independent stack.
Thread Logic Code
1.3 Thread Features
Thread preemptive execution.
efficient.
It can prevent a single thread from monopolizing the CPU for a long time.
In a single-core CPU, execution is performed simultaneously at the macro level, and sequentially at the micro level.
1.4. Thread creation
There are three ways to create a thread:
inherit the Thread class and rewrite the run method.
Implement the Runnable interface.
Implement the Callable interface.
 

Guess you like

Origin blog.csdn.net/weixin_54255580/article/details/122071517