Fibers

Java does not have fibers, but as there are some libraries that support fiber handlings, it is worth mentioning. A fiber is a finer unit than a thread. A program code executing in a thread may decide to give up the execution and tell the fiber manager to just execute some other fiber. What is the point and why is it better than using another thread? The reason is that this way, fibers can avoid part of the context switch.  A context switch cannot be avoided totally because a different part of the code that starts to execute it may use the CPU registers in a totally different way. As it is the same thread, the context switching is not the task of the OS, but the application.

The OS does not know if the value of a register is used or not. There are bits in the registers, and no one can tell seeing only the processor state whether those bits are relevant for the current code execution or just happen to be there in that way. The program generated by a compiler does know which registers are important and which are those that can just be ignored. This information changes from place to place in the code, but when there is a need for a switch, the fiber passes the information of what is needed to be switched at that point to the code that does the switching.

The compiler calculates this information, but Java does not support fibers in the current version. The tools that implement fibers in Java analyze and modify the byte code of the classes to do this after the compilation phase.

Golang's goroutines are fibers and that is why you can easily start many thousand goroutines in Go, but you better limit the number of threads in Java to a lower number. They are not the same things.

As the terminology lightweight process is fading out and used by less and less fibers, many times are referred to as lightweight threads.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.233.205