DEV Community

loading...

Discussion on: Java is Dead - Long Live Java

Collapse
johanneslichtenberger profile image
Johannes Lichtenberger

Great article, but the callback APIs in Vert.x allowed to implement the other "higher level" or better alternatives very easily. For instance Kotlin Coroutines based methods... and also Promise/Future based versions.

BTW: I think Fibers are only the pendant to other languages coroutines implementations, right?

Collapse
stealthmusic profile image
Jan Wedel Author

Yes, coroutines are a way to implement synchronous looking asynchronous code.

Fibers are a JVM feature as an alternative to heavyweight threads. On top of that, you can build coroutines as a language feature. You can also build Erlang-style Actors without any hacking like byte code manipulation.

Collapse
johanneslichtenberger profile image
Johannes Lichtenberger

So, I guess It's also a big win for Kotlin and Scala. However, what are "lightweight" threads? I guess I understood Coroutines in general, that when the thread would be blocked by I/O for instance, a suspending function suspends itself and a continuation is stored in a scheduler. The thread can do other things until a scheduler decides, that the suspending function / continuation should be resumed on whatever thread/thread pool is specified when I/O is done. However, here threads are always heavyweight and the Coroutines in Kotlin are called Lightweight for instance. However, what the JVM add as lightweight threads? Have to read again about fibers. I think that's like the distinction between the upcoming record types (data classes in Kotlin) and the JVM value types. There, I also have no clue what value types on the JVM mean.

Thread Thread
stealthmusic profile image
Jan Wedel Author

First, there are couple of talks on YouTube if you want to get into the details, e.g. youtube.com/watch?v=vbGbXUjlRyQ

Your assumption about coroutines is right. Fibers is the underlying JVM concept. A fiber is basically a data container that is able to hold stackframes (e.g. local variables) as well as pointer that knows the current location in the code. When a operation blocks, the pointer is saved and the OS thread can be used to run another Fiber until the data is available and the initial Fiber resumes the code using the pointer and the stack.

Thread Thread
stealthmusic profile image
Jan Wedel Author

Record types are basically syntactic suger. If you want to write a simple data holding class that has a couple of fields, a constructor and getters and setters, you can simply use record and you don't need to actually write all that but you can still use it.

Value types are a memory optimization in the JVM where you can actually pack all primitive types (int, String) and arrays in a sequencial order in memory without the needs of references (pointers).

Thread Thread
johanneslichtenberger profile image
Johannes Lichtenberger

Thanks a lot, will watch this talk now. I assume Roman and his team use some workaround / not the optimal solution to implement in Kotlin Coroutines without this JVM enhancement. But that said it'll help Kotlin as well.

I have to say Kotlin might be hyped, but almost all items from Effective Java for instance can be implemented much easier and the defaults (for instance that classes can not be extended per default are much better -- should probably be like that as it's just a few years old). Also I think Generics for instance are much better implemented/easier to use as well as the non null type hierarchy and a lot of other stuff is great. Nontheless, I still mostly (have to) write Java code in my day to day job and I hesitate to mix Java/Kotlin in one module in a multi maven module project of mine :) but I use it in one module exclusively (using Vert.x and its Coroutine based API, which I think is really great).

Thread Thread
stealthmusic profile image
Jan Wedel Author

Yeah, maybe that’s a nice way to sneak in new language to just mix them. 😉

However, if you’re working in a team in a larger code base, you will need to get a team agreement first.