[Chat and Miscellaneous Talk] The concept of fiber

First of all, we must understand several concepts: program, process, thread, fiber.

In a very strict definition, a process is the basic unit used by the operating system for resource scheduling. Later, it was found that the switching of processes was too resource-intensive, so threads were born; switching back and forth with too many threads was still very resource-intensive, so fibers were born. Therefore, from process to thread to fiber, it can be regarded as a layer-by-layer optimization.

So what is a program? From the perspective of the win system, an .exe executable file on the hard disk is a program. Double-click this program, and an instance running in the system is a process. So in theory, a program can correspond to multiple process instances, but now many programs are limited to a single process.

In a process, different execution paths are threads one by one, such as UI thread, network thread, etc. Different paths can be divided into one thread, which is fiber.

If you have learned the basic principles of the operating system, you should know how the operating system switches between threads. When the CPU wants to switch between different threads, each thread will maintain a stack inside itself. Every time the thread is switched, the information in the stack in the thread will be saved, so that through the information in the stack, it is known that each thread executes to where is it.

The mutual switching between fibers is also completed by maintaining the information in the stack. The essential difference between threads and fibers is that the switching of threads needs to pass through the kernel space, while the switching of fibers does not need to pass through the kernel space. The Linux system is divided into user space (user) and kernel space (kernel). The JVM must run in the user space. When some system calls need to be made, they must be called through the kernel space, such as starting a thread. Every time a thread is started, it needs to be called from the kernel space, which takes up more resources, so thread-level concurrency is relatively heavyweight, and switching between threads consumes more resources and takes longer. In order to solve this problem, some people suggested that the thread should be moved to the user space, so that the switching between them does not go through the kernel space, takes up less resources, and has high execution efficiency.

As far as the current operating system is concerned, threads can't actually start up too much. When a system has 10,000 threads, basically all resources are wasted on switching between threads, and nothing serious can be done. If it is fiber, it is still easy to run tens of thousands of them.

However, until JDK13, there was no official support for the fiber thread. With the release of the JDK19 GA version, the feature of virtual threads also made its debut. Virtual threads are lightweight threads implemented by JDK rather than OS. Many virtual threads Sharing the same operating system thread, the number of virtual threads can be much larger than the number of operating system threads, which is a bit like a fiber thread. Oracle is always slower than others in doing things, and I don't know what to think in my mind all day long. If you want to use fibers now, you have to use some third-party open source fiber libraries, such as Quasar.

        <dependency>
            <groupId>co.paralleluniverse</groupId>
            <artifactId>quasar-core</artifactId>
            <version>0.8.0</version>
        </dependency>

The Quasar class library is quite funny to say, the latest version is version 0.8.0 released in 2018, and then there is no more, and it has not been updated to version 1.0 for 23 years... Bring this thing Just play around, and it is not recommended to use it in actual projects.

package com.feenix.fiberboot;

import co.paralleluniverse.fibers.Fiber;
import co.paralleluniverse.fibers.SuspendExecution;
import co.paralleluniverse.strands.SuspendableRunnable;

public class HelloFiber {

    public static void main(String[] args) {
        long s = System.currentTimeMillis();

        for (int k = 0; k < 10000; k++) {
            Fiber<Void> fiber = new Fiber<>(new SuspendableRunnable() {
                @Override
                public void run() throws SuspendExecution, InterruptedException {
                    cal();
                }
            });
            fiber.start();

            /*Thread thread = new Thread(() -> {
                cal();
            });
            thread.start();*/
        }

        long e = System.currentTimeMillis();
        System.out.println("耗时:" + (e - s));
    }

    static void cal() {
        int result = 0;
        for (int i = 0; i < 10000; i++) {
            for (int j = 0; j < 100; j++) {
                result += j;
            }
        }
    }

}

For the same calculation process, the time consumption of using threads is about 3~5 times that of fibers. This is still based on the fact that the amount of data is not very large. As the amount of data increases, the efficiency of fibers is more than faster than threads. A little bit.

おすすめ

転載: blog.csdn.net/FeenixOne/article/details/128550319