Multithreading is an inevitable and important subject in Java. Below we will develop the learning of multi-threading. The following content is an explanation of Java multi-threaded content before "Adding JUC packages in JDK", which includes interfaces such as wait(), notify() in the Object class; interfaces in the Thread class; synchronized keyword.
Note: The JUC package refers to the Java.util.concurrent package, which is done by Java master Doug Lea and added to Java in JDK 1.5 version.
Before entering the study of the following chapters, first understand some related concepts of multithreading.
Thread status diagram
illustrate:
Threads include the following 5 states.
1. New state: After the thread object is created, it enters the new state. For example, Thread thread = new Thread().
2. Runnable: Also known as "executable state". After the thread object is created, other threads call the object's start() method to start the thread. For example, thread.start(). A thread in a ready state may be scheduled to execute by the CPU at any time.
3. Running state (Running): The thread obtains CPU permissions for execution. It should be noted that threads can only enter the running state from the ready state.
4. Blocked state: Blocked state means that the thread gives up the CPU usage rights for some reason and temporarily stops running. It is not until the thread enters the ready state that it has a chance to go to the running state. There are three types of blockage:
(01) Waiting to block - By calling the thread's wait() method, let the thread wait for the completion of a certain work.
(02) Synchronized blocking--A thread fails to acquire synchronized synchronization lock (because the lock is occupied by other threads), it will enter a synchronized blocking state.
(03) Other blocking--The thread will enter a blocking state by calling sleep() or join() of the thread or issuing an I/O request. When the sleep() state timed out, join() waited for thread to terminate or timed out, or I/O processing was completed, the thread re-entered to the ready state.
5. Dead state: The thread has finished executing or exited the run() method due to an exception, and the thread ends its life cycle.
The contents involved in these 5 states include the Object class, Thread and synchronized keywords. We will learn these contents one by one in the following chapters.
Object class, defines sleep/wake functions such as wait(), notify(), notifyAll(), etc.
Thread class defines some column thread operation functions. For example, sleep() sleep function, interrupt() interrupt function, getName() get thread name, etc.
synchronized is a keyword; it is distinguished by synchronized code blocks and synchronized methods. The function of synchronized is to allow threads to acquire the synchronization lock of the object.
When we introduce wait(), notify() and other methods in detail later, we will analyze why "wait(), notify() and other methods should be defined in the Object class, not Thread class."