The concept of threads & life cycle

1. What is process

Baidu Baike:

Process is a running activity of a program on a data set in a computer. It is the basic unit of resource allocation and scheduling in the system and the basis of operating system structure. In the early process-oriented computer architecture, the process is the basic execution entity of the program. In modern thread-oriented computer architectures, processes are containers for threads. A program is a description of instructions, data and their organizational form, while a process is an entity of the program.

For example, turn on Tiktok. Tiktok is a process

2. What are threads

Baidu Baike:

Is the smallest unit in which an operating system can schedule operations. It is contained in the process and is the actual operating unit of the process. A process consists of multiple threads.

Exe click to start the process

For example, when tiktok gives a “like” to a beautiful woman, the process needs to open a thread to give the “like” +1

Thread switching consumes system resources. Coroutines/fibers are smaller and faster

3. Thread lifecycle

1. New: The newly created thread has not yet started execution

2. Runnable: After the start() method is called, it is handed over to the thread scheduler for execution, which is the responsibility of the operating system

Runnable can be divided into two states:

When a Thread is in the waiting state of the CPU, waiting for the CPU to execute, or half of the CPU to execute, the current Thread is suspended, or thread.yield () is used to yield the CPU. Running Status The CPU has the execution permissionCopy the code

3. Blocked: A thread enters a synchronized block and then blocks without obtaining the resource

Waiting: Wait join locksuport.park () is in Waiting state and notify, notifyAll, and locksuport.unpark () are required to escape from Waiting state

5. TimeWaiting: Similar to Waiting but running out of Waiting after a certain amount of time has been added

The thread content Terminated cannot enter any other state after it is Terminated

Look at it once and forget it after a while so find a way to keep it in your mind

Suppose you find an object (Thread)

The possible states of your object are:

Just found object: New

Object clean and drill into bed: Ready

You are free to find someone under the bed: Running

Object to sleep: TimeWaiting

You have to take a bath, the object has to go to the toilet need waitting at the door

You get angry and lock the object out of the door: The object is Blocked and he can’t get in

4. Take a look at the code
// Enumeration of thread states
public enum State {
        NEW,
        RUNNABLE, // May be Ready or Running depending on CPU scheduling
        BLOCKED,
        WAITING,
        TIMED_WAITING,
        TERMINATED;
}
Copy the code

NEW:

 Thread t1 = new Thread(()->{
            System.out.println(Thread 1 "");
 });

System.out.println("T1 state:"+t1.getState().name()); // NEW 
Copy the code

RUNNABLE:

final Thread t1 = new Thread(()->{
    try {
        while (true){ Thread.yield(); }}catch (Exception e){}
});

t1.start();

new Thread(()->{
    System.out.println("T1 state:"+t1.getState().name());
}).start();
Copy the code

Waiting:

public static Object o = new Object();
public static void main(String[] args) throws InterruptedException {
    final Thread t1 = new Thread(()->{
        try {
            synchronized(o){ o.wait(); }}catch (Exception e){}
    });

    t1.start();

    Thread.sleep(1000);

    new Thread(()->{
        System.out.println("T1 state:"+t1.getState().name());
    }).start(); // WAITING

}
Copy the code

TIMED_WAITING:

public static Object o = new Object();
public static void main(String[] args) throws InterruptedException {
    final Thread t1 = new Thread(()->{
        try {
            synchronized (o){
                o.wait(10000); }}catch (Exception e){}
    });

    t1.start();

    Thread.sleep(1000);

    new Thread(()->{
        System.out.println("T1 state:"+t1.getState().name());
    }).start();
}
Copy the code

Blocked:

public static Object o = new Object();
public static void main(String[] args) throws InterruptedException {
    final Thread t1 = new Thread(()->{
        try {
            Thread.sleep(1000);
            synchronized (o){

            }
        } catch (Exception e){}
    });

    t1.start();



    new Thread(()->{
        synchronized (o){
            try {
                Thread.sleep(2000);
                System.out.println("T1 state:"+t1.getState().name());//Blocked
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }).start();
}
Copy the code

Welcome to our official account: