[Java] JUC concurrent programming - process thread

1. What is JUC

JUC is the abbreviation of java.util.concurrent package, added in Java5.0, the purpose is to better support high concurrent tasks. Let developers reduce the problems of race conditions and deadlocks when doing multi-threaded programming!

2. Processes and threads

Multiple threads in a process

1. Process

A process is the smallest unit of resource allocation

Refers to an application program that is running in the system. Once the program is running, it is a process.

2. Thread

Thread is the smallest unit of program execution

The basic unit of system allocation of processor time resources, or a unit of execution flow that is independently executed within a process

Three, the six states of the thread

public enum State {
    
    
    // 新生
    NEW,

    // 运行
    RUNNABLE,

    // 阻塞
    BLOCKED,

    // 等待
    WAITING,

    //超时等待
    TIMED_WAITING,

    //终止
    TERMINATED;
}

Fourth, the difference between wait and sleep

  • sleep is a static method of Thread, and wait is a 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. wait will release the lock, but the premise of calling it is that the current thread holds the lock (that is, the code must be in synchronized)
  • Both can be interrupted by the interrupted method

5. Concurrency and parallelism

Concurrency: Multiple threads are accessing the same resource at the same time, and multiple threads are on one point
Parallelism: Multiple tasks are performed together and aggregated later

1. Serial mode

Serial means that all tasks are performed one by one in sequence. Serial means that a car of firewood must be loaded before it can be transported, and only after it has been delivered can the car of firewood be unloaded, and only after completing these three steps can the next step proceed.

Serial can only get one task at a time and execute this task

2. Parallel mode

Parallelism means that multiple tasks can be obtained at the same time, and these obtained tasks can be executed at the same time. The parallel mode is equivalent to dividing a long queue into multiple short queues, so the length of the task queue is shortened in parallel. Parallel efficiency strongly depends on multi-thread/multi-thread code from the code level, and depends on multi-core CPU from the hardware point of view

3. Concurrent mode

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

4. Monitor

Monitor: It is called a "lock" in Java and a "monitor" in the operating system. It is a synchronization mechanism that ensures that only one thread accesses the protected data or code at the same time.
Jvm synchronization operates based on the process of entry and exit, and entry and exit are implemented using monitor objects. Each object will have a Monitor object. The monitor object is created and destroyed along with the Java object.

In layman's terms: the monitor object is to lock our critical section, lock when entering, and unlock when exiting. Entry and exit are managed through the monitor object

6. User threads and daemon threads

1. User thread (custom thread)

public static void main(String[] args) {
    
    
    Thread thread = new Thread(() -> {
    
    
        //isDaemon()表示是用户线程还是守护线程,如果值为true,则为守护线程,如果值为false,则为用户线程
        System.out.println(Thread.currentThread().getName() + "::" + Thread.currentThread().isDaemon());
        while (true) {
    
    

        }
    }, "thread");

    thread.start();
    System.out.println(Thread.currentThread().getName()+" over");
}

Execution result: the main thread is over, and the user thread is still running, indicating that the jvm is in a state of survival

insert image description here

2. Daemon threads (such as garbage collection)

public static void main(String[] args) {
    
    
    Thread thread = new Thread(() -> {
    
    
        //isDaemon()表示是用户线程还是守护线程,如果值为true,则为守护线程,如果值为false,则为用户线程
        System.out.println(Thread.currentThread().getName() + "::" + Thread.currentThread().isDaemon());
        while (true) {
    
    

        }
    }, "thread");

    //设置守护线程
    //用户线程包括守护线程,在设置用户线程包括守护线程的时候,要写在start()方法之前
    thread.setDaemon(true);

    thread.start();
    System.out.println(Thread.currentThread().getName()+" over");
}

Execution result: there are no user threads, and they are all daemon threads, indicating that the jvm has ended

insert image description here

Supongo que te gusta

Origin blog.csdn.net/weixin_45490023/article/details/131951975
Recomendado
Clasificación