Learning notes of “advocating Silicon Valley advanced technology – JUC high concurrency programming” 01 – overview of JUC


What is JUC

JUC is java Util The abbreviation of concurrent toolkit is the toolkit for processing threads, which has appeared since java1.5.

Concepts of processes and threads

Processes and threads

A process is a running activity of a program in a computer on a certain data set, which is carried out by the systemBasic unit of resource allocation and scheduling, is the foundation of the operating system structure.

A thread is an operating system that canMinimum unit of operation scheduling. It is included in the process and is the actual operating unit in the process.

Process status

Thread enumeration class:

public enum State{
     //New status
     //Ready status
     //Blocking state
     //Waiting status
     //Timeout waiting
     //Termination status

  • New:After calling new() to create a thread, it is in the new state. At this time, the thread only has the Java virtual machine to allocate memory and initialize its member variables. It does not show the dynamic characteristics of any thread.
  • Runnable:When the thread object calls start (), the thread is in a ready state. The Java virtual machine creates a method call stack and a program counter for it, waiting for the thread scheduler in the JVM to schedule. Once the cup is obtained, the execution body in the run () method is executed.
  • Blocked:A thread that is interrupted during execution and waits for a monitor lock. When the following conditions occur, the thread will enter the blocking state:
  • The thread calls the sleep () method to actively give up the CPU resources occupied;
  • A thread calls a blocking IO method. Before the method returns, the thread is blocked;
  • The thread attempted to acquire a synchronization monitor, but the synchronization monitor is being used by another thread;
  • The thread is waiting for a notification;
  • The program called the thread susoend() method to suspend the thread. This method is easy to cause deadlock and is not recommended.
  • Waiting:A thread that is waiting for an operation to be performed by another thread is in this state.

  • Time_ Waiting:A thread that is waiting for another thread to perform an action is in this state.

  • Terminated:The thread completes execution and enters the termination state.

The five state transition relationships of threads are shown in the following figure:

Learning notes of

The difference between wait() / sleep()
  • Sleep () is the static method of thread, and wait () is the method of object, which can be called by any object instance.
  • Sleep () does not release the lock, nor does it need to occupy the lock. While wait() will release the lock and need to re acquire the lock through notify() / notifyall().
  • Sleep () can be called anywhere, while wait () can only be called in a synchronized method or synchronized code block.
Concurrency and parallelism

Serial mode:Serial means that all tasks are carried out in sequence one by one. Serial is to get only one task at a time and execute this task.

Parallel mode:Parallelism means that multiple tasks can be obtained at the same time, and the obtained tasks can be executed at the same time. The efficiency of parallelism strongly depends on multi-process / multi-threaded code from the code level, and depends on multi-core CPU from the hardware point of view.

Concurrency:Concurrency refers to the phenomenon that multiple programs can run at the same time. More specifically, multiple processes or multiple instructions can run at the same time.

Concurrency: multiple threads are accessing the same resource at the same time, and multiple threads are connected to one point
Example: Spring Festival transportation ticket grabbing e-commerce spike
Parallel: multiple tasks are executed together and then summarized
Example: making instant noodles, boiling water in an electric kettle, tearing the seasoning and pouring it into the bucket

Tube pass

Tube pass (monitor)It ensures that only one process is active in the pipe at the same time, that is, the operations defined in the pipe are called by only one process at the same time (implemented by the compiler), but this does not guarantee that the processes execute in the designed order.

Synchronization in the JVM is implemented based on the entry and exit monitor objects. Each object will have a monitor object, and the monitor will be created and destroyed along with the Java object.

The execution thread must hold the pipe process object before executing the method. When the method is completed, it will release the pipe process. The method will hold the pipe process during execution. Other threads cannot obtain the same pipe process.

User threads and daemon threads

User thread:Ordinary threads and custom threads used in daily life.

Daemon thread:Running in the background, it is a special thread, such as garbage collection.

When the main thread ends, the user thread is still running and the JVM survives. If there are no user threads, they are all daemon threads, and the JVM ends


Learning notes of