A Weaver’s Tale, Part 1

“Destiny itself is like a wonderful wide tapestry in which every thread is guided by an unspeakable tender hand, placed beside another thread and held and carried by a hundred others.”
― Rainer Maria Rilke

My first post in the Weaver’s Tale series will be a brief recap of Threads in Java with attention paid to the history of Project Loom.

Java has been publicly available since at least 1995. This was, at the very least, when I first became aware of and started playing around with it. One of the “features” that quickly caught my attention was the ability to easily create and run code in threads outside of the main Process thread.

Java SE 11 Thread JavaDoc shows that the Thread class has been around since Java 1.0. At the time I was suffering a really intense course at Uni learning about how to do this sort of thing in C on Unix and other languages including Occam (which has an entirely different take on this: constructed parallelism). So I started applying what I was learning to Java and rapidly entered a world of hurt.

The whole threading thing in most languages up until this day has kind of been left behind while languages and ecosystems evolve with tasty syntactic sugar, API enhancements and Frameworks “that take the complexity out of your life”. All of this is good, without change and improvement things die off. Java is no exception to this.

In the context of concurrency: we got, as examples of the evolution, Non-Blocking IO in Java 4, Concurrency API in Java 5. Outside of standard Java a veritable forest of frameworks to make use and extend the capabilities of these APIs appeared. Notable examples are Netty takes the pain out of Non-Blocking IO, ReactiveX claims to take the pain out of concurrency and Kotlin introduced Co-Routines. The list is too large to publish here.
The last 2 I will get back to later.

The idea of lightweight Threads was, briefly in mainstream Java, explored and implemented in Java 1.1 but only on a few JVMs. Interested readers might want to check out the Wikipedia Page on green threads in the virtual machine.

In short the foundation of all of the above enhancements, the Thread, is still a big, fat, resource hungry monster of a thing which is incredibly easy to (ab)use and even easier to get horribly wrong.

That is until Project Loom. I first became aware of this when someone called my attention to a tweet by Ron Pressler (@pressron) in 2018. Now I am not going to go through the evolution of Project Loom on here. For the current state of the project you should read Ron Pressler’s State of the Loom series.
To get an idea of how Project Loom has evolved I thoroughly recommend visiting the Project Loom Wiki and watching the Videos from 2018 and 2019 (I was present at the Devoxx BE talks, I recommend those first).

A few weeks ago I decided to take the plunge and start using the Java 16 EA Loom build. The results of my explorations will make up the bulk of the A Weaver’s Tale series.

In my next Weaver’s Tale post I will dive in on the API changes that Loom brings with it and look at how migrating to lightweight threads can be achieved.

Leave a Reply