1 What is a thread
A thread is a sequential flow control within a program. The thread itself cannot run. It can only be used in programs, using resources and environments that are assigned to programs.
2 The difference between a process and a thread
- The internal data and state of multiple processes are completely independent, and multithreading is a shared piece of memory space and a set of system resources that may interact with each other.
- The data of the thread itself is usually only the register data, and a program executes the stack that is used, so the thread switching is less burdensome than the process switching.
3 Multi-Threading
Multithreading refers to the ability to run multiple different threads at the same time in a single program to perform different tasks.
- A process can contain one or more threads.
- A program that implements multiple code runs concurrently requires multithreading.
- The CPU randomly takes the time to get our program to do this one thing and do another thing.
4 purpose of multi-threading
Is "Maximize CPU resources", when a thread processing does not need to occupy the CPU, but only with resources such as I/O, so that other threads that need to consume CPU resources have the opportunity to gain CPU resources. This is the ultimate goal of multithreaded programming.
5. Single Thread
- When the program starts running, it automatically generates a thread, and the main method is run on this thread.
- Our programs are executed by the thread.
JAVA Learning Summary Multithreading