[analysis of process, thread and fiber]

Process, thread, fiber:

Concept:
Process: a running program
Thread: an execution queue in a running program. A process has multiple threads
Fiber: generally refers to threads in threads; The kernel space is called when the thread starts, and the user space is called when the fiber starts. Switching between threads is heavy and consumes more resources; Fiber switching is lightweight and consumes less resources. Thread is the concurrency of multiple program streams in kernel space; Fiber is the concurrency of multiple program streams in user space.
For example, Linux is divided into two system levels, one is user space and the other is kernel space. When starting a thread, the kernel space needs to be called, so the switching between threads will be heavy and consume more resources; In order to improve efficiency, moving threads to user space will consume low resources and high efficiency. This is the fiber path. Each fiber has its own stack, and its switching is also through the stack.
Summary: a fiber is a thread in user mode and a thread in a thread. Switching and scheduling do not need to go through the operating system, lightweight threads; JAVA does not support fiber directly until JDK13. If you want to "fiber", you need to implement it through quasar class library

There are three ways to start threads in Java:
The first is to inherit the Thread class:
Create and start a multithread by inheriting the Thread class. The steps are as follows:
1. Define a subclass of the Thread class and rewrite the run() method of the class. The run() method body represents the tasks that the Thread needs to complete. Therefore, the run() method is called the Thread execution body;
2. Create a subclass thread object and start the thread by calling the start() method of the parent class
Create subclasses:

public class Thread1 extends Thread{
	
	@Override
	public void run() {
		for (int i = 0; i < 10; i++) {
			System.out.println(Thread.currentThread().getName()+"implement"+i);
		}
		
	}

}

Create test class Main:

public class Main {

	public static void main(String[] args) {
		new Thread1().start();
		for (int i = 0; i < 10; i++) {
			System.out.println(Thread.currentThread().getName()+"implement"+i);
		}

	}

}

Log printing:

main Execute the 0 th time
Thread-0 Execute the 0 th time
main Execute the first time
Thread-0 Execute the first time
Thread-0 Execute the 2nd time
Thread-0 Execute the 3rd time
Thread-0 Execute the 4th meeting
Thread-0 Execute the 5th meeting
Thread-0 Execute the 6th meeting
Thread-0 Execute the 7th Meeting
Thread-0 Execute the 8th meeting
Thread-0 Execute the 9th meeting
main Execute the 2nd time
main Execute the 3rd time
main Execute the 4th meeting
main Execute the 5th meeting
main Execute the 6th meeting
main Execute the 7th Meeting
main Execute the 8th meeting
main Execute the 9th meeting

Summary: mian thread and thread-0 thread execute alternately in an irregular loop. In the main function, we create an instance object of this thread and start the thread through the start method. During execution, you can see that the thread runs in a preemptive manner. Although only one thread instance is created, there are actually two threads running, as well as the thread execution body of the main thread represented by the main method.
expand:
1. Thread.currentThread(), a static method of thread class, always returns the thread object currently executing.
2. getName(): this method is an instance method of thread class, which returns the name of the thread currently executing. By default, the name of the main thread is main, and the names of multi threads started by the user are Thread-0,Thread-1,Thread3... Thread-n, etc.

The second is to implement the Runnable interface
The steps to create and start multithreading by implementing the Runnable interface are as follows:
1. Define the implementation class of the Runnable interface, rewrite the run() method of the interface, and the method body of the run() method is also the thread execution body of the thread.
2. Create an instance object of the Runnable implementation class, and use the instance object as the target of the Thread to create the Thread class. The Thread object is the real Thread object.
3. Call the start() method of the thread object to start the thread.

Define the implementation class of Runnable interface:

public class Thread2 implements Runnable {

	@Override
	public void run() {
		for (int i = 0; i < 10; i++) {
			System.out.println(Thread.currentThread().getName()+" Executive paragraph"+i+"second");
		}

	}
}

Mian class:

public class Main {

	
	public static void main(String[] args) {
		new Thread(new Thread2()).start();
		for (int i = 0; i < 10; i++) {
			System.out.println(Thread.currentThread().getName()+" Executive paragraph"+i+"second");
		}

	}

}

Log printing:

main Execute the 0 th time
Thread-0 Execute the 0 th time
main Execute the first time
Thread-0 Execute the first time
main Execute the 2nd time
Thread-0 Execute the 2nd time
Thread-0 Execute the 3rd time
Thread-0 Execute the 4th meeting
main Execute the 3rd time
Thread-0 Execute the 5th meeting
Thread-0 Execute the 6th meeting
Thread-0 Execute the 7th Meeting
Thread-0 Execute the 8th meeting
Thread-0 Execute the 9th meeting
main Execute the 4th meeting
main Execute the 5th meeting
main Execute the 6th meeting
main Execute the 7th Meeting
main Execute the 8th meeting
main Execute the 9th meeting

In particular, pay attention to:
Instead of directly executing the run method of Thread2, the name in the main function fills Thread2 into the Thread and uses the start method to start it. The Runable implementation class contains the run method, which is only used as the Thread executor. The actual Thread object is still the Thread instance object, and Thread is the object that really creates the Thread

The third type: start through anonymous inner class (thread pool):
The anonymous inner class is essentially a class that implements the Runnable interface and rewrites the run method, but this class has no name and is directly passed into the Thread class as a parameter. Example code:

public class Main {

	public static void main(String[] args) {
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				for (int i = 0; i < 10; i++) {
					System.out.println(Thread.currentThread().getName()+" Executive paragraph"+i+"second");
				}
			}
		}).start();
		for (int i = 0; i < 10; i++) {
			System.out.println(Thread.currentThread().getName()+" Executive paragraph"+i+"second");
		}

	}
}

Log printing:

main Execute the 0 th time
Thread-0 Execute the 0 th time
main Execute the first time
Thread-0 Execute the first time
main Execute the 2nd time
Thread-0 Execute the 2nd time
main Execute the 3rd time
Thread-0 Execute the 3rd time
main Execute the 4th meeting
Thread-0 Execute the 4th meeting
Thread-0 Execute the 5th meeting
Thread-0 Execute the 6th meeting
Thread-0 Execute the 7th Meeting
Thread-0 Execute the 8th meeting
Thread-0 Execute the 9th meeting
main Execute the 5th meeting
main Execute the 6th meeting
main Execute the 7th Meeting
main Execute the 8th meeting
main Execute the 9th meeting

Common thread methods:
Thread.yield(): the current thread gives up CPU, goes back to the waiting queue, and grabs CPU with other threads. It may grab again or it may not be able to grab Thread.join(): if the t2.join thread is invoked in the t1 thread, then it will run to the T2 thread after running. T2 will run back to t1 when it is running, usually waiting for the thread, and the thread will be executed in sequence.

Status of thread:
Creation state: the state in which the thread is successfully created is the creation state
Ready state: the state when the thread creating the state is started is the ready state, the running thread releases CPU resources to the ready state, or the thread blocking in the blocking state is released to the ready state.
Running status: a thread in ready status obtains CPU resources and reaches the running status.
Blocking state: the running thread waits for user input, thread hibernation, etc. to reach the thread blocking state or
Dead state: when the thread is naturally executed or terminated by external interference, the thread reaches the dead state

How to get the state of a thread: thread.getState().

Tags: Multithreading thread pool

Posted on Tue, 30 Nov 2021 05:16:32 -0500 by jds580s