I write Engineering Topic weekly...?

Blog of web engineer at Tokyo. [Japanese version] http://taichiw.hatenablog.com/

1 million threads are executable in parallel with "Fiber" !? - Project Valhalla & Project Loom : JavaDayTokyo 2018 Report 2 #JavaDayTokyo

I attended sessions about Project Valhalla and Project Loom. Speaker of both sessions was Mr. David Buck *1.
Unfortunately, both functionality will not be in Java 11. Then, we can use them in actual service in 2021 *2 in the earliest case.

Project Valhalla

Value Types

Here is a class which has primitive type fields.

class Point {
   int x;
   int y;
}

Then, if we make these kind of array

  Point[] points;

each Point instances have their headers. Those will be huge overhead on memory.

This better if we think only memory optimization but it's not cool on object oriented programming.

  int[] xList;
  int[] yList;

So, here is one of solution*3.
If the class is declared with "value" like this, the memory allocation behave similarly to "primitive array".

value class Point {
   int x;
   int y;
}

Then, not only optimizing memory allocation, also improving speed for referencing each elements becomes possible.


Generic Specialization

In addition, the gap between primitive and class will be removed more.

For example, this declaration becomes possible.

ArrayList<int> xList;

In other example, we don't need "special implementation for primitive" like intStream anymore.
It's "not cool" that only primitive type variable requires special method.

Project Loom

On current JVM architecture, threads on JVM are mapped with threads managed by OS. The purpose of each threads might be different. Some of them might be used for very heavy process like encryption. On the other hand, some of them might be used for very light process, like just updating any variable. But OS need to make "thread, it's possible to use anything" because OS doesn't know what is the use for each threads.

Then, new idea "Fibers" was introduced. Fibers are threads, which are managed by run time of Java or user's code, not by OS. Application can manage threads (= Fibers) much better than OS because it knows how those threads are used. Surprisingly, JVM can generate thousand times threads than OS.

Blocking vs asynchronous

The traditional "thread way" is weak for scaling. Even if that thread is just waiting I/O, those are need to be kept on memory. As solution against this issue, "asynchronous" is used. But this way is difficult to coding / debugging. It's much more complex than "thread way".

"Fibers" can resolve these issues at once. The program can be kept simple because Fibers are just threads from program's perspective. On the other hand, it's strong for scalability too.

"Next era!"

Again, Fibers are threads. So each fibers has information "what will be executed" and "what has been executed". At the same time, those are objects managed by Java. Thus, they are "seriallizable".

Then... these will be possible.

  • Serialize and save a running thread. Next, stop JVM. Later JVM re-start and continue the saved thread.
  • On distributed database, transfer a process (= a fiber) between servers.

The current development stage

Right now, the development is on very early stage. Nothing is committed yet to remote repository because Developers just enjoy on their local machine lol
In the best case, any early access will be released within this year.

Anyway, this is very exciting improvement of Java. I'm looking forward to the release.

*1:He also ran demo of Fright Recorder in the keynote session. He looked so busy

*2:next LTS version is 17, will be released in 2021

*3:"How to write" is still under discussion. It's NOT fixed yet