Java thread Basics
preface
What is a thread? Threads, Sometimes called lightweight process (LWP), it is the smallest unit of program execution flow. A standard thread consists of thread ID and current instruction pointer (PC), register set and stack. In addition, thread is an entity in the process and the basic unit independently scheduled and dispatched by the system. Thread does not own system resources and only has a little essential resources in operation, but it can share all resources owned by the process with other threads belonging to the same process.
One thread can create and undo another thread, and multiple threads in the same process can execute concurrently. Due to the mutual restriction between threads, threads show discontinuity in operation. Threads also have three basic states: ready, blocked and running. Ready state means that the thread has all the conditions for running, can run logically, and is waiting for the processor; Running status refers to that the thread occupies the processor running; Blocking state means that a thread is waiting for an event (such as a semaphore) and is logically not executable. Every program has at least one thread. If the program has only one thread, it is the program itself.
Process vs thread
Processes and threads are inclusive, but multitasking can be implemented by multiple processes or threads, or mixed multi processes + multi threads.
Compared with multithreading, the disadvantages of multithreading are:
Advantages of multi process:
Multithreaded application scenarios
Life cycle and five basic states
First, let's take a look at the description of thread status given in the thread class:
Next, take a look at the classic diagram of thread life cycle in Java:
The above figure basically covers the important knowledge points of multithreading in Java. After mastering the knowledge points in the figure above, you can basically master multithreading in Java. It mainly includes:
Java threads have five basic states
Take a popular example to explain the above five states, such as going to the bathroom:
You usually go to the bathroom in the mall, When you are ready to go to the toilet, you are in a new state. When you go to the toilet, you have to queue up. When there is a pit, it's your turn. Shit is running. After you shit, you find that there is no toilet paper. You have to wait for someone to send you paper. This state is blocking (blocked), when you come out after going to the toilet, you will be dead when going to the toilet is over.
Note: constipation is also a state of obstruction. You've been constipated for too long, and others can't wait to drive you away. This is hanging up. There's another situation. You're constipated, and others can't wait. I'll tell you to go out and brew for a while, and then come and shit in 5 minutes. This is sleep.
Implementation of custom thread
From a practical point of view, if you want to use multithreading, the first step is to know how to implement custom threads. Because the tasks that need to be completed by threads are different in actual development, we need to define threads according to thread tasks. JDK provides our developers with three ways to customize threads for use in actual development, To develop multi-threaded programs that meet the requirements!
The following are three implementation methods of thread, and the advantages and disadvantages of each implementation are analyzed. Finally, these three implementation methods are summarized;
Method 1: inherit thread class
Advantages: the implementation is simple. You only need to instantiate the instance of the inherited class to use threads. Disadvantages: the extensibility is insufficient. Java is a single inherited language. If a class has inherited other classes, you can't implement custom threads in this way
Method 2: implement runnable interface
advantage:
Disadvantages: the process of constructing thread instances is relatively cumbersome
Method 3: implement callable interface
advantage:
Disadvantages: